-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_lib.sh
executable file
·131 lines (107 loc) · 2.26 KB
/
_lib.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
#!/bin/bash
LIBSOURCED="TRUE"
# Reset
NC="\033[0m" # Text Reset
# Regular Colors
RED="\033[0;31m"
REDF="\033[1;31m"
REDB="\033[1;41m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
BCYAN="\033[1;36m"
BORANGE="\e[38;5;208m"
function output {
case "$1" in
SUCCESS)
printf "\e[32m%s%s\e[0m\n" "INFO : " "$2"
;;
INFO)
printf "\e[94m%s%s\e[0m\n" "INFO : " "$2"
;;
WARN)
printf "\e[33m%s%s\e[0m\n" "WARN : " "$2"
;;
WAIT)
printf "\e[33m%s%s\e[0m\n" "WAIT : " "$2"
;;
ERROR)
printf "\e[31m%s%s\e[0m\n" "ERROR: " "$2"
;;
FATAL)
printf "\e[41;97m%s%s\e[0m\n" "FATAL: " "$2"
;;
*)
printf "%s%s\n" " " "$1"
;;
esac
}
function echo_fatal() {
output "FATAL" "$1"
}
function echo_error() {
output "ERROR" "$1"
}
function echo_success() {
output "SUCCESS" "$1"
}
function echo_warning() {
output "WARN" "$1"
}
function echo_info() {
output "INFO" "$1"
}
function ask_with_yes_no() {
local question="${1}"
read -r -p "$(printf "%b" "${BORANGE}${question}${NC} (y/n) " ) " -n 1
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0 # Yes
else
return 1 # No
fi
}
function check_params() {
local do_exit=false
for var in "$@"; do
if [[ -z "${!var}" ]]; then
echo_error "$var is required but not configured"
local do_exit=true
fi
done
[[ ${do_exit} == false ]] || exit 2
}
function check_secret() {
local do_exit=false
for var in "$@"; do
if [[ -z "${!var}" ]]; then
echo_error "$var is required but not configured! Please use *.sec file"
local do_exit=true
fi
done
[[ ${do_exit} == false ]] || exit 2
}
function write_line_if_not_exists () {
local line=$1
local file=$2
local prefix=$3
if grep -qxF "$line" "$file" ; then
: # echo "$line exists in $file"
else
echo -e "${prefix}${line}" >> "$file"
fi
}
function render_template() {
local template="$1"
local output="$2"
awk '
{
while (match($0, /\$\{?[A-Za-z_][A-Za-z0-9_]*\}?/)) {
var = substr($0, RSTART+1, RLENGTH-1)
gsub(/[\{\}]/, "", var)
val = ENVIRON[var]
$0 = substr($0, 1, RSTART - 1) val substr($0, RSTART + RLENGTH)
}
print
}
' ${template} > ${output}
}