-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathbash_functions.sh
192 lines (163 loc) · 3.56 KB
/
bash_functions.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
#!/usr/bin/env bash
# Common Bash Functions
abort() {
echo >&2 '
***************
*** ABORTED ***
***************
'
echo "An error occurred. Exiting..." >&2
exit 1
}
warning () {
echo >&2 "WARNING:" "$@"
}
die () {
echo >&2 "$@"
abort
}
echo_array() {
for value in "$@"; do
echo "${value}"
done
}
what_os() {
uname_out="$(uname -s)"
case "${uname_out}" in
Linux*) machine=Linux;;
Darwin*) machine=MacOS;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${uname_out}"
esac
echo "${machine}"
}
contains_files() {
local file_list=($@)
for file in "$@"; do
if ! [ -f "${file}" ]
then
return 1
fi
done
return 0
}
check_not_empty () {
local suggestion=${2:-}
if [[ -z "${1// }" ]]; then
die "Argument is empty. ${suggestion}"
fi
}
check_not_empty_variable () {
local var=$1
local suggestion=${2:-}
local empty_or_x
local value
# http://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash
empty_or_x=$(eval echo "\${${var}+x}")
value=$(eval echo "\${${var}}")
if [ -z "${empty_or_x}" ]; then
die "Variable '${var}' is unset. ${suggestion}"
elif [ -z "${value}" ]; then
die "Variable '${var}' is empty. ${suggestion}"
fi
}
check_file_exists () {
local file=$1
local suggestion=${2:-}
if [ ! -f "${file}" ]; then
die "File not found: '${file}' (PWD='${PWD}'). ${suggestion}"
fi
}
check_file_not_exists () {
local file=$1
local suggestion=${2:-}
if [ -f "${file}" ]; then
die "File found: '${file}' (PWD='${PWD}'). ${suggestion}"
fi
}
check_file_not_empty () {
local file=$1
local suggestion=${2:-}
check_file_exists "${file}" "${suggestion}"
if [ ! -s "${file}" ]; then
die "File is empty: '${file}' (PWD='${PWD}'). ${suggestion}"
fi
}
check_dir_exists () {
local directory=$1
local suggestion=${2:-}
if [ ! -d "${directory}" ]; then
die "Directory not found: '${directory}' (PWD='${PWD}'). ${suggestion}"
fi
}
check_command_exists () {
local command=$1
local suggestion=${2:-}
hash "${command}" 2>/dev/null || die "Command '${command}' is required but it's not installed. ${suggestion}"
}
delete_file_if_exists() {
local file=$1
if [ -f "${file}" ]; then
rm "${file}"
fi
}
copy_files() {
local target_path=$1
local file_list=(${@:2})
mkdir -p "${target_path}"
echo
for file in "${file_list[@]}"; do
echo "Copying ${file} ${target_path}"
cp -r "${file}" "${target_path}"
done
}
strip_prefix_and_file_from_path() {
local input=$1
local prefix_pattern=$2
local prefixless
prefixless="${input##${prefix_pattern}}"
dirname "${prefixless}"
}
strip_suffix() {
local input=$1
local suffix_pattern=$2
echo "${input%${suffix_pattern}}"
}
print_function_arguments() {
local func=$1
declare -f "$func" | grep -E "local.+\\\$[\{\(]*[0-9@]+"
}
retry() {
local command=$@
delay=10
max_attempt=20
attempt=0
until [ ${attempt} -ge ${max_attempt} ]
do
if ${command}; then
break
fi;
attempt=$[$attempt+1]
echo "Retry #${attempt}/${max_attempt}, wait ${delay} seconds"
sleep ${delay}
done
}
mac_compatibility() {
case "$(what_os)" in
MacOS)
echo "Checking MacOS compatibility"
check_command_exists greadlink "Read bin/README.md"
# shellcheck disable=SC2034
readlink="greadlink"
;;
Linux)
# shellcheck disable=SC2034
readlink="readlink"
;;
*)
die "Sorry, but this script doesn't support your system :("
;;
esac
}
mac_compatibility