New option -o for endReturn to directly evaluate an error code

Addition to api help
This commit is contained in:
2019-12-09 14:45:06 +01:00
parent bf51d0ee11
commit f514b73322

View File

@@ -54,27 +54,56 @@ helpSequencer() {
helpApi() { helpApi() {
echo "sequencer.sh build-in functions" echo "sequencer.sh build-in functions"
echo echo
echo " echoerr <...>" echo " exe [COMMANDLINE]"
echo " echo output to stderr" echo " Execute command line without pipes or redirects (>,<,|)."
echo " <...> - all parameter are forwarded to echo" echo " Supporting: dry-run (-d): only print command without execution"
echo " verbose (-v): print command before execution"
echo echo
echo " endCheckEmpty <Variable Name> [Description]" echo " exep \"[COMMANDLINE]\""
echo " exit 666 if variable is empty" echo " See exe, but support for pipes or redirects."
echo " <Variable Name> - Name used within eval" echo " Since the command line is given as string all apostrophes need to be esacped."
echo " [Description] - Additional text for error output"
echo echo
echo " saveReturn [Error Code]" echo " addConf <OPTIONS> <CONFIGTEXT> <CONFIGFILE>"
echo " Save error code if it is != 0 for later use with endReturn" echo " Trying to write or append to a file."
echo echo " If the CONFIGFILE exists, a backup (name_%Y%m%d-%H%M%S.bck) is saved at the same location."
echo " endReturn [-f] [Message]" echo " If -s fails or -m, \"$(realpath "$MISSING_CONF")\" is created with the conflicts"
echo " Notifys user that there was an error (previously saved by saveReturn)" echo " to be resolved by the user."
echo " and asks to continue or end sequence. Always exits with saved error code." echo " <OPTIONS>"
echo " -f : force exit with the last saved error code without user input" 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 " <CONFIGTEXT>"
echo " Text to be created or added to <CONFIGFILE>"
echo " <CONFIGFILE>"
echo " Target file to be created or modified."
echo echo
echo " step <Step number or alias>" echo " step <Step number or alias>"
echo " Executes a single step also by alias. Useful if step numbers get reorganized." 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 " dry-run is not applied in this function! The executed step is responsible."
echo echo
echo " echoerr [...]"
echo " echo to stderr"
echo " [...] : all parameter are forwarded 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 [-f] [-o ERRORCODE] [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 " -f : force exit without user input, if error code is not 0"
echo " -o : override stored error code and check ERRORCODE"
echo
} }
# Echo to stderr # Echo to stderr
@@ -116,19 +145,31 @@ getReturn() {
return $ERNO return $ERNO
} }
# endReturn [-f] [MESSAGE] # endReturn [-f] [-o ERRORCODE] [MESSAGE]
# -f : force exit with $ERNO without user input # -f : force exit with $ERNO without user input
# -o : override and check given error code
# MESSAGE : Custom error message # MESSAGE : Custom error message
# #
endReturn() { endReturn() {
local forceExit=0 local forceExit=0
local errorCode=$ERNO
local endMessage="" local endMessage=""
for arg in "$@" ; do for arg in "$@" ; do
case "$1" in case "$1" in
-f) -f)
forceExit=1 forceExit=1
skipStep=0 shift
;;
-o)
shift
local rex='^[-]*[0-9]+$'
# Check if string is a number or alias
if [[ "$1" =~ $rex ]] ; then
errorCode=$1
else
echoerr " [W] Ignoring invalid error code: $1"
fi
shift shift
;; ;;
"") "")
@@ -141,32 +182,34 @@ endReturn() {
esac esac
done done
if [[ ( $ERNO -ne 0 && $QUIET -ne 0 ) || ( $ERNO -ne 0 && $forceExit -ne 0 ) ]] ; then if [[ ( $errorCode -ne 0 && $QUIET -ne 0 ) || ( $errorCode -ne 0 && $forceExit -ne 0 ) ]] ; then
echo echo
if [ "$endMessage" != "" ]; then if [ "$endMessage" != "" ]; then
echoerr -e " [E] $endMessage\n Sequence stopped" echoerr -e " [E] $endMessage\n Sequence stopped"
else else
echoerr -e " [E] Return value $ERNO detected.\n Sequence stopped" echoerr -e " [E] Return value $errorCode detected.\n Sequence stopped"
fi fi
exit $ERNO exit $errorCode
fi fi
if [ $ERNO -ne 0 ] ; then if [ $errorCode -ne 0 ] ; then
echo echo
if [ "$endMessage" != "" ]; then if [ "$endMessage" != "" ]; then
echoerr -e " [W] $endMessage" echoerr -e " [W] $endMessage"
else else
echoerr " [W] Return value $ERNO detected." echoerr " [W] Return value $errorCode detected."
fi fi
read -p "End sequence: [y]/n? " answer read -p "End sequence: [y]/n? " answer
case $answer in case $answer in
[nN]) [nN])
# reset saved error code if user chooses to continue
ERNO=0
echo echo
echo " [I] Continuing sequence..." echo " [I] Continuing sequence..."
;; ;;
*) *)
echo echo
echoerr " [E] Sequence stopped" echoerr " [E] Sequence stopped"
exit $ERNO; exit $errorCode;
;; ;;
esac esac
fi fi
@@ -192,7 +235,7 @@ addConf() {
confMode="-m" confMode="-m"
;; ;;
*) # default *) # default
echo "Parameter 1 (-a|-c|-m|-s) missing for addConf()" echoerr " [E] Parameter 1 (-a|-c|-m|-s) missing for addConf()"
exit 0; exit 0;
;; ;;
esac esac
@@ -231,14 +274,15 @@ addConf() {
else else
echo "$2" >> "$3" echo "$2" >> "$3"
fi fi
echo -e "ok \n [W] Existing config saved to ${addConfBackup}" echo -e "ok \n [I] Existing config saved to ${addConfBackup}"
return 0 return 0
else else
echo "nok (backup exists)" echo "nok"
echoerr " [W] backup exists"
fi fi
fi fi
else else
echo "nok (no change requested)" echo -e "ok \n [I] no change requested"
fi fi
# add configuration to missingConf file # add configuration to missingConf file
@@ -270,7 +314,7 @@ execute() {
# check if step function exists # check if step function exists
declare -F step_$1 &>>/dev/null || NOTFOUND=1 declare -F step_$1 &>>/dev/null || NOTFOUND=1
if [ $NOTFOUND -eq 1 ] && [ $NOREPORT -ne 1 ] ; then if [ $NOTFOUND -eq 1 ] && [ $NOREPORT -ne 1 ] ; then
echo "Step $1 not found" echoerr " [E] Step $1 not found"
exit 1; exit 1;
fi fi