-
Notifications
You must be signed in to change notification settings - Fork 721
/
lib.sh
282 lines (229 loc) · 4.8 KB
/
lib.sh
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
to_jsonlist() {
for x in "$@"
do echo "\"$x\""
done | jq --slurp '.'
}
jq_tolist() {
local exp=$1; shift
jq "$exp | join (\" \")" --raw-output "$@"
}
jq_fmutate() {
local f=$1; shift
test -f "$f" || { echo '{}' > "$f"; }
jq "$@" "$f" | sponge "$f"
}
jq_check_json() {
jq '.' "$1" >/dev/null
}
json_compact_prettify()
{
for f in "$@"
do if test -n "$(find $f -size +1M)";
## Skip large files.
then continue; fi
jq_fmutate "$f" --raw-input --raw-output --slurp \
'gsub("\n +";"")|gsub("\n ]";"]")|gsub(",\"";",\n \"")' &
done
wait
}
jscompact()
{
json_compact_prettify "$@"
}
helptopcmd() {
set +x
local topcmd=$1 cmd=$2; shift 2
white $topcmd
echo -n " "
yellow $cmd
echo -n " "
green $*
}
helpcmd() {
set +x
local cmd=$1; shift
yellow $cmd
echo -n " "
green $*
}
helpopt() {
set +x
green $*
}
__usage() {
local op=$1 desc=$2
cat >&2 <<EOF
$(red USAGE:) $(white $(basename "$0")) $(blue WB-OPTS..) $(red $op) $(green ${op^^[a-z]}-OPTS..) $(yellow SUBOP) $(green SUBOP-ARGS..)
$(blue $desc):
$(cat)
EOF
}
usage() {
set +x
__usage "$@"
exit 1
}
muffle_trace_set_exit='if test -n "$(echo $- | tr -cd x)"; then set +x; local exit="set -x"; else local exit=; fi'
restore_trace='eval $exit'
declare -A colormap
colormap=(
[black]=30
[red]=31
[green]=32
[yellow]=33
[blue]=34
[magenta]=35
[cyan]=36
[white]=37
[reset]=0
)
color() {
echo -ne "\033[${colormap[$1]}m"
}
with_color() {
eval $muffle_trace_set_exit
local color=$1; shift
color $color
echo -ne "$@"
color reset
eval $restore_trace
}
colorise_colors=(
red green blue yellow white cyan
)
colorise() {
eval $muffle_trace_set_exit
local i
for ((i=0; $#!=0; i++))
do echo -n "$(with_color ${colorise_colors[$((i % 6))]} $1) "
shift
done
eval $restore_trace
}
newline() {
echo >&2
}
msg() {
echo "workbench: $*" >&2
}
msg_ne() {
echo -ne "workbench: $*" >&2
}
plain() {
eval $muffle_trace_set_exit
with_color reset $*
eval $restore_trace
}
green() {
eval $muffle_trace_set_exit
with_color green $*
eval $restore_trace
}
blue() {
eval $muffle_trace_set_exit
with_color blue $*
eval $restore_trace
}
white() {
eval $muffle_trace_set_exit
with_color white $*
eval $restore_trace
}
blk() {
eval $muffle_trace_set_exit
with_color black $*
eval $restore_trace
}
yellow() {
eval $muffle_trace_set_exit
with_color yellow $*
eval $restore_trace
}
red() {
eval $muffle_trace_set_exit
with_color red $*
eval $restore_trace
}
verbose() {
eval $muffle_trace_set_exit
if test -n "${verbose:-}"
then local subsys=$1; shift
msg "$(with_color blue $subsys): $*"
fi
eval $restore_trace
}
progress() {
eval $muffle_trace_set_exit
local subsys=$1; shift
msg "$(with_color green $subsys): $(with_color blue "$@")"
eval $restore_trace
}
progress_ne() {
eval $muffle_trace_set_exit
local subsys=$1; shift
msg_ne "$(with_color green $subsys): $(with_color blue "$@")"
eval $restore_trace
}
warn() {
eval $muffle_trace_set_exit
local subsys=$1; shift
msg "$(with_color green $subsys): $(with_color yellow "$@")"
eval $restore_trace
}
fail() {
eval $muffle_trace_set_exit
msg "$(with_color red $*)"
exit 1
}
fatal() {
fail "FATAL: $*"
}
jqtest() {
jq --exit-status "$@" > /dev/null
}
git_repo_commit_description() {
local repo=$1
local commit=$(git -C "$repo" rev-parse 'HEAD' 2>/dev/null || true)
test -n "$commit" && {
git -C "$repo" describe --match '[0-9].*' --tags $commit 2>/dev/null |
cut -d'-' -f1,2 | tr -d '\n'
git -C "$repo" diff --exit-code --quiet || echo '-modified'
} || echo "unknown-not-a-git-repo"
}
# Wait for any job to fail or all to be OK!
wait_fail_any () {
local processes=("$@")
# There are any processes left?
if test -n "${processes[*]:-}"
then
local wait_exit_status
local exited_process
wait -n -p exited_process "${processes[@]}"
wait_exit_status=$?
# New array without the exited process
local processes_p=()
for p in "${processes[@]}"
do
if test "${p}" != "${exited_process}"
then
processes_p+=("${p}")
fi
done
# Something else to wait for?
if test -n "${processes_p[*]:-}"
then
# Keep waiting or kill 'em all ?'
if test "${wait_exit_status}" -eq 0
then
wait_fail_any "${processes_p[@]}"
else
kill "${processes_p[@]}" 2>/dev/null || true
return "${wait_exit_status}"
fi
else
return "${wait_exit_status}"
fi
else
return 0
fi
}