Blame Scripts/Functions/cli_checkRepoDirSource.sh

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