-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.sh
More file actions
167 lines (131 loc) · 3.21 KB
/
helper.sh
File metadata and controls
167 lines (131 loc) · 3.21 KB
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
#!/usr/bin/env bash
#
# Generic bash script helpers
#
. /etc/environment
COLOR_RED="$(tput -Txterm setaf 1)"
COLOR_GREEN="$(tput -Txterm setaf 2)"
COLOR_YELLOW="$(tput -Txterm setaf 3)"
COLOR_BLUE="$(tput -Txterm setaf 6)"
FONT_BOLD="$(tput -Txterm bold)"
FONT_RESET="$(tput -Txterm sgr0)"
MESSAGE_LOG_DIR='/var/log'
MESSAGE_TIMESTAMP=1
usage() {
local SCRIPT="${1}"
HELP_TEXT="$(awk '/^# Usage:/,/^$/ { sub(/^# ?/,"");if(i++)print s;s=$0; }' "${SCRIPT}")"
echo
output_message_format "${HELP_TEXT//\{SELF\}/${SCRIPT}}" 'green'
echo
}
output_to_log() {
local LOG_CONTENT="${1}"
if [ ! -d "${MESSAGE_LOG_DIR}" ]; then
mkdir -p "${MESSAGE_LOG_DIR}"
fi
if [[ -n "${OS_TYPE}" ]] && [[ "${OS_TYPE}" == 'mac' ]]; then
if [[ -n "${MESSAGE_LOG_FILE}" ]]; then
echo "${LOG_CONTENT}" >>"${MESSAGE_LOG_FILE}"
fi
else
if [[ -n "${MESSAGE_LOG_FILE}" ]]; then
echo "${LOG_CONTENT}" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" >>"${MESSAGE_LOG_FILE}"
fi
fi
}
output_date_format() {
if [ -n "${MESSAGE_TIMESTAMP}" ]; then
echo "[$(date +"%m-%d-%y %T")] "
fi
}
confirm() {
local CONFIRM_MESSAGE="${1}"
while true; do
read -r -p "${CONFIRM_MESSAGE} (yes/no) " ANSWER
output_to_log "${CONFIRM_MESSAGE} (${ANSWER})"
case ${ANSWER} in
[Yy]*)
break
;;
[Nn]*)
message_action ' - stopping'
exit
;;
*)
message_notice 'Please answer yes or no.'
;;
esac
done
}
output_message_format() {
local MESSAGE="${1}"
local COLOR="${2}"
local PREFIX="${3}"
local COLOR_FORMAT=
local PREFIX_TEXT=
case "${COLOR}" in
'yellow')
COLOR_FORMAT="${COLOR_YELLOW}"
;;
'green')
COLOR_FORMAT="${COLOR_GREEN}"
;;
'red')
COLOR_FORMAT="${FONT_BOLD}${COLOR_RED}"
;;
'blue')
COLOR_FORMAT="${COLOR_BLUE}"
;;
esac
if [[ -n "${PREFIX}" ]]; then
PREFIX_TEXT="[${PREFIX}] "
fi
echo "${COLOR_FORMAT}$(output_date_format)${PREFIX_TEXT}${MESSAGE}${FONT_RESET}"
}
message_action() {
local MESSAGE="${1}"
local OUTPUT_MESSAGE=
OUTPUT_MESSAGE="$(output_message_format "${MESSAGE}" 'yellow' 'action')"
output_to_log "${OUTPUT_MESSAGE}"
echo "${OUTPUT_MESSAGE}"
}
message_ok() {
local MESSAGE="${1}"
local OUTPUT_MESSAGE=
OUTPUT_MESSAGE="$(output_message_format "${MESSAGE}" 'green' 'ok')"
output_to_log "${OUTPUT_MESSAGE}"
echo "${OUTPUT_MESSAGE}"
}
message_notice() {
local MESSAGE="${1}"
local OUTPUT_MESSAGE=
OUTPUT_MESSAGE="$(output_message_format "${MESSAGE}" 'blue')"
output_to_log "${OUTPUT_MESSAGE}"
echo "${OUTPUT_MESSAGE}"
}
message() {
local MESSAGE="${1}"
local OUTPUT_MESSAGE=
OUTPUT_MESSAGE="$(output_message_format "${MESSAGE}")"
output_to_log "${OUTPUT_MESSAGE}"
echo "${OUTPUT_MESSAGE}"
}
message_error_notice() {
local MESSAGE="${1}"
local OUTPUT_MESSAGE=
OUTPUT_MESSAGE="$(output_message_format "${MESSAGE}" 'red' 'error')"
output_to_log "${OUTPUT_MESSAGE}"
echo "${OUTPUT_MESSAGE}" >&2
}
message_error() {
local MESSAGE="${1}"
message_error_notice "${MESSAGE}"
exit 1
}
check_argument() {
local KEY="${1}"
local VALUE="${2}"
if [[ -z "${VALUE}" ]]; then
message_error "${KEY} requires a value, current empty"
fi
}