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

878a2b
#!/bin/bash
878a2b
#
878a2b
# cli_exportFunctions.sh -- This function exports funtionalities to
878a2b
# `centos-art.sh' script execution evironment.
878a2b
#
03486a
# Copyright (C) 2009, 2010, 2011, 2012 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
a3d8f0
    # Verify the number of arguments passed to this function.
a3d8f0
    if [[ $# -lt 1 ]];then
a3d8f0
        cli_printMessage "${FUNCNAME}: `gettext "At least one argument must be passed."`"
a3d8f0
    fi
a3d8f0
a3d8f0
    # Retrive export identifier for the function we want to export.
a3d8f0
    local EXPORTID=$1
a3d8f0
a3d8f0
    # Define the source location where function files are placed in.
cd44d0
    local LOCATION=${CLI_BASEDIR}/Functions/$(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.
a3d8f0
    local PATTERN="^function[[:space:]]+${SUFFIX}[[:space:]]+{$"
878a2b
a3d8f0
    # Define the list of files.
878a2b
    local FUNCFILE=''
a3d8f0
    local FUNCFILES=$(cli_getFilesList ${LOCATION} --pattern="${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.
878a2b
        cli_checkFiles $FUNCFILE --execution
878a2b
a3d8f0
        # Initialize the function file.
878a2b
        . $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
}