Blame Automation/centos-art.sh-mods/Cli/cli_exportFunctions.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# cli_exportFunctions.sh -- This function standardizes the way
Alain Reguera Delgado 8f60cb
# specific functionalities are exported to centos-art.sh script
Alain Reguera Delgado 8f60cb
# environment.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 8f60cb
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 8f60cb
# your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
# $Id$
Alain Reguera Delgado 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
function cli_exportFunctions {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Retrieve export identifier for the function we want to export.
Alain Reguera Delgado 8f60cb
    local EXPORTID="$1"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify the export identification existence. This argument must
Alain Reguera Delgado 8f60cb
    # be passed as first argument and match a relative path format.
Alain Reguera Delgado 8f60cb
    if [[ ! $EXPORTID =~ '^[A-Z][[:alpha:]]+(/[[:alpha:]_]+)+$' ]];then
Alain Reguera Delgado 8f60cb
        cli_printMessage "`gettext "The export id doesn't match its pattern."`" --as-error-line
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define the source location where function files are placed in.
Alain Reguera Delgado 8f60cb
    local LOCATION=${CLI_FUNCDIR}/$(dirname ${EXPORTID})
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define suffix used to retrieve function files.
Alain Reguera Delgado 8f60cb
    local SUFFIX=$(basename "$EXPORTID")
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify the suffix value used to retrieve function files.
Alain Reguera Delgado 8f60cb
    # Assuming no suffix value is passed as second argument to this
Alain Reguera Delgado 8f60cb
    # function, use the function name value (CLI_FUNCNAME) as default
Alain Reguera Delgado 8f60cb
    # value.
Alain Reguera Delgado 8f60cb
    if [[ $SUFFIX == '' ]];then
Alain Reguera Delgado 8f60cb
        SUFFIX="${CLI_FUNCNAME}"
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Redefine suffix to match all related function files inside the
Alain Reguera Delgado 8f60cb
    # related function directory.
Alain Reguera Delgado 8f60cb
    SUFFIX=${SUFFIX}'[[:alpha:]_]*'
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define the pattern used to retrieve function names from function
Alain Reguera Delgado 8f60cb
    # files.
Alain Reguera Delgado 8f60cb
    local PATTERN="^function[[:space:]]+${SUFFIX}[[:space:]]+{[[:space:]]*$"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define the list of files.
Alain Reguera Delgado 8f60cb
    local FUNCFILE=''
Alain Reguera Delgado 8f60cb
    local FUNCFILES=$(cli_getFilesList ${LOCATION} --pattern="${LOCATION}/${SUFFIX}\.sh$" \
Alain Reguera Delgado 8f60cb
        --maxdepth='1' --mindepth='1' --type='f')
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify the list of files. If no function file exists for the
Alain Reguera Delgado 8f60cb
    # location specified stop the script execution. Otherwise the
Alain Reguera Delgado 8f60cb
    # script will surely try to execute a function that haven't been
Alain Reguera Delgado 8f60cb
    # exported yet and report an error about it.
Alain Reguera Delgado 8f60cb
    if [[ $FUNCFILES == '' ]];then
Alain Reguera Delgado 8f60cb
        cli_printMessage "${FUNCNAME}: `gettext "No function file was found."`" --as-error-line
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Process the list of files.
Alain Reguera Delgado 8f60cb
    for FUNCFILE in $FUNCFILES;do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Verify the execution rights for function file.
Alain Reguera Delgado 8f60cb
        cli_checkFiles -x ${FUNCFILE}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Verify that function files have not been already exported.
Alain Reguera Delgado 8f60cb
        # If they have been already exported don't export them again.
Alain Reguera Delgado 8f60cb
        # Instead, continue with the next function file in the list.
Alain Reguera Delgado 8f60cb
        declare -F | gawk '{ print $3 }' | egrep "^${FUNCFILE}$" > /dev/null
Alain Reguera Delgado 8f60cb
        if [[ $? -eq 0 ]];then
Alain Reguera Delgado 8f60cb
            continue
Alain Reguera Delgado 8f60cb
        fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Initialize the function file.
Alain Reguera Delgado 8f60cb
        . ${FUNCFILE}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Export the function names inside the file to current shell
Alain Reguera Delgado 8f60cb
        # script environment.
Alain Reguera Delgado 8f60cb
        export -f $(egrep "${PATTERN}" ${FUNCFILE} | gawk '{ print $2 }')
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}