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() {
echo "sequencer.sh build-in functions"
echo
echo " echoerr <...>"
echo " echo output to stderr"
echo " <...> - all parameter are forwarded to 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 " endCheckEmpty <Variable Name> [Description]"
echo " exit 666 if variable is empty"
echo " <Variable Name> - Name used within eval"
echo " [Description] - Additional text for error output"
echo " exep \"[COMMANDLINE]\""
echo " See exe, but support for pipes or redirects."
echo " Since the command line is given as string all apostrophes need to be esacped."
echo
echo " saveReturn [Error Code]"
echo " Save error code if it is != 0 for later use with endReturn"
echo
echo " endReturn [-f] [Message]"
echo " Notifys user that there was an error (previously saved by saveReturn)"
echo " and asks to continue or end sequence. Always exits with saved error code."
echo " -f : force exit with the last saved error code without user input"
echo " addConf <OPTIONS> <CONFIGTEXT> <CONFIGFILE>"
echo " Trying to write or append to a 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 " <CONFIGTEXT>"
echo " Text to be created or added to <CONFIGFILE>"
echo " <CONFIGFILE>"
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 " 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
@@ -116,19 +145,31 @@ getReturn() {
return $ERNO
}
# endReturn [-f] [MESSAGE]
# endReturn [-f] [-o ERRORCODE] [MESSAGE]
# -f : force exit with $ERNO without user input
# -o : override and check given error code
# MESSAGE : Custom error message
#
endReturn() {
local forceExit=0
local errorCode=$ERNO
local endMessage=""
for arg in "$@" ; do
case "$1" in
-f)
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
;;
"")
@@ -141,32 +182,34 @@ endReturn() {
esac
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
if [ "$endMessage" != "" ]; then
echoerr -e " [E] $endMessage\n Sequence stopped"
else
echoerr -e " [E] Return value $ERNO detected.\n Sequence stopped"
echoerr -e " [E] Return value $errorCode detected.\n Sequence stopped"
fi
exit $ERNO
exit $errorCode
fi
if [ $ERNO -ne 0 ] ; then
if [ $errorCode -ne 0 ] ; then
echo
if [ "$endMessage" != "" ]; then
echoerr -e " [W] $endMessage"
else
echoerr " [W] Return value $ERNO detected."
echoerr " [W] Return value $errorCode detected."
fi
read -p "End sequence: [y]/n? " answer
case $answer in
[nN])
# reset saved error code if user chooses to continue
ERNO=0
echo
echo " [I] Continuing sequence..."
;;
*)
echo
echoerr " [E] Sequence stopped"
exit $ERNO;
exit $errorCode;
;;
esac
fi
@@ -192,7 +235,7 @@ addConf() {
confMode="-m"
;;
*) # default
echo "Parameter 1 (-a|-c|-m|-s) missing for addConf()"
echoerr " [E] Parameter 1 (-a|-c|-m|-s) missing for addConf()"
exit 0;
;;
esac
@@ -231,14 +274,15 @@ addConf() {
else
echo "$2" >> "$3"
fi
echo -e "ok \n [W] Existing config saved to ${addConfBackup}"
echo -e "ok \n [I] Existing config saved to ${addConfBackup}"
return 0
else
echo "nok (backup exists)"
echo "nok"
echoerr " [W] backup exists"
fi
fi
else
echo "nok (no change requested)"
echo -e "ok \n [I] no change requested"
fi
# add configuration to missingConf file
@@ -270,7 +314,7 @@ execute() {
# check if step function exists
declare -F step_$1 &>>/dev/null || NOTFOUND=1
if [ $NOTFOUND -eq 1 ] && [ $NOREPORT -ne 1 ] ; then
echo "Step $1 not found"
echoerr " [E] Step $1 not found"
exit 1;
fi