forked from mibah/cdepl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil
executable file
·154 lines (131 loc) · 3.2 KB
/
util
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
#!/bin/bash
#
# Copyright (C) 2018 Heinrich-Heine-Universitaet Duesseldorf, Institute of Computer Science, Department Operating Systems
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#
readonly UTIL_LOG_LEVEL_DEBUG="4"
readonly UTIL_LOG_LEVEL_INFO="3"
readonly UTIL_LOG_LEVEL_WARN="2"
readonly UTIL_LOG_LEVEL_ERROR="1"
readonly UTIL_LOG_LEVEL_OFF="0"
__UTIL_LOG_LEVEL="4"
##
# Set the log level for the logger calls
#
# $1: Log level (refer to constants)
##
util_log_set_level()
{
local level=$1
if [ "$level" -gt "4" ]; then
level="4"
elif [ "$level" -lt "0" ]; then
level="0"
fi
__UTIL_LOG_LEVEL="$level"
}
##
# Log an error message and exit cdepl
#
# $1: Message to log
##
util_log_error_and_exit()
{
local msg=$1
>&2 printf "\\e[1;31m%s\\e[m\\n" "$msg"
local trace
trace="$(util_print_calltrace)"
if [ -n "${trace}" ]; then
>&2 printf "Call trace:\\n%s\\n" "$(util_print_calltrace)"
fi
exit -1
}
##
# Log an error message
#
# $1: Message to log
##
util_log_error()
{
local msg=$1
if [ "$__UTIL_LOG_LEVEL" -ge "1" ]; then
>&2 printf "\e[1;31m%s\e[m\n" "$msg"
fi
}
##
# Log a warning message
#
# $1: Message to log
##
util_log_warn()
{
local msg=$1
if [ "$__UTIL_LOG_LEVEL" -ge "2" ]; then
>&2 printf "\e[1;33m%s\e[m\n" "$msg"
fi
}
##
# Log a message
#
# $1: Message to log
##
util_log()
{
local msg=$1
if [ "$__UTIL_LOG_LEVEL" -ge "3" ]; then
>&2 printf "\e[1;34m%s\e[m\n" "$msg"
fi
}
##
# Log a debug message
#
# $1: Message to log
##
util_log_debug()
{
local msg=$1
local args=${*:2}
if [ "$__UTIL_LOG_LEVEL" -ge "4" ]; then
>&2 printf "\e[1;32m%s%s\e[m\n" "$msg" "$args"
fi
}
##
# Print the current function call trace to stdout
##
util_print_calltrace()
{
local i=0
while true; do
if [ ! "${FUNCNAME[$i]}" ]; then
break;
fi
>&2 echo "${BASH_SOURCE[$i]}:${FUNCNAME[$i]}:${BASH_LINENO[$i]}"
i=$((i + 1))
done
}
####################################
# "private" functions for cdepl, not to be called by the user
_util_check_bash_version()
{
if ! [[ "$(realpath /proc/$$/exe)" = *"bash"* ]] ; then
util_log_error_and_exit "Current shell not supported by deploy script, bash only"
fi
if [ -z "${BASH_VERSION}" ]; then
util_log_error_and_exit "Can't detect bash version, check \$BASH_VERSION"
fi
# Some features like "declare -A" require version 4
if [ "${BASH_VERSION%%[^0-9]*}" -lt 4 ]; then
versionCheck=$(echo "${BASH_VERSION%%[^0-9]* }" | awk -F '.' '{split($3, a, "("); print a[1]; print ($1 >= 3 && $2 > 2) ? "YES" : ($2 == 2 && a[1] >= 57) ? "YES" : "NO" }')
if [ "$versionCheck" == "NO" ]; then
util_log_error_and_exit "Bash version >= 3.2.57 required (Recommended is version 4)"
fi
fi
}