Blame Automation/Modules/Cli/cli.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
#   cli.sh -- This function initiates the centos-art.sh script
Alain Reguera Delgado 615395
#   command-line interface. This is the first script the centos-art.sh
Alain Reguera Delgado 615395
#   runs, onces it has been executed in a terminal.
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 {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 0de111
    # Initialize command-line interface default configuration values.
Alain Reguera Delgado ddf7d2
    if [[ -f ${TCAR_SCRIPT_INIT_DIR}/cli.conf ]];then
Alain Reguera Delgado ddf7d2
        . ${TCAR_SCRIPT_INIT_DIR}/cli.conf
Alain Reguera Delgado 0de111
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize list of common functionalities to load.
Alain Reguera Delgado ddf7d2
    for MODULE_SCRIPT in $(ls ${TCAR_SCRIPT_INIT_DIR}/Scripts/*.sh);do
Alain Reguera Delgado ddf7d2
        if [[ -x ${MODULE_SCRIPT} ]];then
Alain Reguera Delgado ddf7d2
            . ${MODULE_SCRIPT}
Alain Reguera Delgado ddf7d2
            export -f $(grep '^function ' ${MODULE_SCRIPT} | cut -d' ' -f2)
Alain Reguera Delgado 8f60cb
        else
Alain Reguera Delgado ddf7d2
            echo "${MODULE_SCRIPT} `gettext "has not execution rights."`"
Alain Reguera Delgado 8f60cb
        fi
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Trap signals in order to terminate the script execution
Alain Reguera Delgado 8f60cb
    # correctly (e.g., removing all temporal files before leaving).
Alain Reguera Delgado 8f60cb
    # Trapping the exit signal seems to be enough by now, since it is
Alain Reguera Delgado 8f60cb
    # always present as part of the script execution flow. Each time
Alain Reguera Delgado 8f60cb
    # the centos-art.sh script is executed it will inevitably end with
Alain Reguera Delgado 8f60cb
    # an EXIT signal at some point of its execution, even if it is
Alain Reguera Delgado 8f60cb
    # interrupted in the middle of its execution (e.g., through
Alain Reguera Delgado 8f60cb
    # `Ctrl+C').
Alain Reguera Delgado 8f60cb
    trap cli_terminateScriptExecution 0
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado ddf7d2
    # Define module arguments local to this function. This is very
Alain Reguera Delgado ddf7d2
    # important in order to provide option parsing for different
Alain Reguera Delgado ddf7d2
    # function environment levels.
Alain Reguera Delgado ddf7d2
    local TCAR_ARGUMENTS=''
Alain Reguera Delgado 615395
Alain Reguera Delgado ddf7d2
    # Define module's name (MODULE_NAME) using the first argument
Alain Reguera Delgado 0de111
    # in the command-line.
Alain Reguera Delgado ddf7d2
    local MODULE_NAME=$(cli_getRepoName "${1}" "-f" | cut -d '-' -f1)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado ddf7d2
    # Define regular expression to match available modules.
Alain Reguera Delgado ddf7d2
    local MODULE_NAME_LIST=$(ls ${TCAR_SCRIPT_BASEDIR}/Modules/ \
Alain Reguera Delgado ddf7d2
        | sed -r 's/.+-([[:alnum:]]+)$/\1/' \
Alain Reguera Delgado ddf7d2
        | tr '\n' '|' | sed -r 's/\|$//')
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
    # Check module's name possible values.
Alain Reguera Delgado ddf7d2
    if [[ ! ${MODULE_NAME} =~ "^(${MODULE_NAME_LIST})$" ]];then
Alain Reguera Delgado ddf7d2
        cli_printMessage "`gettext "The module provided isn't valid."`" --as-error-line
Alain Reguera Delgado ddf7d2
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define function directory.
Alain Reguera Delgado ddf7d2
    local MODULE_DIR=${TCAR_SCRIPT_BASEDIR}/Modules/${TCAR_SCRIPT_NAME}-${MODULE_NAME}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define function file name.
Alain Reguera Delgado ddf7d2
    local MODULE_INIT_FILE=${MODULE_DIR}/${MODULE_NAME}.sh
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Check function script execution rights.
Alain Reguera Delgado 0de111
    cli_checkFiles -x ${MODULE_INIT_FILE}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Remove the first argument passed to centos-art.sh command-line
Alain Reguera Delgado 8f60cb
    # in order to build optional arguments inside functionalities. We
Alain Reguera Delgado 8f60cb
    # start counting from second argument (inclusive) on.
Alain Reguera Delgado 8f60cb
    shift 1
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado ddf7d2
    # When the word cli is provided as module to centos-art.sh script,
Alain Reguera Delgado ddf7d2
    # the cli module reduces its mission to just printing help and
Alain Reguera Delgado ddf7d2
    # version information about itself. After that, it must finish the
Alain Reguera Delgado ddf7d2
    # script execution successfully.
Alain Reguera Delgado ddf7d2
    if [[ ${MODULE_NAME} == "${TCAR_SCRIPT_INIT}" ]];then
Alain Reguera Delgado ddf7d2
        cli_getOptions "${@}"
Alain Reguera Delgado ddf7d2
    fi
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado 8f60cb
    # Define default text editors used by centos-art.sh script.
Alain Reguera Delgado 615395
    if [[ ! "${TCAR_USER_EDITOR}" =~ '/usr/bin/(vim|emacs|nano)' ]];then
Alain Reguera Delgado 615395
        TCAR_USER_EDITOR='/usr/bin/vim'
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
    
Alain Reguera Delgado 8f60cb
    # Check text editor execution rights.
Alain Reguera Delgado 615395
    cli_checkFiles -x ${TCAR_USER_EDITOR}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Go for function initialization. Keep the cli_exportFunctions
Alain Reguera Delgado 8f60cb
    # function calling after all variables and arguments definitions.
Alain Reguera Delgado 0de111
    cli_exportFunctions "${MODULE_INIT_FILE}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Execute function.
Alain Reguera Delgado ddf7d2
    ${MODULE_NAME} "${@}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}