Blame Automation/centos-art.sh-cli/Scripts/cli_exportFunctions.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
#   cli_exportFunctions.sh -- This function standardizes the way
Alain Reguera Delgado 615395
#   specific functionalities are exported to centos-art.sh script
Alain Reguera Delgado 615395
#   environment.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#   Written by: 
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 615395
#     Key fingerprint = D67D 0F82 4CBD 90BC 6421  DF28 7CCE 757C 17CA 3951
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 615395
######################################################################
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 615395
    local FUNCTION_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 615395
    if [[ ! ${FUNCTION_EXPORTID} =~ '^[A-Z][[:alpha:]]+(/[[:alpha:]_]+)+$' ]];then
Alain Reguera Delgado 615395
        cli_printMessage "`gettext "The function's 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 615395
    local FUNCTION_LOCATION=${TCAR_CLI_MODSDIR}/$(dirname ${FUNCTION_EXPORTID})
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define suffix used to retrieve function files.
Alain Reguera Delgado 615395
    local FUNCTION_SUFFIX=$(basename "${FUNCTION_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 615395
    # function, use the function name value (CLI_FUNCTION_NAME) as default
Alain Reguera Delgado 8f60cb
    # value.
Alain Reguera Delgado 615395
    if [[ -z ${FUNCTION_SUFFIX} ]];then
Alain Reguera Delgado 615395
        FUNCTION_SUFFIX="${CLI_FUNCTION_NAME}"
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 615395
    FUNCTION_SUFFIX=${FUNCTION_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 615395
    local FUNCTION_PATTERN="^function[[:space:]]+${FUNCTION_SUFFIX}[[:space:]]+{[[:space:]]*$"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define the list of files.
Alain Reguera Delgado 615395
    local FUNCTION_FILE=''
Alain Reguera Delgado 615395
    local FUNCTION_FILES=$(cli_getFilesList ${FUNCTION_LOCATION} \
Alain Reguera Delgado 615395
        --pattern="${FUNCTION_LOCATION}/${FUNCTION_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 615395
    if [[ -z ${FUNCTION_FILES} ]];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 615395
    for FUNCTION_FILE in ${FUNCTION_FILES};do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Verify the execution rights for function file.
Alain Reguera Delgado 615395
        cli_checkFiles -x ${FUNCTION_FILE}
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 615395
        declare -F | gawk '{ print $3 }' | egrep "^${FUNCTION_FILE}$" > /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 615395
        . ${FUNCTION_FILE}
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 615395
        export -f $(egrep "${FUNCTION_PATTERN}" ${FUNCTION_FILE} | gawk '{ print $2 }')
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}