-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
.bash_safe_source
76 lines (65 loc) · 1.62 KB
/
.bash_safe_source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
# shellcheck disable=SC1091
# vim: ft=bash
# © Copyright 2021 Konstantin Gredeskoul
# Part of BASHMATIC framework.
set +e
function is-debug() {
export DEBUG=${DEBUG:-0}
export BASHMATIC_DEBUG=${BASHMATIC_DEBUG:-0}
export BASHMATIC_PATH_DEBUG=${BASHMATIC_PATH_DEBUG:-0}
local sum=$(( DEBUG + BASHMATIC_DEBUG + BASHMATIC_PATH_DEBUG ))
if [[ ${sum} -gt 0 ]]; then
return 0
else
return 1
fi
}
function debug-info() {
is-debug || return
printf "${txtblk}${bakgrn} [debug] ➔ ${clr} ${bldgrn}%-60.60s${clr}${bldgrn}$2" "$1"
[[ -n $3 ]] && eval "$3"
}
function debug-error() {
is-debug || return
printf "${txtblk}${bakred} [error] ➔ ${clr} ${bldred}%-60.60s${clr}${txtred}$2" "$1"
[[ -n $3 ]] && eval "$3"
}
function result-ok() {
is-debug && printf "${bldwht}${bakgrn}[ ✔︎ ]${clr}\n"
}
function result-failure() {
is-debug && printf "${bldwht}${bakred}[ ✘ ]${clr}"
if [[ $1 =~ ([0-9]) ]] ; then
printf " [ exit code=$1 ]\n"
else
echo
fi
}
is-debug && BASHMATIC_DEBUG=1
is-debug || unset BASHMATIC_DEBUG
if is-debug; then
# shellcheck source=./.envrc.debug.on
source "${BASHMATIC_HOME}/.envrc.debug.on"
else
# shellcheck source=./.envrc.debug.off
source "${BASHMATIC_HOME}/.envrc.debug.off"
fi
function source_if_exists() {
for file in "$@"; do
if [[ -s "${file}" ]]; then
debug-info "${file}"
# shellcheck source=./${file}
source "${file}"
local code=$?
if ((code)); then
result-failure ${code}
else
result-ok
fi
else
debug-error "File [$file] does not exist."
fi
done
return ${code}
}