Browse Source

refactored1

main
pvincent 1 week ago
parent
commit
dc3371f388
  1. 29
      bin/bash-back
  2. 13
      useless.sh

29
bin/bash-back

@ -1,9 +1,8 @@
#!/usr/bin/env bash
# CONSTANTS
TMP_ERROR=$(mktemp -t "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u))
DEBUG=${BASH_BACK_DEBUG:-false}
SCRIPT=$1
BASH_BACK_ERROR=$(mktemp -t "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u))
BASH_BACK_DEBUG=${BASH_BACK_DEBUG:-false}
# FUNCTIONS
@ -54,7 +53,7 @@ function on_exit {
stack_summary=()
if [[ $code -gt 0 ]]; then
last_error=$(tail -n1 $TMP_ERROR)
last_error=$(tail -n1 $BASH_BACK_ERROR)
last_source=${stack_sources[0]}
last_source=${last_source//./"\."} # replace . by \.
regex1="^$last_source: line ([0-9]+): (.*)\$"
@ -97,7 +96,7 @@ function on_exit {
}
function dump_summary {
$DEBUG && >/dev/tty echo '---stacktrace---'
$BASH_BACK_DEBUG && >/dev/tty echo '---stacktrace---'
for i in "${stack_summary[@]}"; do
>&4 echo -e "$i"
done
@ -105,13 +104,10 @@ function dump_summary {
function dump_stderr {
last_trim=${1:-false}
mapfile -t tmp_error <$TMP_ERROR
if $last_trim; then
unset tmp_error[-1]
tmp_error=("${tmp_error[@]}")
fi
mapfile -t tmp_error <$BASH_BACK_ERROR
$last_trim && unset tmp_error[-1] && tmp_error=("${tmp_error[@]}")
if [[ "${#tmp_error[@]}" -gt 0 ]]; then
$DEBUG && >/dev/tty echo '---stderr---'
$BASH_BACK_DEBUG && >/dev/tty echo '---stderr---'
for i in "${tmp_error[@]}"; do
echo -e "$i"
done
@ -126,17 +122,20 @@ function success {
function cleanup {
trap - ERR TERM INT EXIT
exec 3>&- 4>&-
rm $TMP_ERROR
rm $BASH_BACK_ERROR
}
# MAIN
set -TEue -o pipefail
[[ $# -ne 1 ]] && >&2 echo 'ERROR: script expected!' && builtin exit 1
trap 'on_exit $?' ERR TERM INT EXIT
trap 'on_return' DEBUG
# magic FD
exec 3> >(tee $TMP_ERROR >/dev/null) 4>/dev/stderr
$DEBUG && >/dev/tty echo '---stdout---'
source $SCRIPT 2>&3
exec 3> >(tee $BASH_BACK_ERROR >/dev/null) 4>/dev/stderr
$BASH_BACK_DEBUG && >/dev/tty echo '---stdout---'
source $1 2>&3
success

13
useless.sh

@ -22,6 +22,10 @@ function F1 {
function main {
F1
F3
>&2 echo some trace in stderr1
lib/utility.bash
>&2 echo some trace in stderr2
}
function F3 {
@ -30,13 +34,6 @@ function F3 {
}
echo start of useless
F3
>&2 echo some trace in stderr1
lib/utility.bash
>&2 echo some trace in stderr2
. lib/utility.bash
main
# error
main
echo end of useless
Loading…
Cancel
Save