Blame Automation/Modules/centos-art.sh-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 0de111
    if [[ -f ${TCAR_CLI_INIT_DIR}/cli.conf ]];then
Alain Reguera Delgado 0de111
        . ${TCAR_CLI_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 0de111
    for CLI_FUNCTION in $(ls ${TCAR_CLI_INIT_DIR}/Scripts/*.sh);do
Alain Reguera Delgado 615395
        if [[ -x ${CLI_FUNCTION} ]];then
Alain Reguera Delgado 615395
            . ${CLI_FUNCTION}
Alain Reguera Delgado 615395
            export -f $(grep '^function ' ${CLI_FUNCTION} | cut -d' ' -f2)
Alain Reguera Delgado 8f60cb
        else
Alain Reguera Delgado 615395
            echo "${CLI_FUNCTION} `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 615395
    # Redefine arguments using current positional parameters. 
Alain Reguera Delgado 615395
    cli_setArguments "${@}"
Alain Reguera Delgado 615395
Alain Reguera Delgado 615395
    # Redefine positional parameters using arguments variable.
Alain Reguera Delgado 0de111
    eval set -- "${TCAR_MODULE_ARGUMENTS}"
Alain Reguera Delgado 0de111
Alain Reguera Delgado 0de111
    # Check the number of arguments passed in the command-line
Alain Reguera Delgado 0de111
    # interface.  The first argument must be the name of the module
Alain Reguera Delgado 0de111
    # you want to execute. So at least one argument must be provided
Alain Reguera Delgado 0de111
    # in the command-line.
Alain Reguera Delgado 0de111
    if [[ $# -lt 1 ]];then
Alain Reguera Delgado 0de111
        cli_printHelp ${TCAR_CLI_NAME}
Alain Reguera Delgado 0de111
    else
Alain Reguera Delgado 0de111
        # Check module's name. The module's name is critical for
Alain Reguera Delgado 0de111
        # centos-art.sh script to do something coherent. If it is not
Alain Reguera Delgado 0de111
        # provided or provided incorrectly, finish the script
Alain Reguera Delgado 0de111
        # execution with help information.
Alain Reguera Delgado 0de111
        if [[ ! "${1}" =~ '^[[:alpha:]]+$' ]];then
Alain Reguera Delgado 0de111
            cli_printHelp ${TCAR_CLI_NAME}-cli
Alain Reguera Delgado 0de111
        fi
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 0de111
    # Define function name (MODULE_INIT_NAME) using the first argument
Alain Reguera Delgado 0de111
    # in the command-line.
Alain Reguera Delgado 0de111
    local MODULE_INIT_NAME=$(cli_getRepoName ${1} -f | cut -d '-' -f1)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define function directory.
Alain Reguera Delgado 0de111
    local MODULE_INIT_DIR=${TCAR_CLI_BASEDIR}/Modules/${TCAR_CLI_NAME}-${MODULE_INIT_NAME}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define function file name.
Alain Reguera Delgado 0de111
    local MODULE_INIT_FILE=${MODULE_INIT_DIR}/${MODULE_INIT_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 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 0de111
    ${MODULE_INIT_NAME} "${@}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}