-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctivi-bash-boilerplate
334 lines (301 loc) · 9.97 KB
/
octivi-bash-boilerplate
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
# SPDX-FileCopyrightText: 2016-2025 IMAGIN Sp. z o.o.
# SPDX-FileContributor: Marcin Engelmann <[email protected]>
# SPDX-License-Identifier: MIT
#
# Part of Octivi Bash Boilerplate https://github.com/octivi/bash-boilerplate
#
# Specify the shell for shellcheck as we don't use shebang here
# shellcheck shell=bash
################################################################################
# Define colors and formatting
#
# Define colors ANSI 4-bit colors
# - https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
# - https://misc.flogisoft.com/bash/tip_colors_and_formatting
# - https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
# - https://superuser.com/questions/301353/escape-non-printing-characters-in-a-function-for-a-bash-prompt
################################################################################
# \x1b (ESC Escape) is hex equivalent of Bash '\e' that can be used more widely.
# Foreground # Background
readonly __c_black=$'\x1b[0;30m'; readonly __c_b_black=$'\x1b[0;40m' # 0. Black
readonly __c_red=$'\x1b[0;31m'; readonly __c_b_red=$'\x1b[0;41m' # 1. Red
readonly __c_green=$'\x1b[0;32m'; readonly __c_b_green=$'\x1b[0;42m' # 2. Green
readonly __c_yellow=$'\x1b[0;33m'; readonly __c_b_yellow=$'\x1b[0;43m' # 3. Yellow
readonly __c_blue=$'\x1b[0;34m'; readonly __c_b_blue=$'\x1b[0;44m' # 4. Blue
readonly __c_magenta=$'\x1b[0;35m'; readonly __c_b_magenta=$'\x1b[0;45m' # 5. Magenta
readonly __c_cyan=$'\x1b[0;36m'; readonly __c_b_cyan=$'\x1b[0;46m' # 6. Cyan
readonly __c_white=$'\x1b[0;37m'; readonly __c_b_white=$'\x1b[0;47m' # 7. White
readonly __c_grey=$'\x1b[1;30m'; readonly __c_b_grey=$'\x1b[1;40m' # 8. Bright Black (Grey)
readonly __c_bred=$'\x1b[1;31m'; readonly __c_b_bred=$'\x1b[1;41m' # 9. Bright Red
readonly __c_bgreen=$'\x1b[1;32m'; readonly __c_b_bgreen=$'\x1b[1;42m' # 10. Bright Green
readonly __c_byellow=$'\x1b[1;33m'; readonly __c_b_byellow=$'\x1b[1;43m' # 11. Bright Yellow
readonly __c_bblue=$'\x1b[1;34m'; readonly __c_b_bblue=$'\x1b[1;44m' # 12. Bright Blue
readonly __c_bmagenta=$'\x1b[1;35m'; readonly __c_b_bmagenta=$'\x1b[1;45m' # 13. Bright Magenta
readonly __c_bcyan=$'\x1b[1;36m'; readonly __c_b_bcyan=$'\x1b[1;46m' # 14. Bright Cyan
readonly __c_bwhite=$'\x1b[1;37m'; readonly __c_b_bwhite=$'\x1b[1;47m' # 15. Bright White
readonly __c_default=$'\x1b[0;39m'; readonly __c_b_default=$'\x1b[0;49m' # Default color
readonly __c_bld=$'\x1b[1m' # Bold text
readonly __c_und=$'\x1b[4m' # Underline text
readonly __c_inv=$'\x1b[7m' # Inverse: swap background and foreground colors
readonly __c_reg=$'\x1b[22;24m' # Regular text only, keep colors
readonly __c_rst=$'\x1b[0m' # Reset all attributes
readonly __c_error="${__c_red}"
readonly __c_warning="${__c_yellow}"
readonly __c_ok="${__c_green}"
################################################################################
# Print help message
#
# Usage: usage
#
# Outputs:
# - Help message defined in the script code, prefixed with '#/'
################################################################################
function usage() {
# __my_path is assigned in script that includes this library
# shellcheck disable=SC2154
printf "%b\n" "$(grep '^#/' "${__my_path}" | cut -c4-)" 1>&2
}
################################################################################
# Output current timestamp in ISO 8601 format (2020-02-11T08:56:00+00:00)
# to stdout
#
# Usage: now
# echo "$(now)"
#
# Outputs:
# - timestamp
################################################################################
function now() {
printf '%(%Y-%m-%dT%H:%M:%S%z)T' -1
}
################################################################################
# Output timestamp and message to stdout
#
# Usage: echo "message" | print
# print "message"
#
# Arguments:
# - message to print on stdout (optional)
#
# Outputs:
# - timestamp and message
################################################################################
function print() {
if (($# == 0)) && [[ -p /dev/stdin ]]; then
# No arguments and stdin is opened, so read message from stdin
while read -r line; do
print "${line}"
done
else
# Message passed as an argument
printf '%s %b\n' "$(now)" "$*"
fi
}
################################################################################
# Print error message to stderr
#
# Usage: echo "message" | error
# error "message"
#
# Arguments:
# - message to print on stderr (optional)
#
# Outputs:
# - message
################################################################################
function error() {
if (($# == 0)) && [[ -p /dev/stdin ]]; then
# No arguments and stdin is opened, so read message from stdin
while read -r line; do
error "${line}"
done
else
# Message passed as an argument
if [[ -z ${NO_COLOR-} || -n ${FORCE_COLOR-} ]]; then
print "${__c_error}ERROR: $*${__c_rst}" 1>&2
else
print "ERROR: $*" 1>&2
fi
fi
}
################################################################################
# Print warning message to stderr
#
# Usage: echo "message" | warning
# warning "message"
#
# Arguments:
# - message to print on stderr (optional)
#
# Outputs:
# - message
################################################################################
function warning() {
if (($# == 0)) && [[ -p /dev/stdin ]]; then
# No arguments and stdin is opened, so read message from stdin
while read -r line; do
warning "${line}"
done
else
# Message passed as an argument
if [[ -z ${NO_COLOR-} || -n ${FORCE_COLOR-} ]]; then
print "${__c_warning}WARN: $*${__c_rst}" 1>&2
else
print "WARN: $*" 1>&2
fi
fi
}
################################################################################
# Print OK message to stdout
#
# Usage: echo "message" | ok
# ok "message"
#
# Arguments:
# - message to print on stdout (optional)
#
# Outputs:
# - message
################################################################################
function ok() {
if (($# == 0)) && [[ -p /dev/stdin ]]; then
# No arguments and stdin is opened, so read message from stdin
while read -r line; do
ok "${line}"
done
else
# Message passed as an argument
if [[ -z ${NO_COLOR-} || -n ${FORCE_COLOR-} ]]; then
print "${__c_ok}OK: $*${__c_rst}"
else
print "OK: $*"
fi
fi
}
################################################################################
# Print a bigger header to stdout
#
# Usage: echo "header" | header
# header "header"
#
# Arguments:
# - header to print on stdout (optional)
#
# Outputs:
# - header
################################################################################
function header() {
if (($# == 0)) && [[ -p /dev/stdin ]]; then
# No arguments and stdin is opened, so read header from stdin
while read -r line; do
header "${line}"
done
else
# Message passed as an argument
print "##########"
print "# $*"
print "##########"
fi
}
################################################################################
# Print an optional message and exit with either the given status or that of the
# most recent command
#
# Based on https://mywiki.wooledge.org/BashFAQ/101
#
# Usage: some_command || die [status code] [message]
# die [status code] [message]
#
# Arguments:
# - status code (optional)
# - message to print on exit (optional)
#
# Outputs:
# - message
################################################################################
function die() {
# Get the exit code of the most recent command.
local ret=$?
if (( $# > 0 )) && [[ $1 != *[^0-9]* ]]; then
# At least one argument and it is a number, use as an exit code
ret="$1"
shift
fi
if (( $# > 0 )); then
# Still at least one argument, use all of them as a message
local -r message="$*"
fi
if [[ -v message ]]; then
if [[ $ret ]]; then
error "${message}"
else
print "${message}"
fi
fi
exit "${ret}"
}
################################################################################
# Check if this instance of bash version is at least X.Y
#
# Usage: require_bash_version 4 4 || die 2 "At least Bash 4.4 is required"
#
# Arguments:
# - minor
# - major
#
# Returns:
# - 0 bash version is at least as required
# - 1 bash version is older than required
################################################################################
function require_bash_version() {
[[ ${BASH_VERSINFO[0]} -gt $1 || (${BASH_VERSINFO[0]} -eq $1 && ${BASH_VERSINFO[1]} -ge $2) ]]
return
}
################################################################################
# Check if required command is available
#
# Usage: require_command || die 2 "Cannot find 'X'. Install 'xxx' package"
# require_command || die 2
#
# Arguments:
# - required command
#
# Returns:
# - 0 required command is available
# - 1 required command is not available
################################################################################
function require_command() {
type -P "$1" > /dev/null
return
}
################################################################################
# Check if script is being run by root user
#
# Usage: require_root || die 2 "This script must be run as root"
# require_root && die 2 "This script must not be run as root"
#
# Returns:
# - 0 being run as root
# - 1 not being run as root
################################################################################
function require_root() {
(( EUID == 0 ))
return
}
################################################################################
# Check if script is piped
#
# Usage: is_piped
#
# Returns:
# - 0 being piped
# - 1 not being piped
################################################################################
function is_piped() {
[[ ! -t 1 ]]
return
}
# Disable colors if script is piped
if is_piped; then
NO_COLOR=1
fi