sequencer - Attempt to reduce line amount without loosing readability

This commit is contained in:
2022-12-11 00:34:43 +01:00
parent aeef3e89cd
commit ba4eae5ef2

View File

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