-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_utils.sh
294 lines (245 loc) · 8.08 KB
/
common_utils.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
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
#
# common util functions.
# use short namespace `cu`, since these functions will be used frequent.
#
################################################################################
# api functions:
# - simple color print functions:
# - cu::red_echo
# - cu::yellow_echo
# - cu::blue_echo
# - cu::head_line_echo
# - validation functions:
# - cu::is_number_string
# - cu::is_blank_string
# - version related functions
# - cu::version_le
# - cu::version_lt
# - cu::version_ge
# - cu::version_gt
# - cu::is_version_match
# - cu::get_latest_version_match
# - cu::get_oldest_version_match
# - execution helper functions:
# - cu::log_then_run
# - cu::loose_run
# - cu::die
################################################################################
#
#_ source guard begin _#
[ -n "${source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA:+has_value}" ] && return
source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA=$(realpath -- "${BASH_SOURCE[0]}")
# the value of source guard is the canonical dir path of this script
readonly source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA=${source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA%/*}
#_ source guard end _#
set -eEuo pipefail
################################################################################
# simple color print functions
################################################################################
cu::color_echo() {
local color=$1
shift
# if stdout is terminal, turn on color output.
#
# about CI env var
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
if [[ -t 1 || "${GITHUB_ACTIONS:-}" = true ]]; then
printf "\e[1;${color}m%s\e[0m\n" "$*"
else
printf '%s\n' "$*"
fi
}
cu::red_echo() {
cu::color_echo 31 "$@"
}
cu::yellow_echo() {
cu::color_echo 33 "$@"
}
cu::blue_echo() {
cu::color_echo 36 "$@"
}
cu::head_line_echo() {
cu::color_echo "2;35;46" ================================================================================
cu::yellow_echo "$*"
cu::color_echo "2;35;46" ================================================================================
}
################################################################################
# validation functions
################################################################################
cu::is_number_string() {
(($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*"
[[ "$1" =~ ^[0-9]+$ ]]
}
cu::is_blank_string() {
(($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*"
[[ "$1" =~ ^[[:space:]]*$ ]]
}
################################################################################
# version related functions
#
# versions comparison/sort by command `sort -V`
#
# How to compare a program's version in a shell script?
# https://unix.stackexchange.com/questions/285924
################################################################################
# version comparison, is version1 less than or equal to version2?
#
# usage:
# cu::version_le <version1> <version2>
cu::version_le() {
(($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*"
local ver=$1
local destVer=$2
[ "$ver" = "$destVer" ] && return 0
[ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$ver" ]
}
# version comparison, is version1 less than version2?
#
# usage:
# cu::version_lt <version1> <version2>
cu::version_lt() {
(($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*"
local ver=$1
local destVer=$2
[ "$ver" = "$destVer" ] && return 1
[ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$ver" ]
}
# version comparison, is version1 greater than or equal to version2?
#
# usage:
# cu::version_ge <version1> <version2>
cu::version_ge() {
(($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*"
local ver=$1
local destVer=$2
[ "$ver" = "$destVer" ] && return 0
[ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$destVer" ]
}
# version comparison, is version1 greater than version2?
#
# usage:
# cu::version_gt <version1> <version2>
cu::version_gt() {
(($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*"
local ver=$1
local destVer=$2
[ "$ver" = "$destVer" ] && return 1
[ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$destVer" ]
}
# version match, is version match version pattern?
#
# usage:
# cu::version_gt <version> <version pattern>
#
# example:
# cu::is_version_match 11.0.1-p1 11 # true
# cu::is_version_match 11.0.1-p1 11.0 # true
# cu::is_version_match 11.0.1-p1 11.0.1 # true
# cu::is_version_match 11.0.1-p1 11.0.1-p1 # true
#
# cu::is_version_match 11.0.1-p1 10 # false
# cu::is_version_match 11.0.1-p1 12 # false
# cu::is_version_match 11.0.1-p1 11.1 # false
# cu::is_version_match 11.0.1-p1 11.0.0 # false
# cu::is_version_match 11.0.1-p1 11.0.1-rc # false
cu::is_version_match() {
(($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*"
local ver="$1" version_pattern="$2"
[[ "$ver" == "$version_pattern" || "$ver" == "$version_pattern"[.-]* ]]
}
cu::_get_first_version_match() {
(($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*"
local version_pattern="$1" ver
while read -r ver; do
if cu::is_version_match "$ver" "$version_pattern"; then
echo "$ver"
# drain the rest content of stdin
cat >/dev/null
return
fi
done
}
# get the latest version of versions(one version per line) from stdin
#
# usage:
# cu::get_latest_version_match <version pattern>
cu::get_latest_version_match() {
(($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*"
local -r version_pattern="$1"
sort -V -r | cu::_get_first_version_match "$version_pattern"
}
# get the oldest version of versions(one version per line) from stdin
#
# usage:
# cu::get_oldest_version_match <version pattern>
cu::get_oldest_version_match() {
(($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*"
local -r version_pattern="$1"
sort -V | cu::_get_first_version_match "$version_pattern"
}
################################################################################
# execution helper functions
################################################################################
# log the command line, then run it.
#
# usage:
# cu::log_then_run command_to_run command_args...
#
# example:
# cu::log_then_run echo hello world
cu::log_then_run() {
local simple_mode=false
[ "$1" = "-s" ] && {
simple_mode=true
shift
}
(($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided"
if $simple_mode; then
echo "Run under work directory $PWD : $(cu::print_calling_command_line "$@")" >&2
"$@"
else
cu::blue_echo "Run under work directory $PWD :" >&2
cu::blue_echo "$(cu::print_calling_command_line "$@")" >&2
time "$@"
fi
}
cu::loose_run() {
(($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided"
set +eEuo pipefail
"$@"
local exit_code=$?
set -eEuo pipefail
return $exit_code
}
# print calling(quoted) command line which is able to copy and paste to rerun safely
#
# How to get the complete calling command of a BASH script from inside the script (not just the arguments)
# https://stackoverflow.com/questions/36625593
#
# bash metacharacter:
# https://www.gnu.org/software/bash/manual/html_node/Definitions.html
cu::print_calling_command_line() {
local arg isFirst=true bash_meta_char_regex=$'[ \t\n|&;()<>$!"]'
for arg; do
if $isFirst; then
isFirst=false
else
printf ' '
fi
if [[ "$arg" =~ \' ]]; then
printf '%q' "$arg"
elif [[ "$arg" =~ $bash_meta_char_regex ]]; then
printf "'%s'" "$arg"
else
printf "%s" "$arg"
fi
done
echo
}
# output the error message then exit with error(exit code is 1)
cu::die() {
(($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided"
cu::red_echo "Error: $*" >&2
exit 1
}