sequencer - make more use of getopts and sort helpApi alphabetically
This commit is contained in:
309
sequencer.sh
309
sequencer.sh
@@ -180,119 +180,6 @@ sequencer.sh global variables:
|
||||
Profile string selected with -p argument
|
||||
|
||||
sequencer.sh build-in functions:
|
||||
USAGE_API
|
||||
echo -e "$(col green) root$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if current user is root.
|
||||
e.g. root || echo "Not root"
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) running$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if same script already runs.
|
||||
e.g. running && exit 1
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) exists [OPTIONS] [ELEMENT]$(col off)"
|
||||
cat <<USAGE_API
|
||||
[ELEMENT]
|
||||
: either a variable- or a funtion name (-f)
|
||||
[OPTIONS]
|
||||
-f : a function
|
||||
-- : end of options
|
||||
USAGE_API
|
||||
echo -e "$(col green) quiet$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs non-interactive (-q|-qq).
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) silent$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs interactive.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) dry$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs in dry-run mode (-d)
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) verbose$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs in verbose mode (-v)
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) contextHelp$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if current output is only for help purposes.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) contextExe$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if current output is in execution mode.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) confirm [OPTIONS] [QUESTION]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Default (empty character) = no
|
||||
Invalid character trigger the default
|
||||
[OPTIONS]
|
||||
-f : interactive even if quiet
|
||||
-n : no input help
|
||||
-y : default = yes
|
||||
-- : end of options
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) ask [OPTION] [QUESTION] [DEFAULT]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Will ask for input even if quiet, when [DEFAULT] is empty
|
||||
[OPTION]
|
||||
-e : allow empty input
|
||||
Ignored if [DEFAULT] is available
|
||||
-s : ask for secret (don't print input)
|
||||
Does not add a newline. Usage:
|
||||
pass=\$(ask -s "Password"); echo
|
||||
-- : End of options
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) exe [COMMANDLINE]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Execute command line without pipes or redirects (>,<,|).
|
||||
Supporting: dry-run (-d): only print command without execution
|
||||
verbose (-v): print command before execution
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) exep \"[COMMAND STRING(s)]\"$(col off)"
|
||||
cat <<USAGE_API
|
||||
See exe, but support for pipes or redirects.
|
||||
e.g.: exep echo hello world \\> \\'out put.log\\'
|
||||
exep echo hello world \\> out\\\\ put.log
|
||||
exep "echo hello world > 'out put.log'"
|
||||
exep "echo hello world > out\\ put.log"
|
||||
Important:
|
||||
- Shell commands cd, read, ... won't work because [COMMAND STRING(s)] is started in a new shell.
|
||||
- All apostrophes need to be esacped since the command line is given as string.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) escpath <PATH>$(col off)"
|
||||
cat <<USAGE_API
|
||||
Escaping non-printable characters with the proposed POSIX $'' syntax
|
||||
e.g. \$(escpath /my own/ho me/path) = $(escpath /my own/ho me/path)
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) initSeqConfig [OPTION] <NAME> [TEMPLATE]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Create a configuration file in ${seq_configRoot}/ and source it if already existent.
|
||||
[OPTION]
|
||||
-p : Use profiles
|
||||
-t : Source config also if created from template
|
||||
-e : Create empty configuration if no template is found
|
||||
Returns
|
||||
0 : sourced configuration or
|
||||
(-t) : created and sourced configuration from template
|
||||
1 : created configuration from template but not sourced
|
||||
2 : created empty configuration
|
||||
3 : No configuration created
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) addConf <OPTIONS> [SOURCE TYPE] <SOURCE> <DESTINATION FILE>$(col off)"
|
||||
cat <<USAGE_API
|
||||
@@ -313,10 +200,16 @@ USAGE_API
|
||||
Target file to be created or modified.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) step <STEP NUMBER OR ALIAS>$(col off)"
|
||||
echo -e "$(col green) ask [OPTION] [QUESTION] [DEFAULT]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Executes a single step also by alias. Useful if step numbers get reorganized.
|
||||
dry-run is not applied in this function! The executed step is responsible.
|
||||
Will ask for input even if quiet, when [DEFAULT] is empty
|
||||
[OPTION]
|
||||
-e : allow empty input
|
||||
Ignored if [DEFAULT] is available
|
||||
-s : ask for secret (don't print input)
|
||||
Does not add a newline. Usage:
|
||||
pass=\$(ask -s "Password"); echo
|
||||
-- : End of options
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) color [FOREGROUND COLOR] [BACKGROUND COLOR]$(col off)"
|
||||
@@ -327,14 +220,30 @@ USAGE_API
|
||||
[COLOR]: black, red, green, yellow, blue, magenta, cyan, white
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) info|warning|error|debug [OPTIONS] [STRING]$(col off)"
|
||||
echo -e "$(col green) confirm [OPTIONS] [QUESTION]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Output [STRING] (can be multiline) to stdout according to log level.
|
||||
[OPTIONS]
|
||||
-a : Append [STRING] skipping leading " [ ] " prefix
|
||||
-d : No leading " [ ] " prefix
|
||||
-e : Output to stderr
|
||||
-n : No newline
|
||||
Default (empty character) = no
|
||||
Invalid character trigger the default
|
||||
[OPTIONS]
|
||||
-f : interactive even if quiet
|
||||
-n : no input help
|
||||
-y : default = yes
|
||||
-- : end of options
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) contextExe$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if current output is in execution mode.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) contextHelp$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if current output is only for help purposes.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) dry$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs in dry-run mode (-d)
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) echoinfo [...]$(col off)"
|
||||
@@ -357,16 +266,6 @@ USAGE_API
|
||||
<VARIABLENAME> : Name without \$
|
||||
[DESCRIPTION] : Additional text for error output
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) saveReturn [ERRORCODE]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Save ERRORCODE if it is != 0 for later use with endReturn
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) getReturn$(col off)"
|
||||
cat <<USAGE_API
|
||||
Return last saved error code
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) endReturn [OPTIONS] [MESSAGE]$(col off)"
|
||||
cat <<USAGE_API
|
||||
@@ -379,6 +278,107 @@ USAGE_API
|
||||
[MESSAGE]
|
||||
String which is displayed in the error output
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) escpath <PATH>$(col off)"
|
||||
cat <<USAGE_API
|
||||
Escaping non-printable characters with the proposed POSIX $'' syntax
|
||||
e.g. \$(escpath /my own/ho me/path) = $(escpath /my own/ho me/path)
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) exe [COMMANDLINE]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Execute command line without pipes or redirects (>,<,|).
|
||||
Supporting: dry-run (-d): only print command without execution
|
||||
verbose (-v): print command before execution
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) exep \"[COMMAND STRING(s)]\"$(col off)"
|
||||
cat <<USAGE_API
|
||||
See exe, but support for pipes or redirects.
|
||||
e.g.: exep echo hello world \\> \\'out put.log\\'
|
||||
exep echo hello world \\> out\\\\ put.log
|
||||
exep "echo hello world > 'out put.log'"
|
||||
exep "echo hello world > out\\ put.log"
|
||||
Important:
|
||||
- Shell commands cd, read, ... won't work because [COMMAND STRING(s)] is started in a new shell.
|
||||
- All apostrophes need to be esacped since the command line is given as string.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) exists [OPTIONS] [ELEMENT]$(col off)"
|
||||
cat <<USAGE_API
|
||||
[ELEMENT]
|
||||
: either a variable- or a funtion name (-f)
|
||||
[OPTIONS]
|
||||
-f : a function
|
||||
-- : end of options
|
||||
USAGE_API
|
||||
echo -e "$(col green) getReturn$(col off)"
|
||||
cat <<USAGE_API
|
||||
Return last saved error code (see saveReturn)
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) info|warning|error|debug [OPTIONS] [STRING]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Output [STRING] (can be multiline) to stdout according to log level.
|
||||
[OPTIONS]
|
||||
-a : Append [STRING] skipping leading " [ ] " prefix
|
||||
-d : No leading " [ ] " prefix
|
||||
-e : Output to stderr
|
||||
-n : No newline
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) initSeqConfig [OPTION] <NAME> [TEMPLATE]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Create a configuration file in ${seq_configRoot}/ and source it if already existent.
|
||||
[OPTION]
|
||||
-p : Use profiles
|
||||
-t : Source config also if created from template
|
||||
-e : Create empty configuration if no template is found
|
||||
Returns
|
||||
0 : sourced configuration or
|
||||
(-t) : created and sourced configuration from template
|
||||
1 : created configuration from template but not sourced
|
||||
2 : created empty configuration
|
||||
3 : No configuration created
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) quiet$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs non-interactive (-q|-qq).
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) root$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if current user is root.
|
||||
e.g. root || echo "Not root"
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) running$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if same script already runs.
|
||||
e.g. running && exit 1
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) saveReturn [ERRORCODE]$(col off)"
|
||||
cat <<USAGE_API
|
||||
Save ERRORCODE if it is != 0 for later use with endReturn
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) silent$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs interactive.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) step <STEP NUMBER OR ALIAS>$(col off)"
|
||||
cat <<USAGE_API
|
||||
Executes a single step also by alias. Useful if step numbers get reorganized.
|
||||
dry-run is not applied in this function! The executed step is responsible.
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) verbose$(col off)"
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs in verbose mode (-v)
|
||||
|
||||
USAGE_API
|
||||
}
|
||||
|
||||
@@ -661,23 +661,21 @@ confirm() {
|
||||
local inputHelp='[y/N] ' # default no
|
||||
local noHelp=0
|
||||
local force=0
|
||||
local arg
|
||||
|
||||
for _ in "${@}" ; do
|
||||
case "${1:-}" in
|
||||
--)
|
||||
shift && break ;;
|
||||
-f)
|
||||
force=1
|
||||
shift ;;
|
||||
-n)
|
||||
noHelp=1
|
||||
shift ;;
|
||||
-y)
|
||||
while getopts "fny" arg; do
|
||||
case "${arg}" in
|
||||
f) force=1 ;;
|
||||
n) noHelp=1 ;;
|
||||
y)
|
||||
rexReply='^[^Nn]*$' # default yes
|
||||
inputHelp='[Y/n] ' # default yes
|
||||
shift ;;
|
||||
;;
|
||||
*) ;;
|
||||
esac
|
||||
done
|
||||
shift "$((OPTIND-1))"; OPTIND=1
|
||||
|
||||
(( noHelp )) && inputHelp=
|
||||
if interactive || (( force )) ; then
|
||||
read -r -p "${1:-} ${inputHelp}" -n 1
|
||||
@@ -705,18 +703,17 @@ ask() {
|
||||
sqr::debugPause
|
||||
local hidden=
|
||||
local empty=0
|
||||
for _ in "$@" ; do
|
||||
case "${1:-}" in
|
||||
--)
|
||||
shift && break ;;
|
||||
-e)
|
||||
empty=1
|
||||
shift ;;
|
||||
-s)
|
||||
hidden="-s"
|
||||
shift ;;
|
||||
local arg
|
||||
|
||||
while getopts "fny" arg; do
|
||||
case "${arg}" in
|
||||
-e) empty=1 ;;
|
||||
-s) hidden="-s" ;;
|
||||
*) ;;
|
||||
esac
|
||||
done
|
||||
shift "$((OPTIND-1))"; OPTIND=1
|
||||
|
||||
local answer=
|
||||
if [[ -n "${2:-}" ]] ; then
|
||||
! interactive && printf '%s\n' "${2}" && sqr::debugContinue && return 0
|
||||
@@ -1638,7 +1635,7 @@ sqr::main() {
|
||||
echo " - Printed commands may not be accurate (e.g. quotation incorrect)"
|
||||
echo " - Sequence may ignore dry run"
|
||||
color none
|
||||
confirm -f -n -y "Press enter to continue or Ctrl + C to abort"
|
||||
confirm -fny "Press enter to continue or Ctrl + C to abort"
|
||||
fi
|
||||
|
||||
# Determine system default editor
|
||||
|
Reference in New Issue
Block a user