-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
helpers.bash
334 lines (306 loc) · 7.78 KB
/
helpers.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env bash
# _ _
# | |__ ___| |_ __ ___ _ __ ___
# | '_ \ / _ \ | '_ \ / _ \ '__/ __|
# | | | | __/ | |_) | __/ | \__ \
# |_| |_|\___|_| .__/ \___|_| |___/
# |_|
#
# Helper functions.
#
# These functions are primarily intended to be used within scripts. Each name
# starts with a leading underscore to indicate that it is an internal
# function and avoid collisions with gloablly defined names.
#
# Bash Boilerplate: https://github.com/xwmx/bash-boilerplate
#
# Copyright (c) 2016 William Melody • hi@williammelody.com
###############################################################################
###############################################################################
# _command_exists()
#
# Usage:
# _command_exists <name>
#
# Exit / Error Status:
# 0 (success, true) If a command with <name> is defined in the current
# environment.
# 1 (error, false) If not.
#
# Information on why `hash` is used here:
# http://stackoverflow.com/a/677212
_command_exists() {
hash "${1}" 2>/dev/null
}
###############################################################################
# _contains()
#
# Usage:
# _contains <query> <list-item>...
#
# Exit / Error Status:
# 0 (success, true) If the item is included in the list.
# 1 (error, false) If not.
#
# Examples:
# _contains "${_query}" "${_list[@]}"
_contains() {
local _query="${1:-}"
shift
if [[ -z "${_query}" ]] ||
[[ -z "${*:-}" ]]
then
return 1
fi
for __element in "${@}"
do
[[ "${__element}" == "${_query}" ]] && return 0
done
return 1
}
###############################################################################
# _download_from()
#
# Usage:
# _download_from <url> [<outfile>]
#
# Description:
# Download the file at <url> and print to standard output or <outfile>, if
# present. Uses `curl` if available, falling back to `wget`. Messages from
# `curl` and `wget` are suppressed.
#
# Exit / Error Status:
# 0 (success, true) If the download is successful.
# 1 (error, false) If there was an error.
#
# Examples:
# # Download and stream to standard output.
# _download_from "https://example.com" | less
#
# # Download to outfile with error handling.
# if ! _download_from "https://example.com/example.pdf" /path/to/example.pdf
# then
# printf "Download error.\\n"
# exit 1
# fi
_download_from() {
local _downloaded=0
local _target_path="${2:-}"
local _timeout=15
local _url="${1:-}"
if [[ -z "${_url}" ]] ||
[[ ! "${_url}" =~ ^https\:|^http\:|^file\:|^ftp\:|^sftp\: ]]
then
return 1
fi
if [[ -n "${_target_path}" ]]
then
if hash "curl" 2>/dev/null
then
curl \
--silent \
--location \
--connect-timeout "${_timeout}" \
"${_url}" \
--output "${_target_path}" \
&& _downloaded=1
elif hash "wget" 2>/dev/null
then
wget \
--quiet \
--connect-timeout="${_timeout}" \
--dns-timeout="${_timeout}" \
-O "${_target_path}" \
"${_url}" \
2>/dev/null \
&& _downloaded=1
fi
else
if hash "curl" 2>/dev/null
then
curl \
--silent \
--location \
--connect-timeout "${_timeout}" \
"${_url}" \
&& _downloaded=1
elif hash "wget" 2>/dev/null
then
wget \
--quiet \
--connect-timeout="${_timeout}" \
--dns-timeout="${_timeout}" \
-O - \
"${_url}" \
2>/dev/null \
&& _downloaded=1
fi
fi
if ! ((_downloaded))
then
return 1
fi
}
###############################################################################
# _interactive_input()
#
# Usage:
# _interactive_input
#
# Exit / Error Status:
# 0 (success, true) If the current input is interactive (eg, a shell).
# 1 (error, false) If the current input is stdin / piped input.
_interactive_input() {
[[ -t 0 ]]
}
###############################################################################
# _join()
#
# Usage:
# _join <delimiter> <list-item>...
#
# Description:
# Print a string containing all <list-item> arguments separated by
# <delimeter>.
#
# Example:
# _join "${_delimeter}" "${_list[@]}"
#
# More Information:
# https://stackoverflow.com/a/17841619
_join() {
local _delimiter="${1:-}"
shift
local _joined_string="${1:-}"
shift
local __element
for __element in "${@:-}"
do
[[ -n "${__element:-}" ]] || continue
_joined_string+="${_delimiter:-}${__element:-}"
done
printf "%s\\n" "${_joined_string}"
}
###############################################################################
# _readlink()
#
# Usage:
# _readlink [-e|-f|<options>] <path/to/symlink>
#
# Options:
# -f All but the last component must exist.
# -e All components must exist.
#
# Description:
# Wrapper for `readlink` that provides portable versions of GNU `readlink -f`
# and `readlink -e`, which canonicalize by following every symlink in every
# component of the given name recursively.
#
# More Information:
# http://stackoverflow.com/a/1116890
_readlink() {
local _target_path
local _target_file
local _final_directory
local _final_path
local _option
for __arg in "${@:-}"
do
case "${__arg}" in
-e|-f)
_option="${__arg}"
;;
-*)
# do nothing
# ':' is bash no-op
:
;;
*)
if [[ -z "${_target_path:-}" ]]
then
_target_path="${__arg}"
fi
;;
esac
done
if [[ -z "${_option}" ]]
then
readlink "${@}"
else
if [[ -z "${_target_path:-}" ]]
then
printf "_readlink: missing operand\\n"
return 1
fi
cd "$(dirname "${_target_path}")" || return 1
_target_file="$(basename "${_target_path}")"
# Iterate down a (possible) chain of symlinks
while [[ -L "${_target_file}" ]]
do
_target_file="$(readlink "${_target_file}")"
cd "$(dirname "${_target_file}")" || return 1
_target_file="$(basename "${_target_file}")"
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
_final_directory="$(pwd -P)"
_final_path="${_final_directory}/${_target_file}"
if [[ "${_option}" == "-f" ]]
then
printf "%s\\n" "${_final_path}"
return 0
elif [[ "${_option}" == "-e" ]]
then
if [[ -e "${_final_path}" ]]
then
printf "%s\\n" "${_final_path}"
return 0
else
return 1
fi
else
return 1
fi
fi
}
###############################################################################
# _spinner()
#
# Usage:
# _spinner <pid>
#
# Description:
# Display an ascii spinner while <pid> is running.
#
# Example Usage:
# ```
# _spinner_example() {
# printf "Working..."
# (sleep 1) &
# _spinner $!
# printf "Done!\n"
# }
# (_spinner_example)
# ```
#
# More Information:
# http://fitnr.com/showing-a-bash-spinner.html
_spinner() {
local _pid="${1:-}"
local _delay=0.75
local _spin_string="|/-\\"
if [[ -z "${_pid}" ]]
then
printf "Usage: _spinner <pid>\\n"
return 1
fi
while ps a | awk '{print $1}' | grep -q "${_pid}"
do
local _temp="${_spin_string#?}"
printf " [%c] " "${_spin_string}"
_spin_string="${_temp}${_spin_string%${_temp}}"
sleep ${_delay}
printf "\\b\\b\\b\\b\\b\\b"
done
printf " \\b\\b\\b\\b"
}