This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
__commons.sh
executable file
·667 lines (583 loc) · 16.4 KB
/
__commons.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#!/usr/bin/env sh
# MIT License
#
# Copyright (c) 2022-2023 Carlo Corradini
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Fail on error
set -o errexit
# Disable wildcard character expansion
set -o noglob
# ================
# GLOBALS
# ================
# Downloader
DOWNLOADER=
# Return value
RETVAL=
# ================
# PARSE ARGUMENTS
# ================
# Assert argument has a value
# @param $1 Argument name
# @param $2 Argument value
parse_args_assert_value() {
[ -n "$2" ] || FATAL "Argument '$1' requires a non-empty value"
}
# Assert argument value is a non negative integer (>= 0)
# @param $1 Argument name
# @param $2 Argument value
parse_args_assert_non_negative_integer() {
{ is_integer "$2" && [ "$2" -ge 0 ]; } || FATAL "Value '$2' of argument '$1' is not a non negative number"
}
# Assert argument value is a positive integer (> 0)
# @param $1 Argument name
# @param $2 Argument value
parse_args_assert_positive_integer() {
{ is_integer "$2" && [ "$2" -gt 0 ]; } || FATAL "Value '$2' of argument '$1' is not a positive number"
}
# Parse command line arguments
# @param $@ Arguments
parse_args_commons() {
# Number of shift
_shifts=1
# Parse
case $1 in
--disable-color)
# Disable color
LOG_COLOR_ENABLE=false
;;
--disable-spinner)
# Disable spinner
SPINNER_ENABLE=false
;;
--log-level)
# Log level
parse_args_assert_value "$@"
case $2 in
fatal) LOG_LEVEL=$LOG_LEVEL_FATAL ;;
error) LOG_LEVEL=$LOG_LEVEL_ERROR ;;
warn) LOG_LEVEL=$LOG_LEVEL_WARN ;;
info) LOG_LEVEL=$LOG_LEVEL_INFO ;;
debug) LOG_LEVEL=$LOG_LEVEL_DEBUG ;;
*) FATAL "Value '$2' of argument '$1' is invalid" ;;
esac
_shifts=2
;;
--spinner)
# Spinner
parse_args_assert_value "$@"
case $2 in
dots) SPINNER_SYMBOLS=$SPINNER_SYMBOLS_DOTS ;;
grayscale) SPINNER_SYMBOLS=$SPINNER_SYMBOLS_GRAYSCALE ;;
propeller) SPINNER_SYMBOLS=$SPINNER_SYMBOLS_PROPELLER ;;
*) FATAL "Value '$2' of argument '$1' is invalid" ;;
esac
_shifts=2
;;
-*)
# Unknown argument
WARN "Unknown argument '$1' is ignored"
;;
*)
# No argument
WARN "Skipping argument '$1'"
;;
esac
# Return
RETVAL=$_shifts
}
# ================
# LOGGER
# ================
# Fatal log level. Cause exit failure
LOG_LEVEL_FATAL=100
# Error log level
LOG_LEVEL_ERROR=200
# Warning log level
LOG_LEVEL_WARN=300
# Informational log level
LOG_LEVEL_INFO=500
# Debug log level
LOG_LEVEL_DEBUG=600
# Log level
LOG_LEVEL=$LOG_LEVEL_INFO
# Log color flag
LOG_COLOR_ENABLE=true
# Convert log level to equivalent name
# @param $1 Log level
to_log_level_name() {
_log_level=${1:-LOG_LEVEL}
_log_level_name=
case $_log_level in
"$LOG_LEVEL_FATAL") _log_level_name=fatal ;;
"$LOG_LEVEL_ERROR") _log_level_name=error ;;
"$LOG_LEVEL_WARN") _log_level_name=warn ;;
"$LOG_LEVEL_INFO") _log_level_name=info ;;
"$LOG_LEVEL_DEBUG") _log_level_name=debug ;;
*) FATAL "Unknown log level '$_log_level'" ;;
esac
printf '%s\n' "$_log_level_name"
}
# Check if log level is enabled
# @param $1 Log level
is_log_level_enabled() {
[ "$1" -le "$LOG_LEVEL" ]
return $?
}
# Print log message
# @param $1 Log level
# @param $2 Message
_log_print_message() {
_log_level=${1:-LOG_LEVEL_FATAL}
shift
_log_level_name=
_log_message=${*:-}
_log_prefix=
_log_suffix="\033[0m"
# Check log level
is_log_level_enabled "$_log_level" || return 0
case $_log_level in
"$LOG_LEVEL_FATAL")
_log_level_name=FATAL
_log_prefix="\033[41;37m"
;;
"$LOG_LEVEL_ERROR")
_log_level_name=ERROR
_log_prefix="\033[1;31m"
;;
"$LOG_LEVEL_WARN")
_log_level_name=WARN
_log_prefix="\033[1;33m"
;;
"$LOG_LEVEL_INFO")
_log_level_name=INFO
_log_prefix="\033[37m"
;;
"$LOG_LEVEL_DEBUG")
_log_level_name=DEBUG
_log_prefix="\033[1;34m"
;;
esac
# Check color flag
if [ "$LOG_COLOR_ENABLE" = false ]; then
_log_prefix=
_log_suffix=
fi
# Log
printf '%b[%-5s] %b%b\n' "$_log_prefix" "$_log_level_name" "$_log_message" "$_log_suffix"
}
# Fatal log message
# @param $1 Message
FATAL() {
_log_print_message "$LOG_LEVEL_FATAL" "$1" >&2
exit 1
}
# Error log message
# @param $1 Message
ERROR() { _log_print_message "$LOG_LEVEL_ERROR" "$1" >&2; }
# Warning log message
# @param $1 Message
WARN() { _log_print_message "$LOG_LEVEL_WARN" "$1" >&2; }
# Informational log message
# @param $1 Message
INFO() { _log_print_message "$LOG_LEVEL_INFO" "$1"; }
# Debug log message
# @param $1 Message
# @param $2 JSON value
DEBUG() {
_log_print_message "$LOG_LEVEL_DEBUG" "$1"
if [ -n "$2" ] && is_log_level_enabled "$LOG_LEVEL_DEBUG"; then
printf '%s\n' "$2" | jq '.'
fi
}
# ================
# SPINNER
# ================
# Spinner PID
SPINNER_PID=
# Spinner symbol time in seconds
SPINNER_TIME=.1
# Spinner symbols dots
SPINNER_SYMBOLS_DOTS="⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏"
# Spinner symbols grayscale
SPINNER_SYMBOLS_GRAYSCALE="░░░░░░░ ▒░░░░░░ ▒▒░░░░░ ▒▒▒░░░░ ▒▒▒▒░░░ ▒▒▒▒▒░░ ▒▒▒▒▒▒░ ▒▒▒▒▒▒▒ ░▒▒▒▒▒▒ ░░▒▒▒▒▒ ░░░▒▒▒▒ ░░░░▒▒▒ ░░░░░▒▒ ░░░░░░▒"
# Spinner symbols propeller
SPINNER_SYMBOLS_PROPELLER="/ - \\ |"
# Spinner symbols
SPINNER_SYMBOLS=$SPINNER_SYMBOLS_PROPELLER
# Spinner flag
SPINNER_ENABLE=true
# Spinner logic
_spinner() {
# Termination flag
_terminate=false
# Termination signal
trap '_terminate=true' USR1
# Message
_spinner_message=${1:-""}
while :; do
# Cursor invisible
tput civis
for s in $SPINNER_SYMBOLS; do
# Save cursor position
tput sc
# Symbol and message
printf "%s %s" "$s" "$_spinner_message"
# Restore cursor position
tput rc
# Terminate
if [ "$_terminate" = true ]; then
# Clear line from position to end
tput el
break 2
fi
# Animation time
sleep "$SPINNER_TIME"
# Check parent still alive
# Parent PID
_spinner_ppid=$(ps -p "$$" -o ppid=)
if [ -n "$_spinner_ppid" ]; then
# shellcheck disable=SC2086
_spinner_parent_up=$(ps --no-headers $_spinner_ppid)
if [ -z "$_spinner_parent_up" ]; then break 2; fi
fi
done
done
# Cursor normal
tput cnorm
return 0
}
# Start spinner
# @param $1 Message
# shellcheck disable=SC2120
spinner_start() {
_spinner_message=${1:-"Loading..."}
INFO "$_spinner_message"
[ "$SPINNER_ENABLE" = true ] || return 0
[ -z "$SPINNER_PID" ] || FATAL "Spinner PID ($SPINNER_PID) already defined"
# Spawn spinner process
_spinner "$_spinner_message" &
# Spinner process id
SPINNER_PID=$!
}
# Stop spinner
spinner_stop() {
[ "$SPINNER_ENABLE" = true ] || return 0
[ -n "$SPINNER_PID" ] || FATAL "Spinner PID is undefined"
# Send termination signal
kill -s USR1 "$SPINNER_PID"
# Wait may fail
wait "$SPINNER_PID" || :
# Reset pid
SPINNER_PID=
}
# ================
# ASSERT
# ================
# Assert command is installed
# @param $1 Command name
assert_cmd() {
check_cmd "$1" || FATAL "Command '$1' not found"
DEBUG "Command '$1' found at '$(command -v "$1")'"
}
# Assert spinner
assert_spinner() {
[ "$SPINNER_ENABLE" = true ] || return 0
assert_cmd ps
assert_cmd tput
}
# Assert Docker image
# @param $1 Docker image
# @param $2 Dockerfile
# @param $3 Dockerfile context
assert_docker_image() {
assert_cmd docker
_docker_image=$1
_dockerfile=${2:-}
_dockerfile_context=${3:-}
! docker image inspect "$_docker_image" > /dev/null 2>&1 || {
DEBUG "Docker image '$_docker_image' found"
return 0
}
WARN "Docker image '$_docker_image' not found"
if [ -z "$_dockerfile" ]; then
INFO "Pulling Docker image '$_docker_image'"
docker pull "$_docker_image" || FATAL "Error pulling Docker image '$_docker_image'"
else
[ -n "$_dockerfile_context" ] || _dockerfile_context=$(dirname "$_dockerfile")
INFO "Building Docker image '$_docker_image' using Dockerfile '$_dockerfile' with context '$_dockerfile_context'"
docker build --rm -t "$_docker_image" -f "$_dockerfile" "$_dockerfile_context" || FATAL "Error building Docker image '$_docker_image'"
fi
}
# Assert executable downloader
assert_downloader() {
[ -z "$DOWNLOADER" ] || return 0
_assert_downloader() {
# Return failure if it doesn't exist or is no executable
[ -x "$(command -v "$1")" ] || return 1
# Set downloader
DOWNLOADER=$1
return 0
}
# Downloader command
_assert_downloader curl \
|| _assert_downloader wget \
|| FATAL "No executable downloader found: 'curl' or 'wget'"
DEBUG "Downloader '$DOWNLOADER' found at '$(command -v "$DOWNLOADER")'"
}
# Assert URL is reachable
# @param $1 URL address
# @param $2 Timeout in seconds
assert_url_reachability() {
assert_downloader
# URL address
_url_address=$1
# Timeout in seconds
_timeout=${2:-10}
DEBUG "Testing URL '$_url_address' reachability"
case $DOWNLOADER in
curl)
curl --fail --silent --show-error --max-time "$_timeout" "$_url_address" > /dev/null || FATAL "URL address '$_url_address' is unreachable"
;;
wget)
wget --quiet --spider --timeout="$_timeout" --tries=1 "$_url_address" 2>&1 || FATAL "URL address '$_url_address' is unreachable"
;;
*) FATAL "Unknown downloader '$DOWNLOADER'" ;;
esac
}
# ================
# CLEANUP
# ================
# Cleanup spinner
cleanup_spinner() {
{ [ "$SPINNER_ENABLE" = true ] && [ -n "$SPINNER_PID" ]; } || return 0
DEBUG "Resetting cursor"
tput rc
tput cnorm
SPINNER_ENABLE=
SPINNER_PID=
}
# Cleanup Docker container
# @param $1 Container id
cleanup_docker_container() {
{ [ -n "$1" ] && check_cmd docker; } || return 0
_container_id=$1
DEBUG "Stopping Docker container '$_container_id'"
docker stop "$_container_id" > /dev/null 2>&1 || return 0
DEBUG "Removing Docker container '$_container_id'"
docker rm "$_container_id" > /dev/null 2>&1 || return 0
}
# Cleanup directory
# @param $1 Directory path
cleanup_dir() {
{ [ -n "$1" ] && [ -d "$1" ]; } || return 0
DEBUG "Removing directory '$1'"
rm -rf "$1" || return 0
}
# ================
# FUNCTIONS
# ================
# Check command is installed
# @param $1 Command name
check_cmd() {
command -v "$1" > /dev/null 2>&1
}
# Parse URL address
# @param $1 URL address
parse_url() {
assert_cmd cut
assert_cmd grep
assert_cmd sed
# Protocol
_url_proto=$(printf '%s\n' "$1" | sed 's,^\(.*://\).*,\1,')
# Remove protocol
_url=$(printf '%s\n' "$1" | sed "s,$_url_proto,,")
# User
_url_user=$(printf '%s\n' "$_url" | cut -d@ -f1 | cut -d: -f1)
# Password
_url_password=$(printf '%s\n' "$_url" | cut -d@ -f1 | cut -d: -f2)
# Host
_url_host=$(printf '%s\n' "$_url" | sed "s,$_url_user@,," | cut -d/ -f1 | sed 's,:.*,,')
# Port
_url_port=$(printf '%s\n' "$_url" | sed "s,$_url_user@,," | cut -d/ -f1 | sed -e 's,^.*:,:,' -e 's,.*:\([0-9]*\).*,\1,' -e 's,[^0-9],,')
# Path
_url_path=$(printf '%s\n' "$_url" | cut -d/ -f2-)
# Return
RETVAL=$(
jq \
--null-input \
--arg url "$_url" \
--arg proto "$_url_proto" \
--arg user "$_url_user" \
--arg password "$_url_password" \
--arg host "$_url_host" \
--arg port "$_url_port" \
--arg path "$_url_path" \
'
{
"url": $url,
"proto": $proto,
"user": $user,
"password": $password,
"host": $host,
"port": $port,
"path": $path
}
'
)
}
# Download a file
# @param $1 Output location
# @param $2 Download URL
download() {
assert_downloader
# Download
DEBUG "Downloading file '$2' to '$1'"
case $DOWNLOADER in
curl)
curl --fail --silent --location --output "$1" "$2" || FATAL "Download file '$2' failed"
;;
wget)
wget --quiet --output-document="$1" "$2" || FATAL "Download file '$2' failed"
;;
*) FATAL "Unknown downloader '$DOWNLOADER'" ;;
esac
DEBUG "Successfully downloaded file '$2' to '$1'"
}
# Print downloaded content
# @param $1 Download URL
download_print() {
assert_downloader > /dev/null
# Download
case $DOWNLOADER in
curl)
curl --fail --silent --location --show-error "$1" || FATAL "Download print '$1' failed"
;;
wget)
wget --quiet --output-document=- "$1" 2>&1 || FATAL "Download print '$1' failed"
;;
*) FATAL "Unknown downloader '$DOWNLOADER'" ;;
esac
}
# Remove and create directory
# @param $1 Directory path
recreate_dir() {
_dir=$1
DEBUG "Recreating directory '$_dir'"
if [ -d "$_dir" ]; then
WARN "Removing directory '$_dir'"
rm -rf "$_dir" || FATAL "Error removing directory '$_dir'"
fi
INFO "Creating directory '$_dir'"
mkdir -p "$_dir" || FATAL "Error creating directory '$_dir'"
}
# Check if value is an integer number
# @param $1 Value
is_integer() {
[ -n "$1" ] || return 1
case $1 in
'' | *[!0-9]*) return 1 ;;
*) return 0 ;;
esac
}
# Git files
# @param $1 Git directory
git_files() {
assert_cmd basename
assert_cmd git
assert_cmd jq
_dir=$1
# Append .git if not present
[ "$(basename "$_dir")" = .git ] || _dir="$_dir/.git"
DEBUG "Git files in '$_dir' Git directory"
# Check if directory
[ -d "$_dir" ] || FATAL "Directory '$_dir' does not exists"
# Git files
_git_files=$(
git --git-dir "$_dir" ls-files --cached --others --exclude-standard --full-name \
| jq --raw-input --null-input '[inputs | select(length > 0)]'
) || FATAL "Error Git files in '$_dir' Git directory"
# Return
# shellcheck disable=2034
RETVAL=$_git_files
}
# Check if directory in git
# @param $1 Git files
# @param $2 Directory
git_has_directory() {
assert_cmd jq
_git_files=$1
_dir=$2
DEBUG "Checking Git has directory '$_dir'"
[ "$(printf '%s\n' "$_git_files" | jq --raw-output --arg dir "$_dir" 'any(.[]; startswith($dir))')" = true ]
return $?
}
# Check if file in git
# @param $1 Git files
# @param $2 File
git_has_file() {
assert_cmd jq
_git_files=$1
_file=$2
DEBUG "Checking Git has file '$_file'"
[ "$(printf '%s\n' "$_git_files" | jq --raw-output --arg file "$_file" 'any(.[]; . == $file)')" = true ]
return $?
}
# ================
# CONFIGURATION
# ================
# Help usage string
# shellcheck disable=2034
HELP_COMMONS_USAGE=$(
cat << EOF
Usage commons: $(basename "$0") [--disable-color] [--disable-spinner] [--log-level <LEVEL>] [--spinner <SPINNER>]
EOF
)
# Help options string
# shellcheck disable=2034
HELP_COMMONS_OPTIONS=$(
cat << EOF
Options commons:
--disable-color Disable color
--disable-spinner Disable spinner
--log-level <LEVEL> Logger level
Default: $(to_log_level_name "$LOG_LEVEL")
Values:
fatal Fatal level
error Error level
warn Warning level
info Informational level
debug Debug level
--spinner <SPINNER> Spinner
Default: propeller
Values:
dots Dots spinner
grayscale Grayscale spinner
propeller Propeller spinner
EOF
)
# Log level
LOG_LEVEL=$LOG_LEVEL_INFO
# Log color flag
LOG_COLOR_ENABLE=true
# Spinner symbols
SPINNER_SYMBOLS=$SPINNER_SYMBOLS_PROPELLER
# Spinner flag
SPINNER_ENABLE=true