-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sh
120 lines (94 loc) · 1.98 KB
/
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
# shellcheck shell=bash
execute() {
local -r cmd="$1"
local -r msg="${2-$1}"
local -r frames='/-\|'
local -r n_frames=${#frames}
local -r tmpFile="$(mktemp /tmp/XXXXX)"
# Create handler for killing subprocesses on exit if we don't already have one.
trap -p EXIT | grep kill_all_subprocesses &> /dev/null \
|| trap kill_all_subprocesses EXIT
eval "$cmd" &> "$tmpFile" &
local cmdPid=$!
local i=0
local frameText=""
while kill -0 "$cmdPid" &> /dev/null; do
frameText=" [${frames:i++%n_frames:1}] $msg"
printf "%s" "$frameText"
sleep 0.2
printf "\r"
done
wait "$cmdPid" &> /dev/null
local exitCode=$?
if [[ "$exitCode" -eq 0 ]]; then
print_success "$msg"
else
print_error "$msg"
print_error_stream < "$tmpFile"
fi
rm -rf "$tmpFile"
return $exitCode
}
get_os() {
local os=""
local kernelName=""
kernelName="$(uname --kernel-name)"
readonly kernelName
if [[ "$kernelName" == "Linux" && -e "/etc/os-release" ]]; then
os="$(. /etc/os-release; echo "$ID")"
else
os="$kernelName"
fi
echo "$os"
}
is_wsl() {
if grep --extended-regexp --ignore-case --quiet "(microsoft|wsl)" /proc/version &> /dev/null; then
return 0
else
return 1
fi
}
kill_all_subprocesses() {
local pid=""
for pid in $(jobs -p); do
kill "$pid"
wait "$pid" &> /dev/null
done
}
print_error() {
print_in_red " [✖] $1\n"
}
print_error_stream() {
while read -r line; do
print_error "↳ ERROR: $line"
done
}
print_in_colour() {
printf "%b" "$(tput setaf "$2" 2> /dev/null)$1$(tput sgr0 2> /dev/null)"
}
print_in_cyan() {
print_in_colour "$1" 6
}
print_in_green() {
print_in_colour "$1" 2
}
print_in_red() {
print_in_colour "$1" 1
}
print_in_yellow() {
print_in_colour "$1" 3
}
print_question() {
print_in_yellow " [?] $1"
}
print_result() {
if [[ "$1" -eq 0 ]]; then
print_success "$2"
else
print_error "$2"
fi
return "$1"
}
print_success() {
print_in_green " [✔] $1\n"
}