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