Browse Source

backtrace.bash

main
pvincent 2 weeks ago
parent
commit
a62bd7acc5
  1. 48
      install.sh
  2. 47
      lib/backtrace.bash
  3. 12
      test.sh

48
install.sh

@ -6,33 +6,6 @@ BASEDIR=$(dirname $0)
## FUNCTIONS ## FUNCTIONS
function exit {
on_exit $1 true
}
function on_exit {
local error_code bypass_last_call stack_lines
error_code=$1
bypass_last_call=${2:-false}
stack_lines=("${BASH_LINENO[@]}")
$bypass_last_call && stack_lines=("${stack_lines[@]:1}")
# remove 1 on head list
[[ ${#stack_lines[@]} > 0 && ${stack_lines[0]} == 1 ]] && unset stack_lines[0]
# remove 0 on tail list
[[ ${#stack_lines[@]} > 0 && ${stack_lines[-1]} == 0 ]] && unset stack_lines[-1]
if [[ $error_code > 0 ]]; then
printf "\nEXIT #${error_code} due to error at line ${stack_lines[*]} : \n-----------------------------------------\n"
trap - ERR TERM INT EXIT
builtin exit "${error_code}"
else
echo SUCCESS
fi
}
function assert_debian13 { function assert_debian13 {
test -f /etc/debian_version && test -f /etc/debian_version &&
grep --quiet ^13\. /etc/debian_version || grep --quiet ^13\. /etc/debian_version ||
@ -67,26 +40,19 @@ function install_ghostty {
function install_homebrew { function install_homebrew {
# $nope # $nope
grep OK file
# exit 11 # exit 11
# echo AAA # echo AAA
on_exit 45
exit 45
raise_error 'BBB' raise_error 'BBB'
false false
echo BBB echo BBB
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
} }
function raise_error {
message=${1:-an undefined error has occured!}
code=${2:-1}
echo "error ${code}: ${message}" >&2
echo "FUNCNAME: ${FUNCNAME[@]}" >&2 # inner outer main
echo "BASH_SOURCE: ${BASH_SOURCE[@]}" script.sh script.sh script.sh >&2
echo "BASH_LINENO: ${BASH_LINENO[@]}" 52 55 0 >&2
trap - ERR TERM INT EXIT
# builtin exit "${code}"
on_exit $code true
}
function set_alternative { function set_alternative {
local key command path_to_command path_to_key local key command path_to_command path_to_key
key=$1 key=$1
@ -145,13 +111,11 @@ function install_neovim {
else else
echo 'nvim-vanilla ready!' echo 'nvim-vanilla ready!'
fi fi
exit 3
} }
## MAIN ## MAIN
set -TEue
trap 'on_exit $?' ERR TERM INT EXIT
source "$BASEDIR/lib/backtrace.bash"
assert_debian13 assert_debian13
assert_gnome_desktop assert_gnome_desktop

47
lib/backtrace.bash

@ -0,0 +1,47 @@
## CONSTANTS
CURRENT_SOURCE="${BASH_SOURCE[0]}"
## FUNCTIONS
function exit {
on_exit $1 true
}
function on_exit {
local error_code bypass_last_call stack_lines
error_code=$1
bypass_last_call=${2:-false}
echo $CURRENT_SOURCE bypass_last_call=$bypass_last_call
stack_lines=("${BASH_LINENO[@]}")
#
# # remove 1 on head list
# [[ ${#stack_lines[@]} > 0 && ${stack_lines[0]} == 1 ]] && unset stack_lines[0]
#
# # remove 0 on tail list
# [[ ${#stack_lines[@]} > 0 && ${stack_lines[-1]} == 0 ]] && unset stack_lines[-1]
if [[ $error_code > 0 ]]; then
printf "\nEXIT #${error_code} due to error at line ${stack_lines[*]} : \n-----------------------------------------\n"
echo "FUNCNAME: ${FUNCNAME[@]}" >&2 # inner outer main
echo "BASH_SOURCE: ${BASH_SOURCE[@]}" >&2
echo "BASH_LINENO: ${BASH_LINENO[@]}" >&2
trap - ERR TERM INT EXIT
builtin exit "${error_code}"
else
echo SUCCESS
fi
}
function raise_error {
message=${1:-an undefined error has occured!}
code=${2:-1}
echo "error ${code}: ${message}" >&2
trap - ERR TERM INT EXIT
on_exit $code true
}
set -TEue
trap 'on_exit $?' ERR TERM INT EXIT

12
test.sh

@ -0,0 +1,12 @@
#!/usr/bin/env -S -- bash
## CONSTANTS
BASEDIR=$(dirname $0)
## MAIN
source "$BASEDIR/lib/backtrace.bash"
echo START test
grep toto nope
echo END test
Loading…
Cancel
Save