From ba4eae5ef2be5968a58e33c6903edb3ce3136294 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Sun, 11 Dec 2022 00:34:43 +0100 Subject: [PATCH] sequencer - Attempt to reduce line amount without loosing readability --- sequencer.sh | 223 +++++++++++++++++++-------------------------------- 1 file changed, 84 insertions(+), 139 deletions(-) diff --git a/sequencer.sh b/sequencer.sh index 9ef798d..9834949 100755 --- a/sequencer.sh +++ b/sequencer.sh @@ -122,7 +122,7 @@ Usage: ${seq_name} [OPTIONS] [STEP NUMBER(s) or ALIAS] [STEP ARGUMENTS] ! Attention - Sequence must support this --help, -h : Display help --helpapi, -ha : Display help about build-in supporting functions - (e.g. exe,addconf,echerr,...) + (e.g. exe,addConf,error,...) --liststeps, -ls : List all step numbers and alias --profile, -p : Sequence configuration profile name (default: "default") (if supported by sequence) @@ -181,7 +181,7 @@ sequencer.sh global variables: sequencer.sh build-in functions: USAGE_API - echo -e "$(col green) addConf [SOURCE TYPE] $(col off)" + echo -e "$(col green) addConf $(col off)" cat <) to a destination file. If the CONFIGFILE exists, a backup (name_%Y%m%d-%H%M%S.bck) is saved at the same location. @@ -192,7 +192,6 @@ USAGE_API -a : append to existing file -s : skip if CONFIGFILE exists (no backup and entry in missing conf) -m : only add content to missing conf and warn user - [SOURCE TYPE] -f : is a file Text or file (-f) to create or added to @@ -492,46 +491,27 @@ USAGE_API [ ! -t 1 ] && return 0 [ -z "${1:-}" ] && tput sgr0 && return 0 case "${1:-}" in - black) - tput setaf 0 ;; - red) - tput setaf 1 ;; - green) - tput setaf 2 ;; - yellow) - tput setaf 3 ;; - blue) - tput setaf 4 ;; - magenta) - tput setaf 5 ;; - cyan) - tput setaf 6 ;; - white) - tput setaf 7 ;; - none) - tput sgr0 - return 0 ;; - *) - tput setaf "${1:-}" ;; + black) tput setaf 0 ;; + red) tput setaf 1 ;; + green) tput setaf 2 ;; + yellow) tput setaf 3 ;; + blue) tput setaf 4 ;; + magenta) tput setaf 5 ;; + cyan) tput setaf 6 ;; + white) tput setaf 7 ;; + none) tput sgr0 && return 0 ;; + *) tput setaf "${1:-}" ;; esac case "${2:-}" in - black) - tput setab 0 ;; - red) - tput setab 1 ;; - green) - tput setab 2 ;; - yellow) - tput setab 3 ;; - blue) - tput setab 4 ;; - magenta) - tput setab 5 ;; - cyan) - tput setab 6 ;; - white) - tput setab 7 ;; + black) tput setab 0 ;; + red) tput setab 1 ;; + green) tput setab 2 ;; + yellow) tput setab 3 ;; + blue) tput setab 4 ;; + magenta) tput setab 5 ;; + cyan) tput setab 6 ;; + white) tput setab 7 ;; esac } @@ -578,14 +558,6 @@ USAGE_API # trap 'sqr::error_report "${FUNCNAME:-.}" ${LINENO}' ERR } -# check if run as root -root() { - [[ $(id -u) -eq 0 ]] -} -# check if there is another PID other than this one -running() { - pidof -o %PPID -x "${0##*/}">>/dev/null -} # exists [-f] [--] [ELEMENT] # [ELEMENT] # : either a variable name or @@ -595,10 +567,8 @@ exists() { for _ in "$@" ; do case "${1:-}" in - --) - shift && break ;; - -f) - func="${2:-}" + --) shift && break ;; + -f) func="${2:-}" esac done if [[ -n "${func}" ]] ; then @@ -607,43 +577,33 @@ exists() { [[ "${1:-}" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]] && [[ -n "${!1:-}" ]] fi } -# interactive -# Started without -q to have user interactions -interactive() { - (( _sqr_interactive )) -} -# quiet -# Started with -q to use defaults for confirmations -quiet() { - (( ! _sqr_interactive )) -} -# silent -# Log level equals 0 (fatal) -silent() { - [[ $LOG_LEVEL -eq 0 ]] -} -# dry-run + +# check if run as root +root() { [[ $(id -u) -eq 0 ]]; } + +# check if there is another PID other than this one +running() { pidof -o %PPID -x "${0##*/}">>/dev/null; } + +# interactive = Started without -q to have user interactions +interactive() { (( _sqr_interactive )); } + +# quiet = Started with -q to use defaults for confirmations +quiet() { (( ! _sqr_interactive )); } + +# silent = Log level equals 0 (fatal) +silent() { [[ $LOG_LEVEL -eq 0 ]]; } + # Started with --dry-run -dry() { - (( _sqr_dry )) -} -# verbose +dry() { (( _sqr_dry )); } + # Started with --verbose -verbose() { - (( _sqr_verbose )) -} -contextHelp() { - (( _sqr_contextHelp )) -} -contextExe() { - (( _sqr_contextExe )) -} +verbose() { (( _sqr_verbose )); } +contextHelp() { (( _sqr_contextHelp )); } +contextExe() { (( _sqr_contextExe )); } # editor [FILE(s)..] # Starts the detected system text editor -editor() { - exe "${_sqr_editor}" "$@" -} +editor() { exe "${_sqr_editor}" "$@"; } ### interactive # confirm [OPTIONS] [--] [QUESTION] @@ -734,24 +694,18 @@ ask() { } # Escaping non-printable characters with the proposed POSIX $'' syntax -escpath() { - printf "%q" "$*" -} +escpath() { printf "%q" "$*"; } # saveReturn # Function returns with in case step wants additional evaluation saveReturn() { - if [[ "${1:-"0"}" -ne 0 ]] ; then - _sqr_errno=${1} - fi + (( ${1:-"0"} )) && _sqr_errno=${1} return "${_sqr_errno}" } # getReturn # Returns latest saved $_sqr_errno -getReturn() { - return "${_sqr_errno}" -} +getReturn() { return "${_sqr_errno}"; } # endReturn [-f] [-o ERRORCODE] [MESSAGE] # -f : force exit with $_sqr_errno without user input @@ -765,10 +719,7 @@ endReturn() { for _ in "$@" ; do case "${1:-}" in - -f) - forceExit=1 - shift - ;; + -f) forceExit=1 && shift ;; -o) shift local rex='^[-]*[0-9]+$' @@ -778,15 +729,9 @@ endReturn() { else warning "Ignoring invalid error code: $1" fi - shift - ;; - "") - break - ;; - *) - endMessage="$*" - break - ;; + shift ;; + "") break ;; + *) endMessage="$*" && break ;; esac done @@ -839,7 +784,13 @@ endIfEmpty() { fi } -# addConf [FILE_MODE] +addConfMultiError() { + if [ -n "${1}" ] ; then + error "Only one of the options (-a|-c|-m|-s) is allowed for addConf" && exit 1 + fi +} + +# addConf # trying to write a file # if exists, one attempt is made to create bck file of it # if all fails, a log file is created with the conflicts to be resolved by the user @@ -848,30 +799,27 @@ addConf() { local confMode="" local transferCmd="echo" - for _ in "$@" ; do - case "${1:-}" in + while getopts "acfms" ; do + case "${arg}" in -c) # create a new file - confMode="-c" - shift ;; + addConfMultiError "${confMode}" && confMode="-c" ;; -a) # append to existing file - confMode="-a" - shift ;; + addConfMultiError "${confMode}" && confMode="-a" ;; -s) # skip if CONFIGFILE exists - confMode="-s" - shift ;; + addConfMultiError "${confMode}" && confMode="-s" ;; -m) # only add content to missing conf and warn user - confMode="-m" - shift ;; + addConfMultiError "${confMode}" && confMode="-m" ;; -f) # choose if source is a file or text - transferCmd="cat" - shift ;; - *) # default - if [ "$confMode" == "" ] ; then - error "Parameter 1 (-a|-c|-m|-s) missing for addConf()" - exit 0 - fi ;; + transferCmd="cat" ;; + *) ;; esac done + shift "$((OPTIND-1))"; OPTIND=1 + + if [ -z "${confMode}" ] ; then + error "One of the mandatory options (-a|-c|-m|-s) is missing for addConf" + exit 0 + fi local source="${1:-}" local dest="${2:?}" @@ -1214,27 +1162,25 @@ initSeqConfig() { local seqProfiles=0 local configExists= local configDir= - for _ in "$@" ; do - case "${1:-}" in - -e) - createEmpty=1 - shift ;; - -p) - seqProfiles=1 - shift ;; - -t) - sourceAlways=1 - shift ;; + local arg + + while getopts "ept" arg ; do + case "${arg}" in + -e) createEmpty=1 ;; + -p) seqProfiles=1 ;; + -t) sourceAlways=1 ;; + *) ;; esac done + shift "$((OPTIND-1))"; OPTIND=1 - local configLoc="$seq_configRoot/$1" + local configLoc="$seq_configRoot/${1:-}" if [[ $seqProfiles -ne 0 ]] ; then [ -z "$seq_profileName" ] && seq_profileName=default - configLoc="$seq_configRoot/$1/${seq_profileName}.cfg" + configLoc="$seq_configRoot/${1:-}/${seq_profileName}.cfg" fi configDir="$(dirname -- "$configLoc")" - local configTemplate="$2" + local configTemplate="${2:-}" # Don't create anything if only profiles should be listed if [ -n "${_seq_profileList}" ] ; then @@ -1340,7 +1286,6 @@ execute() { fi # check if step function exists - #declare -F "step_${1:?}" &>>/dev/null || notFound=1 exists -f "step_${1:?}" || notFound=1 if [ ${notFound} -eq 1 ] && [ ${noReport} -ne 1 ] ; then error "Step ${1:-"-"} not found"