-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.sh
201 lines (167 loc) · 6.21 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
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
#!/bin/bash
# Sometime it is better to explicitly define an UNSET value for variables
UNSET="UNSET"
###################################################################################################
# COLOR DEFINITION
###################################################################################################
# Reset color
RCol='\e[0m'
# Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds
Bla='\e[0;30m'; BBla='\e[1;30m'; UBla='\e[4;30m'; IBla='\e[0;90m'; BIBla='\e[1;90m'; On_Bla='\e[40m'; On_IBla='\e[0;100m';
Red='\e[0;31m'; BRed='\e[1;31m'; URed='\e[4;31m'; IRed='\e[0;91m'; BIRed='\e[1;91m'; On_Red='\e[41m'; On_IRed='\e[0;101m';
Gre='\e[0;32m'; BGre='\e[1;32m'; UGre='\e[4;32m'; IGre='\e[0;92m'; BIGre='\e[1;92m'; On_Gre='\e[42m'; On_IGre='\e[0;102m';
Yel='\e[0;33m'; BYel='\e[1;33m'; UYel='\e[4;33m'; IYel='\e[0;93m'; BIYel='\e[1;93m'; On_Yel='\e[43m'; On_IYel='\e[0;103m';
Blu='\e[0;34m'; BBlu='\e[1;34m'; UBlu='\e[4;34m'; IBlu='\e[0;94m'; BIBlu='\e[1;94m'; On_Blu='\e[44m'; On_IBlu='\e[0;104m';
Pur='\e[0;35m'; BPur='\e[1;35m'; UPur='\e[4;35m'; IPur='\e[0;95m'; BIPur='\e[1;95m'; On_Pur='\e[45m'; On_IPur='\e[0;105m';
Cya='\e[0;36m'; BCya='\e[1;36m'; UCya='\e[4;36m'; ICya='\e[0;96m'; BICya='\e[1;96m'; On_Cya='\e[46m'; On_ICya='\e[0;106m';
Whi='\e[0;37m'; BWhi='\e[1;37m'; UWhi='\e[4;37m'; IWhi='\e[0;97m'; BIWhi='\e[1;97m'; On_Whi='\e[47m'; On_IWhi='\e[0;107m';
###################################################################################################
# NICE UNICODE CHARACTER (WITH COLOR)
###################################################################################################
check="${BGre}\U1d00c${RCol} "
cross="${BRed}\Ud7${RCol} "
star="${Cya}\U2605${RCol} "
warn="${BYel}\U26a0${RCol} "
###################################################################################################
# GENERIC ECHO FUNCTIONS
###################################################################################################
# Display usage it no argument
usageIfNoArgument() {
if [[ ${#} -eq 0 ]]; then
usage
fi
}
# Exit script with the given code, do nothing if no argument
exitIfCodeNotEmpty() {
local exitCode=${1}
if [[ "${exitCode}" != "" ]]; then
exit ${exitCode}
fi
}
# Display an error message (on STDERR) and exit the script if an exitCode has been given
error() {
local exitMessage=${1}
local exitCode=${2}
>&2 echo -e ${Red}${exitMessage}${RCol}
exitIfCodeNotEmpty ${exitCode}
}
# Echo starting with a green check
check() {
echo -e " ${check} ${1}"
}
# Echo starting with a red cross
fail() {
echo -e " ${cross} ${1}"
}
# Echo starting with a warn yellow icon
warn() {
echo -e " ${warn} ${1}"
}
# Colored echo message
info() {
echo -e "${Cya}${1}${RCol}"
}
# Advanced echo with a type in argument (check, warn, etc)
xecho() {
local message=${1}
local echoType=${2}
case ${echoType} in
check)
check ${message};;
fail)
fail ${message};;
warn)
warn ${message};;
info)
info ${message};;
*)
echo ${message};;
esac
}
# Colored Echo starting with a star
command() {
echo -e " ${star} ${BWhi}COMMAND: ${1}${RCol}"
}
###################################################################################################
# GENERIC SCRIPT FUNCTIONS
###################################################################################################
# Return the absolute path of this script (manage symbolic links)
getScriptDirectory() {
local source="${BASH_SOURCE[0]}"
local dir
# resolve source until the file is no longer a symlink
while [ -h "${source}" ]; do
dir="$( cd -P "$( dirname "${source}" )" && pwd )"
source="$(readlink "${source}")"
# if source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ ${source} != /* ]] && source="${dir}/${source}"
done
dir="$( cd -P "$( dirname "${source}" )" && pwd )"
echo "${dir}"
}
# Return 0 and write a check echo if the directory exists. If not, echo a fail or warn message depending the
# second argument and, if an exit code is set, exit, return 1 otherwise.
verifyDirectoryExists() {
local path=${1}
local echoFailType=${2}
local exitCode=${3}
local returnCode=0
if [ -d "${path}" ]; then
check "Directory '${path}' exists"
else
xecho "Directory '${path}' doesn't exist" ${echoFailType}
returnCode=1
fi
exitIfCodeNotEmpty ${exitCode}
return returnCode
}
# Return 0 and write a check echo if the file exists. If not, echo a fail or warn message depending the
# second argument and, if an exit code is set, exit, return 1 otherwise.
verifyFileExists() {
local path=${1}
local echoFailType=${2}
local exitCode=${3}
local returnCode=0
if [ -f "${path}" ]; then
check "File '${path}' exists"
else
xecho "File '${path}' doesn't exist" ${echoFailType}
returnCode=1
fi
exitIfCodeNotEmpty ${exitCode}
return returnCode
}
# Return 0 and write a check echo if the user exists. If not, echo a fail or warn message depending the
# second argument and, if an exit code is set, exit, return 1 otherwise.
verifyUserExists() {
local user=${1}
local echoFailType=${2}
local exitCode=${3}
local returnCode=0
id -u ${user} > /dev/null 2>&1
if [[ ${?} -eq 0 ]]; then
check "User '${user}' exists (UID=$(id -u ${user}), GID=$(id -u ${user}))"
else
xecho "User '${user}' doesn't exist on this system" ${echoFailType}
returnCode=1
fi
exitIfCodeNotEmpty ${exitCode}
return returnCode
}
# Return 0 and write a check echo if the image exists. If not, echo a fail or warn message depending the
# second argument and, if an exit code is set, exit, return 1 otherwise.
verifyDockerImageExists() {
local image=${1}
local echoFailType=${2}
local exitCode=${3}
local returnCode=0
docker images | grep ${image} > /dev/null 2>&1
if [[ ${?} -eq 0 ]]; then
check "Docker image '${image}' exists"
else
xecho "Docker image '${image}' doesn't exist" ${echoFailType}
returnCode=1
fi
exitIfCodeNotEmpty ${exitCode}
return returnCode
}