-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.bash
149 lines (132 loc) · 3.78 KB
/
utils.bash
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
## DOWNLOAD:
## curl -L 'https://gitlab.com/snippets/1830548/raw?inline=false' -o utils.bash
## USAGE:
## source utils.bash
set -eETuo errexit -o nounset -o pipefail #-o errtrace -o functrace
shopt -s expand_aliases lastpipe #gnu_errfmt globstar huponexit inherit_errexit localvar_inherit
#[[ ${BASH_SOURCE:-} != "" ]] && {
# BASH_SOURCE=( EMPTY_BASH_SOURCE )
#}
#export PS4='+ $(date "+%F_%T") ${FUNCNAME[0]:-NOFUNC}() $BASH_SOURCE:${BASH_LINENO[0]} + ' ## From https://stackoverflow.com/a/22617858
export PS4=$'+\e[33m ${BASH_SOURCE[0]:-nofile}:${BASH_LINENO[0]:-noline} ${FUNCNAME[0]:-nofunc}()\e[0m '
var_is_set(){
declare -rn var=$1
! test -z ${var+x}
}
var_is_set_not_empty(){
declare -rn var=$1
! test -z ${var:+x}
}
var_is_unset(){
declare -rn var=$1
test -z ${var+x}
}
var_is_unset_or_empty(){
declare -rn var=$1
test -z ${var:+x}
}
[[ ${BASH_SOURCE:-} != "" ]] && SRCDIR="$(realpath $(dirname "${BASH_SOURCE[-1]}"))"
is_sourced(){
[[ "${BASH_SOURCE[0]}" != "${0}" ]]
}
if test -t /dev/stdout ;then
msgcolor=$'\e[1;37m'
msgnocol=$'\e[0m'
else
msgcolor=$'\e[1;37m'
msgnocol=$'\e[0m'
fi
if test -t /dev/stderr ;then
:;
fi
function colordbg {
if test -t /dev/stderr ;then
echo $'\e[0;36m'
fi
}
function echomsg { echo $'\e[1;37m'"$@"$'\e[0m'; }
if var_is_set DEBUG && [[ $DEBUG != 0 ]] ;then
#shopt -s extdebug
function echodbg { >/dev/stderr echo $'\e[0;36m'"DBG $@"$'\e[0m'; }
function catdbg { >/dev/stderr echo $'\e[0;36m'; cat $@; echo $'\e[0m'; }
else
function echodbg { :; } ## do nothing
fi
if var_is_unset NOINFO || [[ $NOWARN == 0 ]] ;then
function echoinfo { >/dev/stderr echo $'\e[0;37m'"INFO $@"$'\e[0m'; }
else
function echoinfo { :; } ## do nothing
fi
if var_is_unset NOWARN || [[ $NOWARN == 0 ]] ;then
function echowarn { >/dev/stderr echo $'\e[0;33m'"WARN $@"$'\e[0m'; }
else
function echowarn { :; } ## do nothing
fi
function echoerr { >/dev/stderr echo $'\e[0;31m'"ERR $@"$'\e[0m'; }
function fatalerr {
echoerr "$@"
stacktrace #2 2
exit 1
}
alias die=fatalerr
function stacktrace {
local i=1
while caller $i | read line func file; do
echodbg "[$i] $file:$line $func()"
((i++))
done
}
function stacktrace2 {
local i=${1:-1} size=${#BASH_SOURCE[@]}
((i<size)) && echodbg "STACKTRACE"
for ((; i < size-1; i++)) ;do ## -1 to exclude main()
((frame=${#BASH_SOURCE[@]}-i-2 ))
echodbg "[$frame] ${BASH_SOURCE[$i]:-}:${BASH_LINENO[$i]} ${FUNCNAME[$i+1]}()"
done
}
## Execute only for debug purposes command
if var_is_set DEBUG && [[ $DEBUG != 0 ]] ;then
function debug { "$@" | catdbg; }
else
function debug { : "$@"; } ## do nothing
fi
## Do not overwrite but append.
function trap_append {
local handler="$1"
shift
for sigspec in "$@" ;do
local old_handler="$( trap -p "$sigspec" | perl -e "$/ = undef; <STDIN> =~ /'(.*?)'/s; print \$1;" )"
handler="$old_handler"$'\n'"$handler"
trap "$handler" "$sigspec"
done
}
array(){
array=$1; shift; "$@"
}
contains_element(){
declare -n __arr=$array
local elem
for elem in "${__arr[@]}" ;do [[ "$elem" == "$1" ]] && return 0; done
return 1
}
## Usage for_each var_name in array_name do
for_each_in_var(){
declare -rn _a=$1
shift
for elem in ${!_a[@]} ;do
eval "$@" $elem
done
}
kill_sure(){
kill -TERM "$@" &>/dev/null || return 0 ## Already dead
sleep 0.3
if kill -0 "$@" &>/dev/null ;then
echowarn "Force kill $1"
kill -9 "$@" 2>&-
fi
}
function OnError { caller | { read line file; echoerr "in $file:$line"; }; }
#OnError(){ echoerr "in $BASH_SOURCE:$BASH_LINENO"; stacktrace; }
#OnError(){ echoerr "in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"; stacktrace; }
trap OnError ERR