Blame Scripts/Bash/Functions/Commons/cli_checkRepoDirSource.sh

878a2b
#!/bin/bash
878a2b
#
878a2b
# cli_checkRepoDirSource.sh -- This function provides input validation
878a2b
# to repository entries considered as source locations.
878a2b
#
878a2b
# Copyright (C) 2009, 2010, 2011 The CentOS Project
878a2b
#
878a2b
# This program is free software; you can redistribute it and/or modify
878a2b
# it under the terms of the GNU General Public License as published by
878a2b
# the Free Software Foundation; either version 2 of the License, or (at
878a2b
# your option) any later version.
878a2b
#
878a2b
# This program is distributed in the hope that it will be useful, but
878a2b
# WITHOUT ANY WARRANTY; without even the implied warranty of
878a2b
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
878a2b
# General Public License for more details.
878a2b
#
878a2b
# You should have received a copy of the GNU General Public License
878a2b
# along with this program; if not, write to the Free Software
878a2b
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
878a2b
#
878a2b
# ----------------------------------------------------------------------
878a2b
# $Id$
878a2b
# ----------------------------------------------------------------------
878a2b
878a2b
function cli_checkRepoDirSource {
878a2b
878a2b
878a2b
    # Define location in order to make this function reusable not just
878a2b
    # for action value variable but whatever value passed as first
878a2b
    # positional argument.
878a2b
    local LOCATION=$1
878a2b
878a2b
    # Verify location. Assuming no location is passed as first
878a2b
    # positional parameter to this function, print an error message
878a2b
    # and stop script execution.
878a2b
    if [[ "$LOCATION" == '' ]];then
878a2b
        cli_printMessage "`gettext "The first positional parameter is required."`" --as-error-line
878a2b
    fi
878a2b
878a2b
    # Check action value to be sure strange characters are kept far
878a2b
    # away from path provided.
878a2b
    cli_checkPathComponent $LOCATION
878a2b
878a2b
    # Redefine source value to build repository absolute path from
878a2b
    # repository top level on. As we are removing
878a2b
    # /home/centos/artwork/ from all centos-art.sh output (in order to
878a2b
    # save horizontal output space), we need to be sure that all
878a2b
    # strings begining with trunk/..., branches/..., and tags/... use
878a2b
    # the correct absolute path. That is, you can refer trunk's
878a2b
    # entries using both /home/centos/artwork/trunk/... or just
878a2b
    # trunk/..., the /home/centos/artwork/ part is automatically added
878a2b
    # here. 
878a2b
    if [[ $LOCATION =~ '^(trunk|branches|tags)' ]];then
878a2b
        LOCATION=${CLI_WRKCOPY}/$LOCATION 
878a2b
    fi
878a2b
878a2b
    # Re-define source value to build repository absolute path from
878a2b
    # repository relative paths. This let us to pass repository
878a2b
    # relative paths as source value.  Passing relative paths as
878a2b
    # source value may save us some typing; specially if we are stood
878a2b
    # a few levels up from the location we want to refer to as source
878a2b
    # value.  There is no need to pass the absolute path to it, just
878a2b
    # refere it relatively.
878a2b
    if [[ -d ${LOCATION} ]];then
878a2b
878a2b
        # Add directory to the top of the directory stack.
878a2b
        pushd "$LOCATION" > /dev/null
878a2b
878a2b
        # Check directory existence inside the repository.
878a2b
        if [[ $(pwd) =~ "^${CLI_WRKCOPY}" ]];then
878a2b
            # Re-define source value using absolute path.
878a2b
            LOCATION=$(pwd)
878a2b
        else
878a2b
            cli_printMessage "`eval_gettext "The location \\\"\\\$LOCATION\\\" is not valid."`" --as-error-line
878a2b
        fi
878a2b
878a2b
        # Remove directory from the directory stack.
878a2b
        popd > /dev/null
878a2b
878a2b
    elif [[ -f ${LOCATION} ]];then
878a2b
878a2b
        # Add directory to the top of the directory stack.
878a2b
        pushd "$(dirname "$LOCATION")" > /dev/null
878a2b
878a2b
        # Check directory existence inside the repository.
878a2b
        if [[ $(pwd) =~ "^${CLI_WRKCOPY}" ]];then
878a2b
            # Re-define source value using absolute path.
878a2b
            LOCATION=$(pwd)/$(basename "$LOCATION")
878a2b
        else
878a2b
            cli_printMessage "`eval_gettext "The location \\\"\\\$LOCATION\\\" is not valid."`" --as-error-line
878a2b
        fi
878a2b
878a2b
        # Remove directory from the directory stack.
878a2b
        popd > /dev/null
878a2b
878a2b
    fi
878a2b
878a2b
    # Output sanitated location.
878a2b
    echo $LOCATION
878a2b
878a2b
}