MIAOU-BASH is a collection of settings and helpers for leveraging BASH. Developer-friendly, it may be used as solo package with or without the miaou project.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.4 KiB

#!/usr/bin/env bash
# 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
function is_true {
[[ "${1:-}" == 'true' ]]
}
# FIXME: define an alias to bypass ARG evaluation when unnecessary!
# example:
# alias toto="bash -c \"[[ \\\$MIAOU_DEBUG == 'true' ]] && echo yes \\\$* || echo nope\""
function miaou_debug {
is_true "${MIAOU_DEBUG}" && >/dev/tty builtin echo -e "\e[90mDEBUG <==> \e[33m$*\e[0m" || true
}
function tty_ansi {
fg="${1:-}"
[[ -n $fg ]] && >/dev/tty echo -ne "\e[${fg}m" || true
}
function on_error {
>/dev/tty printf '\r' # force carriage return explicitly
code="${1:-100}"
# miaou_debug "on_error: $code ($$ -- $BASHPID)"
if [[ $$ == "$BASHPID" ]]; then
>&2 echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: command_error: ${code}"
failure_type="COMMAND_ERROR"
exit "$code"
else
>&2 echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: subshell_error: ${code}"
kill -PIPE $$
fi
}
function dump_stderr {
miaou_debug --stderr--
mapfile -t tmp_error <"$MIAOU_BASH_ERROR"
tty_ansi 94
for i in "${tmp_error[@]}"; do echo "$i"; done
tty_ansi 0
miaou_debug --end of stderr--
}
function dump_failure {
miaou_debug --error box--
tty_ansi 94
echo -n "FAILURE TYPE = "
tty_ansi 95
echo "$failure_type"
tty_ansi 0
miaou_debug --end of error box--
}
function dump_stacktrace {
miaou_debug --stacktrace--
tty_ansi 94
echo TODO: stacktrace...
tty_ansi 0
miaou_debug --end of stacktrace--
}
function on_exit {
code="${1:-0}"
failure_type="${failure_type:-UNDEFINED}"
2>&3 >&3 dump_stderr
2>&3 >&3 dump_failure
2>&3 >&3 dump_stacktrace
miaou_debug "on_exit $code $failure_type"
}
function on_pipe {
local code=141 # default code error for pipe
local tmp_error regex
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]} && failure_type="SUBSHELL_ERROR"
# miaou_debug on_pipe: "${code}"
exit "$code"
}
## main
set -euo pipefail
set -E # HALT on subshell error
# set -T # DEBUG RETURN
trap 'on_error $?' ERR
trap 'on_exit $?' EXIT
trap 'on_pipe' PIPE
echo "--SOURCE scenario ${BASHPID}"
exec 3>/dev/stderr
2>"$MIAOU_BASH_ERROR" source ./test/stub/scenario.bash subshell_term
exec 3>-
echo "--SUCCESS scenario"