From 69f807fe06d23f70c76f220d235f2403b851ed2f Mon Sep 17 00:00:00 2001 From: Alain Reguera Delgado Date: Jul 15 2013 04:12:44 +0000 Subject: Update centos-art.sh script. - Refactor code used to initialize module environments in cli function through cli_initModule function. - In order for cli_initModule function to be reusable it is necessary to define the MODULE_BASEDIR variable inside the most specific module's configuration file. - The value verification of TCAR_USER_EDITOR variable was removed from cli function. I didn't put in centos-art.conf either. So, be sure to put an existence text editor as value to this variable. By default, /bin/vi is being used. --- diff --git a/Automation/Modules/Cli/Scripts/cli_getOptions.sh b/Automation/Modules/Cli/Scripts/cli_getOptions.sh index b4d3f72..d606855 100755 --- a/Automation/Modules/Cli/Scripts/cli_getOptions.sh +++ b/Automation/Modules/Cli/Scripts/cli_getOptions.sh @@ -53,7 +53,9 @@ function cli_getOptions { # Define valid long options. local ARGSL="help,version" - # Initialize arguments local to this function environment. + # Define module arguments local to this function. This is very + # important in order to provide option parsing for different + # function environment levels. local TCAR_ARGUMENTS='' # Redefine arguments using getopt(1) command parser. diff --git a/Automation/Modules/Cli/Scripts/cli_initModule.sh b/Automation/Modules/Cli/Scripts/cli_initModule.sh new file mode 100755 index 0000000..38b496d --- /dev/null +++ b/Automation/Modules/Cli/Scripts/cli_initModule.sh @@ -0,0 +1,65 @@ +#!/bin/bash +###################################################################### +# +# cli_initModule.sh -- This function initiates module environments +# inside the centos-art.sh script. +# +# Written by: +# * Alain Reguera Delgado , 2009-2013 +# Key fingerprint = D67D 0F82 4CBD 90BC 6421 DF28 7CCE 757C 17CA 3951 +# +# Copyright (C) 2013 The CentOS Project +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or (at +# your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# +###################################################################### + +function cli_initModule { + + # Define module's name (MODULE_NAME) using the first argument + # in the command-line. + local MODULE_NAME=$(cli_getRepoName "${1}" "-f" | cut -d '-' -f1) + + # Define regular expression to match available modules. + local MODULE_NAME_LIST=$(ls ${MODULE_BASEDIR}/ \ + | tr '\n' '|' | sed -r 's/\|$//' | tr '[[:upper:]]' '[[:lower:]]') + + # Check module's name possible values. + if [[ ! ${MODULE_NAME} =~ "^(${MODULE_NAME_LIST})$" ]];then + cli_printMessage "`gettext "The module provided isn't valid."`" --as-error-line + fi + + # Define function directory. + local MODULE_DIR=${MODULE_BASEDIR}/$(cli_getRepoName "${MODULE_NAME}" "-d") + + # Define function file name. + local MODULE_INIT_FILE=${MODULE_DIR}/${MODULE_NAME}.sh + + # Check function script execution rights. + cli_checkFiles -x ${MODULE_INIT_FILE} + + # Remove the first argument passed to centos-art.sh command-line + # in order to build optional arguments inside functionalities. We + # start counting from second argument (inclusive) on. + shift 1 + + # Go for function initialization. Keep the cli_exportFunctions + # function calling after all variables and arguments definitions. + cli_exportFunctions "${MODULE_INIT_FILE}" + + # Execute function. + ${MODULE_NAME} "${@}" + +} diff --git a/Automation/Modules/Cli/cli.conf b/Automation/Modules/Cli/cli.conf old mode 100644 new mode 100755 index 6359c68..1dae65f --- a/Automation/Modules/Cli/cli.conf +++ b/Automation/Modules/Cli/cli.conf @@ -30,6 +30,7 @@ # Command-line interface (read-only) configuration variables. # ---------------------------------------------------------------------- declare -xr MODULE_VERSION='0.1' +declare -x MODULE_BASEDIR=${TCAR_SCRIPT_BASEDIR}/Modules # ---------------------------------------------------------------------- # Internationalization configuration variables. diff --git a/Automation/Modules/Cli/cli.sh b/Automation/Modules/Cli/cli.sh index 207b174..d10d7e8 100755 --- a/Automation/Modules/Cli/cli.sh +++ b/Automation/Modules/Cli/cli.sh @@ -29,12 +29,7 @@ function cli { - # Initialize command-line interface default configuration values. - if [[ -f ${TCAR_SCRIPT_INIT_DIR}/cli.conf ]];then - . ${TCAR_SCRIPT_INIT_DIR}/cli.conf - fi - - # Initialize list of common functionalities to load. + # Initialize command-line interface common functions. for MODULE_SCRIPT in $(ls ${TCAR_SCRIPT_INIT_DIR}/Scripts/*.sh);do if [[ -x ${MODULE_SCRIPT} ]];then . ${MODULE_SCRIPT} @@ -54,60 +49,16 @@ function cli { # `Ctrl+C'). trap cli_terminateScriptExecution 0 - # Define module arguments local to this function. This is very - # important in order to provide option parsing for different - # function environment levels. - local TCAR_ARGUMENTS='' - - # Define module's name (MODULE_NAME) using the first argument - # in the command-line. - local MODULE_NAME=$(cli_getRepoName "${1}" "-f" | cut -d '-' -f1) - - # Define regular expression to match available modules. - local MODULE_NAME_LIST=$(ls ${TCAR_SCRIPT_BASEDIR}/Modules/ \ - | sed -r 's/.+-([[:alnum:]]+)$/\1/' \ - | tr '\n' '|' | sed -r 's/\|$//') - - # Check module's name possible values. - if [[ ! ${MODULE_NAME} =~ "^(${MODULE_NAME_LIST})$" ]];then - cli_printMessage "`gettext "The module provided isn't valid."`" --as-error-line + # Initialize command-line interface default configuration values. + if [[ -f ${TCAR_SCRIPT_INIT_DIR}/${TCAR_SCRIPT_INIT}.conf ]];then + . ${TCAR_SCRIPT_INIT_DIR}/${TCAR_SCRIPT_INIT}.conf fi - # Define function directory. - local MODULE_DIR=${TCAR_SCRIPT_BASEDIR}/Modules/${TCAR_SCRIPT_NAME}-${MODULE_NAME} - - # Define function file name. - local MODULE_INIT_FILE=${MODULE_DIR}/${MODULE_NAME}.sh - - # Check function script execution rights. - cli_checkFiles -x ${MODULE_INIT_FILE} - - # Remove the first argument passed to centos-art.sh command-line - # in order to build optional arguments inside functionalities. We - # start counting from second argument (inclusive) on. - shift 1 - - # When the word cli is provided as module to centos-art.sh script, - # the cli module reduces its mission to just printing help and - # version information about itself. After that, it must finish the - # script execution successfully. - if [[ ${MODULE_NAME} == "${TCAR_SCRIPT_INIT}" ]];then + # Verify first argument passed to centos-art.sh script. + if [[ ${1} == "${TCAR_SCRIPT_INIT}" ]];then cli_getOptions "${@}" + else + cli_initModule "${@}" fi - # Define default text editors used by centos-art.sh script. - if [[ ! "${TCAR_USER_EDITOR}" =~ '/usr/bin/(vim|emacs|nano)' ]];then - TCAR_USER_EDITOR='/usr/bin/vim' - fi - - # Check text editor execution rights. - cli_checkFiles -x ${TCAR_USER_EDITOR} - - # Go for function initialization. Keep the cli_exportFunctions - # function calling after all variables and arguments definitions. - cli_exportFunctions "${MODULE_INIT_FILE}" - - # Execute function. - ${MODULE_NAME} "${@}" - } diff --git a/Automation/centos-art.conf b/Automation/centos-art.conf index 0a3a19e..f035829 100755 --- a/Automation/centos-art.conf +++ b/Automation/centos-art.conf @@ -52,7 +52,7 @@ declare -xr TCAR_SCRIPT_INIT=cli # # Set the script initialization module's directory. # -declare -xr TCAR_SCRIPT_INIT_DIR=${TCAR_SCRIPT_BASEDIR}/Modules/${TCAR_SCRIPT_NAME}-${TCAR_SCRIPT_INIT} +declare -xr TCAR_SCRIPT_INIT_DIR=${TCAR_SCRIPT_BASEDIR}/Modules/Cli # # Set the script initialization module's file. #