This repository was archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommon.bash
More file actions
299 lines (249 loc) · 7.26 KB
/
common.bash
File metadata and controls
299 lines (249 loc) · 7.26 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
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
#!/usr/bin/env bash
#
# Common bash functions that we use in several scripts.
#
# Colors
#
# check if stdout is a terminal...
if test -t 1; then
# see if it supports colors...
ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then
export bold="$(tput bold)"
export underline="$(tput smul)"
export standout="$(tput smso)"
export standout_end="$(tput rmso)"
export normal="$(tput sgr0)"
export dim="$(tput dim)"
export black="$(tput setaf 0)"
export red="$(tput setaf 1)"
export green="$(tput setaf 2)"
export yellow="$(tput setaf 3)"
export blue="$(tput setaf 4)"
export magenta="$(tput setaf 5)"
export cyan="$(tput setaf 6)"
export white="$(tput setaf 7)"
fi
fi
# message_std
#
# param1: prefix
# param2: message text
function message_std
{
local prefix=${1}
local message=${2}
echo -e "${prefix}${message}"
}
#
# message_success
#
function message_success()
{
echo -e "${green}==============================${normal}"
echo -e "${green}= SUCCESS!${normal}"
echo -e "${green}==============================${normal}"
}
#
# message_failure
#
function message_failure()
{
echo -e "${red}==============================${normal}"
echo -e "${red}= FAILURE!${normal}"
echo -e "${red}==============================${normal}"
}
# print_centered_text
#
# Prints out a centered text string with endcaps
#
# param1: width
# param2: endcaps
# param3: text to print
function print_centered_text()
{
local width=${1:?}
local endcap=${2:?}
local text=${3:?}
local textsize=${#text}
local capsize=${#endcap}
local span1=$((($width + $textsize - $capsize * 2)/2))
local span2=$(($width - $span1 - $capsize * 2))
printf "%s%${span1}s%${span2}s%s\n" "${endcap}" "${text}" "" "${endcap}"
}
# print_banner()
#
# Prints out a banner block with date/time stamp.
#
# param1: banner text to print
function print_banner()
{
local banner_text=${1:?}
local textdate=$(date +"%Y-%m-%d %H:%M:%S")
local width=60
echo -e ""
echo -e "+----------------------------------------------------------+"
print_centered_text ${width} "|" "${banner_text}"
print_centered_text ${width} "|" " "
print_centered_text ${width} "|" "${textdate}"
echo -e "+----------------------------------------------------------+"
}
# print_banner_2lines()
#
# Prints out a two line banner plus a date/time stamp.
# param1: banner text line 1
# param2: banner text line 2
function print_banner_2lines()
{
local banner_text_line1=${1:?}
local banner_text_line2=${2:?}
local textdate=$(date +"%Y-%m-%d %H:%M:%S")
local width=60
echo -e ""
echo -e "+----------------------------------------------------------+"
print_centered_text ${width} "|" "${banner_text_line1}"
print_centered_text ${width} "|" "${banner_text_line2}"
print_centered_text ${width} "|" " "
print_centered_text ${width} "|" "${textdate}"
echo -e "+----------------------------------------------------------+"
}
# Gets the current script name (full path + filename)
function get_scriptname() {
# Get the full path to the current script
local script_name=`basename $0`
local script_path=$(dirname $(readlink -f $0))
local script_file="${script_path}/${script_name:?}"
echo "${script_file}"
}
# Gets the path to the current script (full path)
function get_scriptpath() {
# Get the full path to the current script
local script_name=`basename $0`
local script_path=$(dirname $(readlink -f $0))
echo "${script_path}"
}
# Get the md5sum of a filename.
# param1: filename
# returns: md5sum of the file.
function get_md5sum() {
local filename=${1:?}
local sig=$(md5sum ${filename:?} | cut -d' ' -f1)
echo "${sig:?}"
}
#
# Install Python pacakges using pip
#
# - @param1 pip_exe - the pip binary to use, i.e., pip3.
#
function get_python_packages() {
local pip_exe=${1:?}
echo -e "--- Pip : ${pip_exe:?}"
pip_args=(
--use-feature=2020-resolver
configparser
mock
pytest
pytest-cov
)
echo -e "--- ${pip_exe:?} install --user ${pip_args[@]}"
${pip_exe:?} install --user ${pip_args[@]}
}
# executable_exists
#
# param1: executable (with path if necessary)
function executable_exists()
{
local cmd=${1:?}
local output=1
if [ ! command -v ${cmd:?} &> /dev/null ]; then
output=0
fi
echo ${output:?}
}
# execute_command
#
# param1: command to execute
function execute_command()
{
local command=${1:?}
message_std "$ ${magenta}${command:?}${normal}"
local is_executable=$(executable_exists ${command:?})
if [[ "${is_executable}" == "1" ]]; then
eval ${command:?}
local err=$?
if [ $err -ne 0 ]; then
message_std "${red}FAILED${normal}"
else
message_std "${green}OK${normal}"
fi
else
message_std "${red}ERROR: command '${command:?}' is not executable"
message_std "${red}FAILED${normal}"
fi
}
# execute_command_checked
#
# param1: command to execute
function execute_command_checked()
{
local command=${1:?}
message_std "$ ${magenta}${command:?}${normal}"
local is_executable=$(executable_exists ${command:?})
if [[ "${is_executable}" == "1" ]]; then
eval ${command:?}
local err=$?
if [ $err -ne 0 ]; then
message_failure
exit $err
else
echo -e "${green}OK${normal}"
fi
else
print_message "${red}ERROR: command '${command:?}' is not executable"
print_message "${red}FAILED${normal}"
exit 32
fi
}
# Custom `select` implementation that allows *empty* input.
# Pass the choices as individual arguments.
# Output is the chosen item, or "", if the user just pressed ENTER.
# Example:
# choice=$(select_with_default 'one' 'two' 'three')
# Attribution: This is copied with minor modifications from here:
# https://stackoverflow.com/a/42790579/2059999
#
# If SELECTION_OVERRIDE is set then we use that value, otherwise we
# query the user.
#
select_with_default() {
local item i=0 numItems=$#
if [ ! -z ${SELECTION_OVERRIDE} ]; then
printf %s "${SELECTION_OVERRIDE}"
return
fi
if [[ "${numItems}" == "1" ]]; then
printf %s "${1}"
return
fi
# Print numbered menu items, based on the arguments passed.
for item; do # Short for: for item in "$@"; do
printf '%s\n' "$((++i))) $item"
done >&2 # Print to stderr, as `select` does.
# Prompt the user for the index of the desired item.
while :; do
printf %s "${PS3-#? }" >&2 # Print the prompt string to stderr, as `select` does.
read -r index
# Make sure that the input is either empty or that a valid index was entered.
[[ -z $index ]] && break # empty input
(( index >= 1 && index <= numItems )) 2>/dev/null || { echo "Invalid selection. Please try again." >&2; continue; }
break
done
if [ -z $index ]; then
index=1
fi
#echo -e ">>> index '${index}'" >&2
#echo -e ">>> opt1 '${1}'" >&2
#echo -e ">>> opt2 '${1}'" >&2
# Output the selected item, if any.
[[ -n $index ]] && printf %s "${@: index:1}"
}