simplified
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
MIAOU-BASH
|
||||
===========
|
||||
|
||||
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.
|
||||
MIAOU-BASH is a collection of settings and helpers aiming for leveraging BASH.
|
||||
Developer-friendly, it may be used as a solo package or combined with other miaou-* utilities.
|
||||
|
||||
## Featuring
|
||||
|
||||
@@ -16,24 +16,27 @@ It's Free Software (AGPLv3), help yourself.
|
||||
Any feedback would be appreciated. [contact@artcode.re](mailto:contact@artcode.re)
|
||||
|
||||
## install
|
||||
|
||||
`curl https://git.artcode.re/miaou/miaou-bash/raw/branch/main/install.sh | sudo bash -s`
|
||||
|
||||
## upgrade
|
||||
|
||||
`upgrade-miaou-bash`
|
||||
|
||||
## uninstall
|
||||
|
||||
`cd /opt/miaou-bash && sudo ./uninstall.sh`
|
||||
|
||||
## development mode (credentials required)
|
||||
|
||||
|
||||
* REAL_DIR=/opt/miaou-bash
|
||||
* sudo mkdir -m 755 $REAL_DIR && sudo chown $(id -un) $REAL_DIR
|
||||
* git clone git@artcode.re:miaou/miaou-bash.git $REAL_DIR
|
||||
* git clone <git@artcode.re>:miaou/miaou-bash.git $REAL_DIR
|
||||
* DEV_DIR=$HOME/DEV/BASH/miaou-bash # change DEST according to your needs!
|
||||
* ln -s $REAL_DIR $DEV_DIR
|
||||
* cd $REAL_DIR && sudo ./install.sh
|
||||
|
||||
## useful library
|
||||
## useful library
|
||||
|
||||
* source /opt/miaou-bash/lib/functions.bash
|
||||
|
||||
|
||||
+22
-21
@@ -2,15 +2,16 @@
|
||||
|
||||
# CONSTANTS
|
||||
|
||||
BASH_BACK_ERROR=$(mktemp -t "$(basename "$0").XXXXXXXX" --tmpdir="/run/user/$(id -u)")
|
||||
BASH_BACK_DEBUG=${BASH_BACK_DEBUG:-false}
|
||||
readonly BASH_BACK_ERROR BASH_BACK_DEBUG
|
||||
MIAOU_BASH_ERROR=$(mktemp -t "$(basename "$0").XXXXXXXX" --tmpdir="/run/user/$(id -u)")
|
||||
MIAOU_BASH_DEBUG=${MIAOU_BASH_DEBUG:-false}
|
||||
readonly MIAOU_BASH_ERROR MIAOU_BASH_DEBUG
|
||||
|
||||
# FUNCTIONS
|
||||
|
||||
function on_return {
|
||||
# regex does not work here unfortunately!
|
||||
if [[ "${BASH_COMMAND}" == 'return '* ]]; then
|
||||
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2) # regex does not work here unfortunately!
|
||||
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2)
|
||||
if [[ -n "$code" ]] && [[ "$code" -ne 0 ]]; then
|
||||
trap - DEBUG
|
||||
>&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: return $code from ${FUNCNAME[1]}"
|
||||
@@ -55,7 +56,7 @@ function on_exit {
|
||||
stack_summary=()
|
||||
|
||||
if [[ $code -gt 0 ]]; then
|
||||
last_error=$(tail -n1 "$BASH_BACK_ERROR")
|
||||
last_error=$(tail -n1 "$MIAOU_BASH_ERROR")
|
||||
last_source=${stack_sources[0]}
|
||||
last_source=${last_source//./"\."} # replace '.' with '\.'
|
||||
regex1="^$last_source: line ([0-9]+): (.*)\$"
|
||||
@@ -87,44 +88,44 @@ function on_exit {
|
||||
stack_detail=$(printf "\t%30s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}")
|
||||
stack_summary+=("$stack_detail")
|
||||
done
|
||||
>&4 dump_stderr true
|
||||
dump_summary
|
||||
dump_failure
|
||||
else
|
||||
>&4 dump_stderr false
|
||||
dump_success
|
||||
fi
|
||||
|
||||
cleanup
|
||||
builtin exit "$code"
|
||||
}
|
||||
|
||||
function dump_summary {
|
||||
$BASH_BACK_DEBUG && >/dev/tty echo '---stacktrace---'
|
||||
function dump_failure {
|
||||
dump_stderr true
|
||||
$MIAOU_BASH_DEBUG && >/dev/tty echo '---stacktrace---'
|
||||
for i in "${stack_summary[@]}"; do
|
||||
>&4 echo -e "$i"
|
||||
done
|
||||
cleanup
|
||||
}
|
||||
|
||||
function dump_stderr {
|
||||
last_trim=${1:-false}
|
||||
mapfile -t tmp_error <"$BASH_BACK_ERROR"
|
||||
mapfile -t tmp_error <"$MIAOU_BASH_ERROR"
|
||||
$last_trim && unset 'tmp_error[-1]' && tmp_error=("${tmp_error[@]}")
|
||||
if [[ "${#tmp_error[@]}" -gt 0 ]]; then
|
||||
$BASH_BACK_DEBUG && >/dev/tty echo '---stderr---'
|
||||
$MIAOU_BASH_DEBUG && >/dev/tty echo '---stderr---'
|
||||
for i in "${tmp_error[@]}"; do
|
||||
echo -e "$i"
|
||||
>&4 echo -e "$i"
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
function success {
|
||||
>&2 dump_stderr
|
||||
function dump_success {
|
||||
dump_stderr
|
||||
cleanup
|
||||
}
|
||||
|
||||
function cleanup {
|
||||
trap - ERR TERM INT EXIT
|
||||
exec 3>&- 4>&-
|
||||
rm "$BASH_BACK_ERROR"
|
||||
rm "$MIAOU_BASH_ERROR"
|
||||
}
|
||||
|
||||
# MAIN
|
||||
@@ -136,10 +137,10 @@ trap 'on_exit $?' ERR TERM INT EXIT
|
||||
trap 'on_return' DEBUG
|
||||
BASH_ARGV0="$1" && shift # to pretend script runs by itself
|
||||
|
||||
# magic FD + delayed tee => /dev/fd/3 means delayed stderr, /dev/fd/4 remains stderr
|
||||
exec 3> >(tee "$BASH_BACK_ERROR" >/dev/null) 4>/dev/stderr
|
||||
# magic FD + delayed tee => /dev/fd/3 means delayed stderr, /dev/fd/4 remains original stderr
|
||||
exec 3> >(tee "$MIAOU_BASH_ERROR" >/dev/null) 4>/dev/stderr
|
||||
|
||||
$BASH_BACK_DEBUG && >/dev/tty echo '---stdout---'
|
||||
$MIAOU_BASH_DEBUG && >/dev/tty echo '---stdout---'
|
||||
# shellcheck source=/dev/null
|
||||
source "$BASH_ARGV0" 2>&3
|
||||
success
|
||||
dump_success
|
||||
|
||||
Reference in New Issue
Block a user