Blame Automation/centos-art.sh-mods/Cli/cli.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# cli.sh -- This function initiates the centos-art.sh script
Alain Reguera Delgado 8f60cb
# command-line interface.  Variables defined in this function are
Alain Reguera Delgado 8f60cb
# accesible by all other functions. The cli function is the first
Alain Reguera Delgado 8f60cb
# script executed by centos-art.sh, onces executed in a terminal.
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 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
# $Id$
Alain Reguera Delgado 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
function cli {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize global variables.
Alain Reguera Delgado 8f60cb
    local CLI_FUNCNAME=''
Alain Reguera Delgado 8f60cb
    local CLI_FUNCDIRNAM=''
Alain Reguera Delgado 8f60cb
    local CLI_FUNCSCRIPT=''
Alain Reguera Delgado 8f60cb
    local ARGUMENTS=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize default value to filter flag. The filter flag
Alain Reguera Delgado 8f60cb
    # (--filter) is used mainly to reduce the number of files to
Alain Reguera Delgado 8f60cb
    # process. The value of this variable is interpreted as
Alain Reguera Delgado 8f60cb
    # egrep-posix regular expression.  By default, when the --filter
Alain Reguera Delgado 8f60cb
    # option is not provided, all paths in the working copy must match
Alain Reguera Delgado 8f60cb
    # except files under hidden directories like `.svn'. We do this in
Alain Reguera Delgado 8f60cb
    # conjunction with `cli_getFilesList', when building the list of
Alain Reguera Delgado 8f60cb
    # files that will be processed.
Alain Reguera Delgado 8f60cb
    local FLAG_FILTER='[[:alnum:]_/-]+'
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize default value to verbosity flag. The verbosity flag
Alain Reguera Delgado 8f60cb
    # (-v | --verbose) controls whether centos-art.sh script prints
Alain Reguera Delgado 8f60cb
    # messages or not. By default, all messages are suppressed except
Alain Reguera Delgado 8f60cb
    # those directed to standard error.
Alain Reguera Delgado 8f60cb
    local FLAG_QUIET='false'
Alain Reguera Delgado 8f60cb
    
Alain Reguera Delgado 8f60cb
    # Initialize default value to answer flag. The answer flag
Alain Reguera Delgado 8f60cb
    # (--answer-yes) controls whether centos-art.sh script does or
Alain Reguera Delgado 8f60cb
    # does not pass confirmation request points. By default, it
Alain Reguera Delgado 8f60cb
    # doesn't.
Alain Reguera Delgado 8f60cb
    local FLAG_ANSWER='false'
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize default value to commit changes flag. This flag
Alain Reguera Delgado 8f60cb
    # (--synchronize) controls whether version control system is
Alain Reguera Delgado 8f60cb
    # triggered or not after realizing changes to source files under
Alain Reguera Delgado 8f60cb
    # version control.
Alain Reguera Delgado 8f60cb
    local FLAG_SYNCHRONIZE='false'
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize list of common functionalities to load.
Alain Reguera Delgado 8f60cb
    local FILES=$(ls ${CLI_FUNCDIR}/Commons/*.sh)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize common functionalities.
Alain Reguera Delgado 8f60cb
    for FILE in ${FILES};do
Alain Reguera Delgado 8f60cb
        if [[ -x ${FILE} ]];then
Alain Reguera Delgado 8f60cb
            . ${FILE}
Alain Reguera Delgado 8f60cb
            export -f $(grep '^function ' ${FILE} | cut -d' ' -f2)
Alain Reguera Delgado 8f60cb
        else
Alain Reguera Delgado 8f60cb
            echo "`eval_gettext "The \\\$FILE needs to have execution rights."`"
Alain Reguera Delgado 8f60cb
            exit
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 8f60cb
    # Redefine ARGUMENTS variable using current positional parameters. 
Alain Reguera Delgado 8f60cb
    cli_parseArgumentsReDef "$@"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Check function name. The function name is critical for
Alain Reguera Delgado 8f60cb
    # centos-art.sh script to do something coherent. If it is not
Alain Reguera Delgado 8f60cb
    # provided, execute the help functionality and end script
Alain Reguera Delgado 8f60cb
    # execution.
Alain Reguera Delgado 8f60cb
    if [[ ! "$1" ]] || [[ ! "$1" =~ '^[[:alpha:]]' ]];then
Alain Reguera Delgado 8f60cb
        cli_runFnEnvironment help --read --format="texinfo" tcar-fs:::
Alain Reguera Delgado 8f60cb
        exit
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define function name (CLI_FUNCNAME) using the first argument in
Alain Reguera Delgado 8f60cb
    # the command-line.
Alain Reguera Delgado 8f60cb
    CLI_FUNCNAME=$(cli_getRepoName $1 -f | cut -d '-' -f1)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define function directory.
Alain Reguera Delgado 8f60cb
    CLI_FUNCDIRNAM=$(cli_getRepoName $CLI_FUNCNAME -d)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define function file name.
Alain Reguera Delgado 8f60cb
    CLI_FUNCSCRIPT=${CLI_FUNCDIR}/${CLI_FUNCDIRNAM}/${CLI_FUNCNAME}.sh
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Check function script execution rights.
Alain Reguera Delgado 8f60cb
    cli_checkFiles -x "${CLI_FUNCSCRIPT}"
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
    # Redefine ARGUMENTS using current positional parameters.
Alain Reguera Delgado 8f60cb
    cli_parseArgumentsReDef "$@"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define default text editors used by centos-art.sh script.
Alain Reguera Delgado 8f60cb
    if [[ ! "$EDITOR" =~ '/usr/bin/(vim|emacs|nano)' ]];then
Alain Reguera Delgado 8f60cb
        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 8f60cb
    cli_checkFiles -x ${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 8f60cb
    cli_exportFunctions "${CLI_FUNCDIRNAM}/${CLI_FUNCNAME}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Execute function.
Alain Reguera Delgado 8f60cb
    $CLI_FUNCNAME
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}