poc miaou_debug
This commit is contained in:
@@ -2,11 +2,23 @@
|
|||||||
|
|
||||||
# constants
|
# constants
|
||||||
|
|
||||||
|
MIAOU_BASH_ERROR=$(mktemp -t "$(basename "$0").XXXXXXXX" --tmpdir="/run/user/$(id -u)")
|
||||||
|
MIAOU_DEBUG=${MIAOU_DEBUG:-false}
|
||||||
|
readonly MIAOU_BASH_ERROR MIAOU_DEBUG
|
||||||
|
|
||||||
## functions
|
## functions
|
||||||
|
|
||||||
|
function is_true {
|
||||||
|
[[ "${1:-}" == 'true' ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
function miaou_debug {
|
||||||
|
is_true "${MIAOU_DEBUG}" && >/dev/tty builtin echo -e "\e[90mDEBUG <==> \e[95m$*\e[0m" || true
|
||||||
|
}
|
||||||
|
|
||||||
function on_error {
|
function on_error {
|
||||||
code="${1:-0}"
|
code="${1:-100}"
|
||||||
>&2 echo on_error: $$ $BASHPID
|
miaou_debug on_error: $$ $BASHPID
|
||||||
if [[ $$ == "$BASHPID" ]]; then
|
if [[ $$ == "$BASHPID" ]]; then
|
||||||
>&2 echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: command_error: ${code}"
|
>&2 echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: command_error: ${code}"
|
||||||
exit "$code"
|
exit "$code"
|
||||||
@@ -14,35 +26,44 @@ function on_error {
|
|||||||
>&2 echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: subshell_error: ${code}"
|
>&2 echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: subshell_error: ${code}"
|
||||||
kill -PIPE $$
|
kill -PIPE $$
|
||||||
fi
|
fi
|
||||||
# kill -INT $BASHPID
|
|
||||||
# kill -INT $$
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function on_exit {
|
function on_exit {
|
||||||
code="${1:-0}"
|
code="${1:-0}"
|
||||||
echo "on_exit $code"
|
|
||||||
|
miaou_debug --stderr--
|
||||||
|
mapfile -t tmp_error <"$MIAOU_BASH_ERROR"
|
||||||
|
for i in "${tmp_error[@]}"; do >&4 echo -e "$i"; done
|
||||||
|
last_error="${tmp_error[-1]}"
|
||||||
|
miaou_debug --end of stderr--
|
||||||
|
|
||||||
|
miaou_debug "on_exit $code"
|
||||||
}
|
}
|
||||||
|
|
||||||
function on_pipe {
|
function on_pipe {
|
||||||
>&2 echo on_pipe: $$ $BASHPID
|
local code=141 # default code error for pipe
|
||||||
#TODO: compute code from latest sterr in case of command_error
|
local tmp_error regex
|
||||||
code="${1:-24}" # 24 => CTRL-C
|
|
||||||
|
|
||||||
trap - EXIT ERR PIPE
|
mapfile -t tmp_error <"$MIAOU_BASH_ERROR"
|
||||||
|
last_error="${tmp_error[-1]}"
|
||||||
|
regex='.*: line [0-9]+: subshell_error: ([0-9]+)'
|
||||||
|
[[ $last_error =~ $regex ]] && code=${BASH_REMATCH[1]}
|
||||||
|
|
||||||
|
miaou_debug on_pipe: "${code}"
|
||||||
exit "$code"
|
exit "$code"
|
||||||
}
|
}
|
||||||
|
|
||||||
## main
|
## main
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
set -T # DEBUG RETURN
|
set -E # HALT on subshell error
|
||||||
set -E # ERR on subshell
|
# set -T # DEBUG RETURN
|
||||||
# set -b
|
|
||||||
|
|
||||||
trap 'on_error $?' ERR
|
trap 'on_error $?' ERR
|
||||||
trap 'on_exit $?' EXIT
|
trap 'on_exit $?' EXIT
|
||||||
trap 'on_pipe' PIPE
|
trap 'on_pipe' PIPE
|
||||||
|
|
||||||
echo "${BASHPID}"
|
echo "--SOURCE scenario ${BASHPID}"
|
||||||
|
exec 3> >(tee "$MIAOU_BASH_ERROR" >/dev/null) 4>/dev/stderr
|
||||||
source ./test/stub/scenario.bash subshell_term
|
source ./test/stub/scenario.bash subshell_term 2>&3
|
||||||
|
echo "--SUCCESS scenario"
|
||||||
|
|||||||
@@ -14,9 +14,11 @@ function do_subshell_false {
|
|||||||
|
|
||||||
function do_subshell_term {
|
function do_subshell_term {
|
||||||
|
|
||||||
# grep none none
|
echo toto
|
||||||
|
# echo 'toto' | grep none
|
||||||
|
|
||||||
echo "found1=$(grep nope nope)"
|
echo -n "found1=$(grep nope nope)"
|
||||||
|
echo "found1=$(grep nope nope | grep none)"
|
||||||
echo "found2=$(command_not_found)"
|
echo "found2=$(command_not_found)"
|
||||||
echo SHOULD NOT SHOW UP!
|
echo SHOULD NOT SHOW UP!
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user