sequencer - endReturn and saveReturn can read last return value ($?)

Reset return value before calling a step function
This commit is contained in:
2023-01-03 15:22:31 +01:00
parent 58bb04c672
commit b9f8ee7599

View File

@@ -68,7 +68,7 @@ set -o pipefail
_sqr_debug=0 _sqr_debug=0
_sqr_dry=0 _sqr_dry=0
_sqr_editor= _sqr_editor=
_sqr_errno=0 _sqr_errno=
_sqr_interactive=1 _sqr_interactive=1
_sqr_single=0 _sqr_single=0
readonly _sqr_stepMax=512 readonly _sqr_stepMax=512
@@ -246,15 +246,7 @@ USAGE_API
echo -e "$(col green) echoinfo [...]$(col off)" echo -e "$(col green) echoinfo [...]$(col off)"
cat <<USAGE_API cat <<USAGE_API
echo additional correctly indented line to step info echo additional correctly indented line to step info
[...] : all parameter are forwared to echo [...] : all parameter are passed to echo
USAGE_API
echo -e "$(col green) echoinfoArgs [...]$(col off)"
cat <<USAGE_API
echo argument description after step number or alias.
This must be called first in the step info function.
Does not add a newline at the end.
[...] : no parameter are forwared to echo
USAGE_API USAGE_API
echo -e "$(col green) endIfEmpty <VARIABLENAME> [DESCRIPTION]$(col off)" echo -e "$(col green) endIfEmpty <VARIABLENAME> [DESCRIPTION]$(col off)"
@@ -266,12 +258,15 @@ USAGE_API
USAGE_API USAGE_API
echo -e "$(col green) endReturn [OPTIONS] [MESSAGE]$(col off)" echo -e "$(col green) endReturn [OPTIONS] [MESSAGE]$(col off)"
cat <<USAGE_API cat <<USAGE_API
Notifys user that there was an error (previously saved by saveReturn, Notifys user that there was an error determined in the following order:
or -o [ERRORCODE]) and asks to continue or end the sequence. 1) -o <ERRORCODE>
2) previously saved by saveReturn
3) \$? not 0
and asks to continue if run interactively or ends the sequence.
Always exits with evaluated error code. Always exits with evaluated error code.
[OPTIONS] [OPTIONS]
-f : force exit without user input, if error code is not 0 -f : force exit without user input, if error code is not 0
-o ERRORCODE : override stored error code and check ERRORCODE -o <ERRORCODE> : override stored error code and check ERRORCODE
[MESSAGE] [MESSAGE]
String which is displayed in the error output String which is displayed in the error output
@@ -371,7 +366,7 @@ USAGE_API
USAGE_API USAGE_API
echo -e "$(col green) silent$(col off)" echo -e "$(col green) silent$(col off)"
cat <<USAGE_API cat <<USAGE_API
Returns true if seq runs interactive. Returns true if seq runs with lowest log level (-qq).
USAGE_API USAGE_API
echo -e "$(col green) step <STEP NUMBER OR ALIAS>$(col off)" echo -e "$(col green) step <STEP NUMBER OR ALIAS>$(col off)"
@@ -740,16 +735,17 @@ rmold() {
done < <(ls ${hideOpt} -rt1 "${target}" | head -$((total - retain))) done < <(ls ${hideOpt} -rt1 "${target}" | head -$((total - retain)))
} }
# saveReturn <ERRNO> # saveReturn [ERRNO]
# Function returns with <ERRNO> in case step wants additional evaluation # Function returns with detected return value in case step wants additional evaluation
saveReturn() { saveReturn() {
(( ${1:-"0"} )) && _sqr_errno=${1} local lerrno=${1:-$?}
return "${_sqr_errno}" (( lerrno )) && _sqr_errno=${lerrno}
return "${_sqr_errno:=${lerrno}}"
} }
# getReturn # getReturn
# Returns latest saved $_sqr_errno # Returns latest saved $_sqr_errno
getReturn() { return "${_sqr_errno}"; } getReturn() { return "${_sqr_errno:-0}"; }
# 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
@@ -757,8 +753,9 @@ getReturn() { return "${_sqr_errno}"; }
# [MESSAGE] : Custom error message # [MESSAGE] : Custom error message
# #
endReturn() { endReturn() {
# Use last return code if nothing was saved before
local errorCode=${_sqr_errno:-$?}
local forceExit=0 local forceExit=0
local errorCode=${_sqr_errno}
local endMessage="" local endMessage=""
for _ in "$@" ; do for _ in "$@" ; do
@@ -766,12 +763,11 @@ endReturn() {
-f) forceExit=1 && shift ;; -f) forceExit=1 && shift ;;
-o) -o)
shift shift
local rex='^[-]*[0-9]+$'
# Check if string is a number or alias # Check if string is a number or alias
if [[ "${1:?}" =~ ${rex} ]] ; then if [[ "${1:?}" =~ ^[0-9]+$ ]] ; then
errorCode="${1:?}" errorCode="${1}"
else else
warning "Ignoring invalid error code: $1" warning "Ignoring invalid error code: ${1}"
fi fi
shift ;; shift ;;
"") break ;; "") break ;;
@@ -803,7 +799,7 @@ endReturn() {
exit "${errorCode}" exit "${errorCode}"
else else
# reset saved error code if user chooses to continue # reset saved error code if user chooses to continue
_sqr_errno=0 _sqr_errno=
sqr::echo sqr::echo
warning "Continuing sequence..." warning "Continuing sequence..."
fi fi
@@ -1395,7 +1391,8 @@ execute() {
exit 1 ;; exit 1 ;;
esac esac
else else
"step_$1" "$1" "${seq_args[@]}" true # reset return value from "if interactive ; then"
"step_${1}" "${1}" "${seq_args[@]}"
_seq_stepReturn="$?" _seq_stepReturn="$?"
fi fi
color none color none