-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbash-helpers
More file actions
executable file
·161 lines (125 loc) · 3.63 KB
/
bash-helpers
File metadata and controls
executable file
·161 lines (125 loc) · 3.63 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
# Helper functions for shell scripts
# Put in you path and you can use them using "source bash-helpers".
set -e
set -o pipefail
if [[ $BASHRC_PROMPT_COLORS ]] ; then
NO_COLOR='\[\e[33;0;m\]'
GRAY='\[\e[38;5;243m\]'
GREY='\[\e[38;5;243m\]'
GREEN='\[\e[38;5;2m\]'
ORANGE='\[\e[38;5;3m\]'
RED='\[\e[38;5;1m\]'
PURPLE='\[\e[38;5;5m\]'
BLUE='\[\e[38;5;21m\]'
YELLOW='\[\e[38;5;226m\]'
PINK='\[\e[38;5;13m\]'
else
NO_COLOR=$(echo -e '\x1b[33;0;m')
GRAY=$(echo -e '\x1b[38;5;243m')
GREY=$(echo -e '\x1b[38;5;243m')
GREEN=$(echo -e '\x1b[38;5;2m')
ORANGE=$(echo -e '\x1b[38;5;3m')
RED=$(echo -e '\x1b[38;5;1m')
fi
function _LOG() {
local level=$1 ; shift
local log_level_prio
local crap
read log_level_prio crap <<<$(_calc_prio $LOG_LEVEL)
if [[ ! $LOG_LEVEL ]] ; then
if [[ -t 1 ]] ; then
log_level_prio=3
else
log_level_prio=6
fi
fi
local location
local prio
local show_location
local color
read prio show_location color <<<$(_calc_prio $level)
if [[ $prio < $log_level_prio ]] ; then
return
fi
if [[ $show_location > 0 ]] ; then
local function
local file
read location function file <<<$(caller 1)
if [[ $file ]] ; then
location=$(basename $file)":"$location
fi
if [[ $location ]] ; then
location=" "$location
fi
fi
if [ -t 1 ] ; then
echo -e "$color${level}$location> $@${NO_COLOR}" >&2
else
echo -e "$(date +'%F %T') ${level}$location> $@" >&2
fi
}
function _calc_prio() {
local level=$1
local prio
local show_location
local color
case $level in
TRACE) prio=1 ; show_location=1 ; color=$GRAY ;;
DEBUG) prio=2 ; show_location=0 ; color=$GRAY ;;
INFO) prio=3 ; show_location=0 ; color=$GREEN ;;
WARN) prio=4 ; show_location=0 ; color=$ORANGE ;;
ERROR) prio=5 ; show_location=1 ; color=$RED ;;
FATAL) prio=6 ; show_location=1 ; color=$RED ;;
esac
echo $prio $show_location $color
}
function TRACE() { _LOG "TRACE" "$@" ; }
function DEBUG() { _LOG "DEBUG" "$@" ; }
function INFO() { _LOG "INFO " "$@" ; }
function WARN() { _LOG "WARN " "$@" ; }
function ERROR() { _LOG "ERROR" "$@" ; }
function DIE() { _LOG "FATAL" "$@" ; exit 1 ; }
function RETURN() { echo -ne "$@" ; exit 0 ; }
function EXIT() {
if [[ "$@" ]] ; then
_LOG "INFO" "$@"
fi
exit 0
}
function usefatal() {
trap DIE ERR
}
function nousefatal() {
trap ERR
}
function repeat() {
perl -0777 -e 'print <STDIN> x $ARGV[0]' "$@"
}
# Print a line with a message the length of the screen.
function line() {
perl - $@ <<'EOF'
my $msg = " " . join(" ", @ARGV) . " ";
print "---", $msg , q{-} x ($ENV{COLUMNS} - 3 - length($msg)), "\n\n";
EOF
}
function prefixif() {
if [[ "$@" ]] ; then
echo -ne " $@"
return
fi
echo -ne "$@"
}
# Interpret argv using a docopt spec and save a JSON representation into $docopt
function docopt() {
if [[ $docopt_files ]] ; then
docopt_files=","$docopt_files
fi
local trace=$(docopt-convert $(readlink -f $0)$docopt_files --json --pretty -- "$@" 2>&1)
TRACE "$trace"
# print help and die if doc is invalid
docopt-convert $(readlink -f $0)$docopt_files -- "$@" || return 1
# save json to $docopt
docopt=$(docopt-convert $(readlink -f $0)$docopt_files --json -- "$@")
eval $(docopt-convert $(readlink -f $0)$docopt_files --env -- "$@")
}