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

878a2b
#!/bin/bash
878a2b
#
baf9b1
# cli_exportFunctions.sh -- This function standardizes the way
baf9b1
# specific functionalities are exported to centos-art.sh script
baf9b1
# environment.
878a2b
#
e6bbbf
# Copyright (C) 2009-2013 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_exportFunctions {
878a2b
42f764
    # Retrieve export identifier for the function we want to export.
42f764
    local EXPORTID="$1"
42f764
42f764
    # Verify the export identification existence. This argument must
42f764
    # be passed as first argument and match a relative path format.
035705
    if [[ ! $EXPORTID =~ '^[A-Z][[:alpha:]]+(/[[:alpha:]_]+)+$' ]];then
42f764
        cli_printMessage "`gettext "The export id doesn't match its pattern."`" --as-error-line
a3d8f0
    fi
a3d8f0
a3d8f0
    # Define the source location where function files are placed in.
3b9515
    local LOCATION=${CLI_FUNCDIR}/$(dirname ${EXPORTID})
878a2b
a3d8f0
    # Define suffix used to retrieve function files.
a3d8f0
    local SUFFIX=$(basename "$EXPORTID")
878a2b
a3d8f0
    # Verify the suffix value used to retrieve function files.
a3d8f0
    # Assuming no suffix value is passed as second argument to this
a3d8f0
    # function, use the function name value (CLI_FUNCNAME) as default
a3d8f0
    # value.
878a2b
    if [[ $SUFFIX == '' ]];then
cd44d0
        SUFFIX="${CLI_FUNCNAME}"
878a2b
    fi
878a2b
cd44d0
    # Redefine suffix to match all related function files inside the
cd44d0
    # related function directory.
cd44d0
    SUFFIX=${SUFFIX}'[[:alpha:]_]*'
cd44d0
a3d8f0
    # Define the pattern used to retrieve function names from function
878a2b
    # files.
6b42d3
    local PATTERN="^function[[:space:]]+${SUFFIX}[[:space:]]+{[[:space:]]*$"
878a2b
a3d8f0
    # Define the list of files.
878a2b
    local FUNCFILE=''
d1aa03
    local FUNCFILES=$(cli_getFilesList ${LOCATION} --pattern="${LOCATION}/${SUFFIX}\.sh$" \
cd44d0
        --maxdepth='1' --mindepth='1' --type='f')
878a2b
a3d8f0
    # Verify the list of files. If no function file exists for the
878a2b
    # location specified stop the script execution. Otherwise the
878a2b
    # script will surely try to execute a function that haven't been
878a2b
    # exported yet and report an error about it.
878a2b
    if [[ $FUNCFILES == '' ]];then
a3d8f0
        cli_printMessage "${FUNCNAME}: `gettext "No function file was found."`" --as-error-line
878a2b
    fi
878a2b
a3d8f0
    # Process the list of files.
878a2b
    for FUNCFILE in $FUNCFILES;do
878a2b
a3d8f0
        # Verify the execution rights for function file.
790d2d
        cli_checkFiles -x ${FUNCFILE}
878a2b
6b42d3
        # Verify that function files have not been already exported.
6b42d3
        # If they have been already exported don't export them again.
6b42d3
        # Instead, continue with the next function file in the list.
6b42d3
        declare -F | gawk '{ print $3 }' | egrep "^${FUNCFILE}$" > /dev/null
6b42d3
        if [[ $? -eq 0 ]];then
6b42d3
            continue
6b42d3
        fi
6b42d3
a3d8f0
        # Initialize the function file.
790d2d
        . ${FUNCFILE}
878a2b
a3d8f0
        # Export the function names inside the file to current shell
878a2b
        # script environment.
878a2b
        export -f $(egrep "${PATTERN}" ${FUNCFILE} | gawk '{ print $2 }')
878a2b
878a2b
    done
878a2b
878a2b
}