-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutil.sh
230 lines (184 loc) · 5.58 KB
/
util.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
# shellcheck shell=bash disable=SC2148
# UT - Utilities
# _UT is an associative array that holds data specific to general utilities.
declare -A _UT
# Declare externally defined global variables ---------------------------------
declare OK STDERR FALSE TRUE
# Getters/Setters -------------------------------------------------------------
# UT_runlogfile getter outputs the value of _UT[runlogfile], which contains the
# name of the file written to when running UT_run_with_progress.
UT_runlogfile() {
printf '%s' "${_UT[runlogfile]}"
}
# UT_tailf getter indicates whether a log will be shown in real-time during the
# 'run', TRUE, or not, FALSE.
# Args
UT_tailf() {
printf '%s' "${_UT[tailf]}"
}
# UT_tailf setter indicates whether a log should be shown during the 'run',
# TRUE, or not, FALSE.
# Args: arg1 - Whether to tail, TRUE or FALSE.
UT_set_tailf() {
_UT[tailf]="$1"
}
# UT_set_plain setter sets plain output, with no colour, animations, or UTF
# high byte characters.
# Args: arg1 - Whether to show plain output, TRUE or FALSE.
UT_set_plain() {
_UT[plain]="$1"
UT_disable_colours
}
# Public Functions ------------------------------------------------------------
# UT_disable_colours resets variables used for colourised output so that they
# contain no colour terminal escapes. Useful if stdin is not a terminal.
UT_disable_colours() {
_UT[yellow]=
_UT[green]=
_UT[red]=
_UT[normal]=
_UT[probablysuccess]="!"
_UT[success]="✓"
_UT[failure]="✕"
}
# UT_run_with_progress displays a progress spinner, item text, and a tick or
# cross based on the exit code.
# Args: arg1 - the text to display.
# arg2-N - remaining args are the program to run and its arguments.
UT_run_with_progress() {
local displaytext=$1 retval int spinner=()
_UT[runlogfile]=$(mktemp -p /var/tmp) || {
printf 'ERROR: mktmp failed.\n' >"${STDERR}"
err || return
}
shift
if [[ ${_UT[tailf]} == "${FALSE}" && ${_UT[plain]} == "${TRUE}" ]]; then
# Output with no colours or spinny
printf '%s' "${displaytext}"
eval "$*" &>"${_UT[runlogfile]}"
retval=$?
# Mark success/fail
if [[ ${retval} -eq 0 ]]; then
printf ' .. %s\n' "${_UT[success]}"
else
printf ' .. %s\n' "${_UT[failure]}"
fi
elif [[ ${_UT[tailf]} == "${FALSE}" ]]; then
# Output with colours and spinny
while read -r char; do
spinner+=("${char}")
done <<<"$(grep -o . <<<"${_UT[spinnerchars]}")"
(
eval "$*" &>"${_UT[runlogfile]}"
) &
# Turn the cursor off
tput civis
# Start the spin animation
printf ' %s' "${displaytext}"
(while true; do
for int in {0..3}; do
printf '\r %s ' "${spinner[int]}"
sleep .1
done
done) &
_UT[spinnerpid]=$!
disown
# Wait for the command to finish
wait %1 2>/dev/null
retval=$?
# Kill the spinner
kill "${_UT[spinnerpid]}" 2>/dev/null
_UT[spinnerpid]=
sleep .5
# Mark success/fail
if [[ ${retval} -eq 127 ]]; then
# The job finished before we started waiting for it
printf '\r %s\n' "${_UT[probablysuccess]}"
elif [[ ${retval} -eq 0 ]]; then
printf '\r %s\n' "${_UT[success]}"
else
printf '\r %s\n' "${_UT[failure]}"
fi
# Restore the cursor
tput cnorm
sleep .5
else
# Tailf output
printf 'COMMAND: %s\n\n' "$*"
(
eval "$*" &>/dev/stdout
) &
sleep 1
# Wait for the command to finish
wait %1 2>/dev/null
retval=$?
# Mark success/fail
if [[ ${retval} -eq 127 ]]; then
# The job finished before we started waiting for it
printf '\n\nSTATUS: OK - (Probably)\n\n'
elif [[ ${retval} -eq 0 ]]; then
printf '\n\nSTATUS: OK\n\n'
else
printf '\n\nSTATUS: FAIL\n\n'
fi
fi
return "${retval}"
}
# UT_cleanup removes any artifacts that were created during execution.
# This is called by 'MA_cleanup' trap only.
UT_cleanup() {
# Called when the script exits.
local int
# Kill the spinny, and anything else, if they're running
if [[ ${_UT[tailf]} == "${FALSE}" ]]; then
[[ -n ${_UT[spinnerpid]} ]] && {
printf '%s\r ✕%s\n' "${_UT[red]}" "${_UT[normal]}"
kill "${_UT[spinnerpid]}"
}
# If progress spinner crashed make sure the cursor is shown
[ -t 1 ] && tput cnorm
else
kill "${_UT[spinnerpid]}" &>/dev/null
fi
return "${OK}"
}
# UT_sed_json_block returns the value of the query_key between
# block_start_key:block_start_val and block_end_key.
# Args:
# Arg 1: data
# Arg 2: block_start_key
# Arg 3: block_start_val
# Arg 4: block_end_key
# Arg 5: query_key
UT_sed_json_block() {
local data block_start_key block_start_val block_end_key query_key
data="$1"
block_start_key="$2"
block_start_val="$3"
block_end_key="$4"
query_key="$5"
echo "$data" | sed -rn \
'/'"${block_start_key}"'.*'"${block_start_val}"'/,/'"${block_end_key}"'/ { s/^ *"'"${query_key}"'": *//p }' \
| tr -d ',"'
}
# Private Functions -----------------------------------------------------------
# _UT_new sets the initial values for the _UT associative array.
# Args: None expected.
_UT_new() {
_UT[tailf]="${FALSE}"
_UT[yellow]=$(tput setaf 3)
_UT[green]=$(tput setaf 2)
_UT[red]=$(tput setaf 1)
_UT[normal]=$(tput sgr0)
_UT[probablysuccess]="${_UT[yellow]}✓${_UT[normal]}"
_UT[success]="${_UT[green]}✓${_UT[normal]}"
_UT[failure]="${_UT[red]}✕${_UT[normal]}"
_UT[runlogfile]=
_UT[spinnerchars]="|/-\\"
_UT[spinnerpid]=
}
# Initialise _UT
_UT_new || exit 1
# vim helpers -----------------------------------------------------------------
#include globals.sh
# vim:ft=sh:sw=2:et:ts=2: