New function "configuration profiles" for sequencer (initSeqConfig)

Changed usage definition to "cat" instead of echo
This commit is contained in:
2021-01-31 17:33:03 +01:00
parent 370a042c8b
commit 4c7eae13c4

View File

@@ -7,9 +7,9 @@
## Version information
VERSION_REV=11
VERSION_MAJOR=1
VERSION_MINOR=5
VERSION_REV=12
VERSION_MAJOR=0
VERSION_MINOR=0
## Start of generic script part
@@ -25,143 +25,151 @@ ALIAS=
SEQ_CONFIG_NAME=".seqs"
SEQ_CONFIG_HOME="$HOME/$SEQ_CONFIG_NAME"
SEQ_CONFIG_FILE=
SEQ_PROFILE_NAME=default
TEMPLATE_NAME=seqTemplateExample.sh
MISSING_CONF=missingConf.log
VERSION_STRING="${VERSION_REV}.${VERSION_MAJOR}.${VERSION_MINOR}"
helpSequencer() {
echo "Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS] [STEP ARGUMENTS]"
echo
echo " [OPTIONS]"
echo " --dry-run, -d : Only print to console what would be done"
echo " ! Attention - Sequence must support this"
echo " --help, -h : Display help"
echo " --helpapi, -ha : Display help about build-in supporting functions"
echo " (e.g. exe,addconf,echerr,...)"
echo " --quiet, -q : Don't ask for permission to execute steps"
echo " If called without starting step number, only this help is shown"
echo " -qq : Same as --quiet but suppresses regular sequencer.sh output"
echo " --single, -s : Execute only one step"
echo " If more than one step is requested, only the first will be executed"
echo " --verbose, -v : Verbose output (use exe() function to call shell commands in seqs)"
echo " ( e.g.: exe apt update )"
echo " --version : Display version of sequencer and revision of sequence"
echo
echo " [STEP NUMBER\"(s)\" 1-${MAX_STEP} or ALIAS]"
echo " No STEP or ALIAS : assume 1 as starting point"
echo " Single STEP or ALIAS : starting point of sequential process"
echo " Multiple STEPS or ALIAS : execute only given steps"
echo " (e.g. $0 \"2 4 12\")"
echo " multiple steps need to be given as string"
echo " [STEP ARGUMENTS]"
echo " * : Arguments will be passed to selected steps and step infos as:"
echo " \$2 ..."
echo " \$1 is always the step number"
cat <<USAGE_EOF
Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS] [STEP ARGUMENTS]
[OPTIONS]
--dry-run, -d : Only print to console what would be done
! Attention - Sequence must support this
--help, -h : Display help
--helpapi, -ha : Display help about build-in supporting functions
(e.g. exe,addconf,echerr,...)
--profile, -p : Sequence configuration profile name (default: \"default\")
If supported by seq
--quiet, -q : Don't ask for permission to execute steps
If called without starting step number, only this help is shown
-qq : Same as --quiet but suppresses regular sequencer.sh output
--single, -s : Execute only one step
If more than one step is requested, only the first will be executed
--verbose, -v : Verbose output (use exe() function to call shell commands in seqs)
( e.g.: exe apt update )
--version : Display version of sequencer and revision of sequence
[STEP NUMBER\"(s)\" 1-${MAX_STEP} or ALIAS]
No STEP or ALIAS : assume 1 as starting point
Single STEP or ALIAS : starting point of sequential process
Multiple STEPS or ALIAS : execute only given steps
(e.g. $0 \"2 4 12\")
multiple steps need to be given as string
[STEP ARGUMENTS]
* : Arguments will be passed to selected steps and step infos as:
\$2 ...
\$1 is always the step number
USAGE_EOF
}
helpApi() {
echo "sequencer.sh API"
echo
echo "The sequencer.sh build-in functions are available in all sequence functions:"
echo "- step_config"
echo " If optional step_config is defined in the sequence, it will be called once before any step."
echo "- step_[1-${MAX_STEP}]_info"
echo "- step_[1-${MAX_STEP}]_alias"
echo "- step_[1-${MAX_STEP}]"
echo
echo "sequencer.sh global variables:"
echo
echo " \$QUIET"
echo " 0 : default"
echo " 1 (-q) : No user interaction (e.g. question to start a step)"
echo " 2 (-qq) : 1 and no regular output of sequencer.sh"
echo " \$DRY"
echo " 0 : default"
echo " 1 (-d) : Commands shall only be printed but not executed"
echo " \$VERBOSE"
echo " 0 : default"
echo " 1 : Print full command from exe() or exep() before executing"
echo " \$SEQ_CONFIG_HOME"
echo " Path to user specific seq configuration directory"
echo " \$SEQ_CONFIG_FILE"
echo " Path to user specific seq configuration file"
echo " Will be empty if unused"
echo
echo "sequencer.sh build-in functions:"
echo
echo " exe [COMMANDLINE]"
echo " Execute command line without pipes or redirects (>,<,|)."
echo " Supporting: dry-run (-d): only print command without execution"
echo " verbose (-v): print command before execution"
echo
echo " exep \"[COMMANDLINE]\""
echo " See exe, but support for pipes or redirects."
echo " Important:"
echo " - Shell commands cd, read, ... won't work because COMMANDLINE is started in a new shell."
echo " - All apostrophes need to be esacped since the command line is given as string."
echo
echo " initSeqConfig [OPTION] <NAME> [TEMPLATE]"
echo " Create a configuration file in $SEQ_CONFIG_HOME/ and source it if already existent."
echo " [OPTION]"
echo " -t : Source config also if created from template"
echo " -e : Create empty configuration if no template is found"
echo " Returns"
echo " 0 : sourced configuration or"
echo " (-t) : created and sourced configuration from template"
echo " 1 : created configuration from template but not sourced"
echo " 2 : created empty configuration"
echo " 3 : No configuration created"
echo
echo " addConf <OPTIONS> [SOURCE TYPE] <SOURCE> <DESTINATION FILE>"
echo " Trying to write or append text or a file (<SOURCE>) to a destination file."
echo " If the CONFIGFILE exists, a backup (name_%Y%m%d-%H%M%S.bck) is saved at the same location."
echo " If -s fails or -m, \"$(realpath "$MISSING_CONF")\" is created with the conflicts"
echo " to be resolved by the user."
echo " <OPTIONS>"
echo " -c : create a new file"
echo " -a : append to existing file"
echo " -s : skip if CONFIGFILE exists (no backup and entry in missing conf)"
echo " -m : only add content to missing conf and warn user"
echo " [SOURCE TYPE]"
echo " -f : <SOURCE> is a file"
echo " <SOURCE>"
echo " Text or file (-f) to create or added to <DESTINATION FILE>"
echo " <DESTINATION FILE>"
echo " Target file to be created or modified."
echo
echo " step <STEP NUMBER OR ALIAS>"
echo " Executes a single step also by alias. Useful if step numbers get reorganized."
echo " dry-run is not applied in this function! The executed step is responsible."
echo
echo " echoerr [...]"
echo " echo to stderr"
echo " [...] : all parameter are forwarded to echo"
echo
echo " echoinfo [...]"
echo " echo additional correctly indented line to step info"
echo " [...] : all parrameter are forwared to echo"
echo
echo " endCheckEmpty <VARIABLENAME> [DESCRIPTION]"
echo " exit 666 if variable is empty"
echo " <VARIABLENAME> : Name used within eval"
echo " [DESCRIPTION] : Additional text for error output"
echo
echo " saveReturn [ERRORCODE]"
echo " Save ERRORCODE if it is != 0 for later use with endReturn"
echo
echo " getReturn"
echo " Return last saved error code"
echo
echo " endReturn [OPTIONS] [MESSAGE]"
echo " Notifys user that there was an error (previously saved by saveReturn,"
echo " or -o [ERRORCODE]) and asks to continue or end the sequence."
echo " Always exits with evaluated error code."
echo " [OPTIONS]"
echo " -f : force exit without user input, if error code is not 0"
echo " -o ERRORCODE : override stored error code and check ERRORCODE"
echo " [MESSAGE]"
echo " String which is displayed in the error output"
echo
helpApi(){
cat <<USAGE_API
sequencer.sh API
The sequencer.sh build-in functions are available in all sequence functions:
- step_config
If optional step_config is defined in the sequence, it will be called once before any step.
- step_[1-${MAX_STEP}]_info
- step_[1-${MAX_STEP}]_alias
- step_[1-${MAX_STEP}]
sequencer.sh global variables:
\$QUIET
0 : default
1 (-q) : No user interaction (e.g. question to start a step)
2 (-qq) : 1 and no regular output of sequencer.sh
\$DRY
0 : default
1 (-d) : Commands shall only be printed but not executed
\$VERBOSE
0 : default
1 : Print full command from exe() or exep() before executing
\$SEQ_CONFIG_HOME
Path to user specific seq configuration directory
\$SEQ_CONFIG_FILE
Path to user specific seq configuration file
Will be empty if unused
sequencer.sh build-in functions:
exe [COMMANDLINE]
Execute command line without pipes or redirects (>,<,|).
Supporting: dry-run (-d): only print command without execution
verbose (-v): print command before execution
exep \"[COMMANDLINE]\"
See exe, but support for pipes or redirects.
Important:
- Shell commands cd, read, ... won't work because COMMANDLINE is started in a new shell.
- All apostrophes need to be esacped since the command line is given as string.
initSeqConfig [OPTION] <NAME> [TEMPLATE]
Create a configuration file in $SEQ_CONFIG_HOME/ 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
addConf <OPTIONS> [SOURCE TYPE] <SOURCE> <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 -s fails or -m, \"$(realpath "$MISSING_CONF")\" is created with the conflicts
to be resolved by the user.
<OPTIONS>
-c : create a new file
-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 : <SOURCE> is a file
<SOURCE>
Text or file (-f) to create or added to <DESTINATION FILE>
<DESTINATION FILE>
Target file to be created or modified.
step <STEP NUMBER OR ALIAS>
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.
echoerr [...]
echo to stderr
[...] : all parameter are forwarded to echo
echoinfo [...]
echo additional correctly indented line to step info
[...] : all parrameter are forwared to echo
endCheckEmpty <VARIABLENAME> [DESCRIPTION]
exit 666 if variable is empty
<VARIABLENAME> : Name used within eval
[DESCRIPTION] : Additional text for error output
saveReturn [ERRORCODE]
Save ERRORCODE if it is != 0 for later use with endReturn
getReturn
Return last saved error code
endReturn [OPTIONS] [MESSAGE]
Notifys user that there was an error (previously saved by saveReturn,
or -o [ERRORCODE]) and asks to continue or end the sequence.
Always exits with evaluated error code.
[OPTIONS]
-f : force exit without user input, if error code is not 0
-o ERRORCODE : override stored error code and check ERRORCODE
[MESSAGE]
String which is displayed in the error output
USAGE_API
}
# Echo to stderr
@@ -291,6 +299,7 @@ endReturn() {
# Create a configuration file in the users' home.
# Source it if already existent
# [OPTION]
# -p : <NAME> is subfolder used for profiles
# -t : Source config also if created from template
# -e : Create empty configuration if no template is found
# Return
@@ -303,27 +312,36 @@ initSeqConfig() {
local arg
local sourceAlways=0
local createEmpty=0
local seqProfiles=0
for arg in "$@" ; do
case "$1" in
-t)
sourceAlways=1
shift
;;
-e)
createEmpty=1
shift
;;
-p)
seqProfiles=1
shift
;;
-t)
sourceAlways=1
shift
;;
esac
done
local configLoc="$SEQ_CONFIG_HOME/$1"
if [ $seqProfiles -ne 0 ] ; then
configLoc="$SEQ_CONFIG_HOME/$1/${SEQ_PROFILE_NAME}.cfg"
fi
local configDir="$(dirname $configLoc)"
local configTemplate="$2"
# Create config subdir in users home
if [ ! -e "$SEQ_CONFIG_HOME/" ] ; then
echo -n " [I] Creating $(realpath $SEQ_CONFIG_HOME)..."
exe mkdir -p "$SEQ_CONFIG_HOME" && echo "Ok" || echo "Nok"
exe chmod 700 "$SEQ_CONFIG_HOME"
if [ ! -e "$configDir/" ] ; then
echo -n " [I] Creating $(realpath $configDir)..."
exe mkdir -p "$configDir" && echo "Ok" || echo "Nok"
exe chmod 700 "$configDir"
fi
if [ -s "$configLoc" ] ; then
@@ -343,7 +361,7 @@ initSeqConfig() {
endReturn -o $? "Unable to use existing configuration: $configExists"
echoerr " [I] Using existing configuration: $configExists"
echoerr " (Moved to $SEQ_CONFIG_HOME)"
echoerr " (Moved to $configDir)"
. "$configLoc"
return 0
fi
@@ -358,7 +376,7 @@ initSeqConfig() {
return 1
else
echo " [W] Using seq configuration from template $configTemplate"
echo " (Copied to $SEQ_CONFIG_HOME)"
echo " (Copied to $configDir)"
SEQ_CONFIG_FILE="$configLoc"
. "$configLoc"
return 0
@@ -872,6 +890,11 @@ main() {
helpApi
exit 0;
;;
--profile|-p) # seq profile name
shift
SEQ_PROFILE_NAME="$1"
shift
;;
--quiet|-q|-qq) # detect if option quiet is available
if [ "$1" == "-qq" ] ; then
QUIET=2