-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathask.lib.sh
313 lines (275 loc) · 12.1 KB
/
ask.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#
# Copyright (c) 2010-2011 Linagora
# Patrick Guiran <pguiran@linagora.com>
# http://github.com/Tauop/ScriptHelper
#
# ScriptHelper 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 2 of
# the License, or (at your option) any later version.
#
# ScriptHelper 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 Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# README ---------------------------------------------------------------------
# This is a bash library purpose is to give ability to ask question, which
# checking answer, in the simpliest way
#
# Global variables ===========================================================
# IMPORTANT: Please to write to those variables
# __LIB_ASK__ : 'Loaded' when the lib is 'source'd
# __ANSWER_LOG_FILE__ : filepath of the file in which user's answer, get with
# ASK(), will be recorded.
# __AUTOANSWER_FILE__ : filepath of the file from which answer will be taken
# __AUTOANSWER_FP__ : file pointer, which determine the last line number of
# the file __AUTOANSWER_FILE__ which has been read
# ----------------------------------------------------------------------------
# don't load several times this file
if [ "${__LIB_ASK__:-}" != 'Loaded' ]; then
__LIB_ASK__='Loaded'
# IMPORTANT: Don't set those variables directly in the parent script
__AUTOANSWER_FILE__=''
__AUTOANSWER_FP__=0
__ANSWER_LOG_FILE__=''
__USE_READLINE__='false'
# Load dependencies
load() {
local var= value= file=
var="$1"; file="$2"
value=$( eval "printf \"%s\" \"\${${var}:-}\"" )
[ -n "${value}" ] && return 1;
if [ -f "${file}" ]; then
. "${file}"
else
printf "ERROR: Unable to load ${file}"
exit 2
fi
return 0;
}
# Load configuration file
load SCRIPT_HELPER_DIRECTORY /etc/ScriptHelper.conf
SCRIPT_HELPER_DIRECTORY="${SCRIPT_HELPER_DIRECTORY:-}"
SCRIPT_HELPER_DIRECTORY="${SCRIPT_HELPER_DIRECTORY%%/}"
load __LIB_MESSAGE__ "${SCRIPT_HELPER_DIRECTORY}/message.lib.sh"
# ----------------------------------------------------------------------------
# usage: HIT_TO_CONTINUE
# desc: display a message to the user, which ask to press ENTER to continue
HIT_TO_CONTINUE () {
if [ ! -f "${__AUTOANSWER_FILE__}" ]; then
MESSAGE --no-log ""
MESSAGE --no-log "Press ENTER to continue, or CTRL+C to exit"
MESSAGE --no-log ""
read
else
MESSAGE --no-log ''
fi
LOG "User press ENTER to continue"
}
# ----------------------------------------------------------------------------
# usage: ASK_SET_AUTOANSWER_FILE <file>
# desc: set a file which contains previously recorded user's answers,
# ie a file which contain a answer per line. Each line of this
# file will be returned at each call of ASK().
# arguments:
# <file> : path to the file from which answers will be read
# note: Call of HIT_TO_CONTINUE() will have no interactive effect, if
# ASK_SET_AUTOANSWER_FILE has been used.
ASK_SET_AUTOANSWER_FILE () {
[ -z "$1" ] && KO "ASK_SET_ANSWER_FILE is called without argument !"
[ $# -gt 1 ] && KO "ASK_SET_ANSWER_FILE: too much arguments !"
__AUTOANSWER_FILE__="$1"
__AUTOANSWER_FP__=0
if [ ! -r "${__AUTOANSWER_FILE__}" ]; then
__AUTOANSWER_FILE__=''
KO "${__AUTOANSWER_FILE__} can't be read"
fi
}
# ----------------------------------------------------------------------------
# usage: ASK_SET_ANSWER_LOG_FILE <file>
# desc: create a log file, which will be used to store all user answers,
# one per line
# arguments:
# <file> : path to the file in which to log answers.
# note: the <file> will be deleted before use
ASK_SET_ANSWER_LOG_FILE () {
[ -z "$1" ] && KO "ASK_SET_ANSWER_FILE is called without argument !"
[ $# -gt 1 ] && KO "ASK_SET_ANSWER_FILE: too much arguments !"
__ANSWER_LOG_FILE__="$1"
if [ -f "${__ANSWER_LOG_FILE__}" ]; then
rm -f "${__ANSWER_LOG_FILE__}" \
|| KO "Unable to delete existing answer log file '${__ANSWER_LOG_FILE__}'."
fi
touch "${__ANSWER_LOG_FILE__}" \
|| KO "Unable to create answer log file '${__ANSWER_LOG_FILE__}'."
}
# ----------------------------------------------------------------------------
# usage: ASK_ENABLE_READLINE [ <options> ]
# desc: enable readline module, used by read shell builtin, if we are in zsh or bash
# options: --force : force usage of read -e
# --history-file : set history file to use
# note: if --history-file is not specified, history builtin will use HISTFILE env var
ASK_ENABLE_READLINE () {
local shell=${SHELL##*/} do_force='false'
while true; do
[ $# -eq 0 ] && break
case "$1" in
--force ) do_force='true'; shift ;;
--history-file ) shift; [ $# -ge 1 ] && HISTFILE="$1"; shift ;;
--* ) shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ "${do_force}" = 'true' \
-o "${shell}" = 'bash' \
-o "${shell}" = 'zsh' ]; then
set +o emacs
set +o history
history -r
__USE_READLINE__='true'
fi
}
# usage: ASK_DISABLE_READLINE
# desc: disable readline module, used by read shell builtin
ASK_DISABLE_READLINE () {
set -o emacs
set -o history
__USE_READLINE__='false'
}
# ----------------------------------------------------------------------------
# usage: ASK [ <options> ] <variable> [ "<text>" ] [ <default value> ] [ "<error>" ]
# desc: Ask a question to the user, get the user response and store it in
# the variable which name is stored in <variable>.
# Control can be made on user answer, and ASK() repeat question if
# the user answer is not valid.
# Display message and user answer are logged, if possible.
# options: --no-print : all call of MESSAGE() won't print anything
# --no-echo : Don't print what the user type
# --with-break : make a break-line after the question printing.
# --pass : implies --no-print + don't log clear text password
# --number : The user answer must be a number
# --yesno : The asked question is a yes/no question.
# Control the user answer.
# --allow-empty : user can hit enter, which reply to the question.
# In this case, the answer will be a empty string
# arguments: <variable> = The name of the variable in which we have to store
# the user response.
# <text> = The question to ask to the user.
# <default value> = value of the answer of the user, when he only
# press ENTER. Set this variable to empty string
# when you don't want default value.
# <error> = The custom error message displayed to the user if its
# answer is not valid. default: "Invalid answer."
# note: format options are ignored if a __AUTOANSWER_FILE__ was set
ASK () {
local question= variable= default= error= cbreak='\n'
local answer= read_opt='' check='' allow_empty= message_opt=
local do_break='false' do_pass='false' no_print='false' no_echo='false'
# parse argument
while true; do
[ $# -eq 0 ] && break
case "$1" in
# display options
--no-print ) shift; no_print='true' ;;
--no-echo ) shift; no_echo='true' ;;
--with-break ) shift; do_break='true' ;;
--pass ) shift; do_pass='true' ;;
# answer format options
--number ) shift; check='number' ;;
--yesno ) shift; check='yesno' ;;
--allow-empty ) shift; allow_empty='true' ;;
--* ) shift ;; # ignore
* ) break ;;
esac
done
# interprete some options
[ "${do_break}" = 'false' ] && message_opt="${message_opt} --no-break "
[ "${no_print}" = 'true' ] && message_opt="${message_opt} --no-print"
[ "${no_echo}" = 'true' -o "${do_pass}" = 'true' ] && read_opt="${read_opt} -s "
[ "${__USE_READLINE__}" = 'true' ] && read_opt="${read_opt} -e "
# parse trailing arguments
# note: the while is just a workaround, as bash has no GOTO statement
while true ; do
[ $# -gt 0 ] && variable="$1" || FATAL "ASK: Missing argument (question)"
[ $# -gt 1 ] && question="$2" || break
[ $# -gt 2 ] && default="$3" || break
[ $# -gt 3 ] && error="$4" || break
break
done
[ -n "${question}" -a "${question}" = "${question% }" ] && question="${question} "
# reset global variable
eval "${variable}=''"
if [ -f "${__AUTOANSWER_FILE__}" ]; then
# automatic mode
MESSAGE --no-log ${message_opt} "${question}"
[ "${do_break}" = 'true' ] && MESSAGE ${message_opt} --no-log --no-break $''
__AUTOANSWER_FP__=$((__AUTOANSWER_FP__ + 1 ))
answer=$( sed -n "${__AUTOANSWER_FP__}p" "${__AUTOANSWER_FILE__}" )
[ -z "${answer}" -a -n "${default}" ] && answer="${default}"
[ "${no_echo}" = 'false' -a "${do_pass}" = 'false' ] && MESSAGE --no-break --no-log "${answer}"
[ "${no_print}" = 'false' ] && BR
else
# interactive mode
if [ "${no_print}" = 'false' ]; then
question="${__MSG_INDENT__}${question}"
[ "${do_break}" = 'true' ] && question=${question}$'\n'${__MSG_INDENT__}
question=$( echo "${question}" | sed -e 's/\\/\\\\/g;s/\"/\\\"/g;s/\$/\\\$/' )
read_opt="${read_opt} -p \"${question}\" "
fi
while eval "read ${read_opt} answer"; do
# deal with default, when user only press ENTER
if [ -z "${answer}" ]; then
if [ -n "${default}" ]; then
answer="${default}"
break;
fi
[ "${allow_empty}" = 'true' ] && break;
else
# delete useless space
answer=$( printf "%s" "${answer}" | sed -e 's/^ *//;s/ *$//;' )
# check user response
case "${check}" in
"yesno" )
answer=$( printf "%s" "${answer}" | tr '[:lower:]' '[:upper:]' )
case "${answer}" in
'Y'|'YES') answer='Y'; break;;
'N'|'NO') answer='N'; break ;;
*) ;;
esac
;; # enf of "yesno"
"number" )
printf "%s" "${answer}" | grep '^[0-9]*$' >/dev/null 2>/dev/null
[ $? -eq 0 ] && break;
;; # end of "number"
* ) break ;;
esac
fi
# NOTE: with --pass, no \n is printed out to the STDOUT, due to '-s' option of 'read'
[ \( "${no_echo}" = 'true' -o "${do_pass}" = 'true' \) -a "${no_print}" = 'false' ] && BR
# display error
# FIXME: Ugly. Don't use ERROR() alias, as it doesn't support --no-log options :/
if [ -n "${error}" ]; then
MESSAGE --no-log --no-indent "${__MSG_INDENT__}ERROR: ${error}"
else
MESSAGE --no-log --no-indent "${__MSG_INDENT__}ERROR: invalid answer"
fi
done # enf of while read
fi
if [ "${do_pass}" = 'true' ]; then
LOG "${question} => $( echo "${answer}" | sed -e 's/./#/g' )"
else
LOG "${question} => ${answer}"
[ "${__USE_READLINE__}" = 'true' ] && history -s -- "${answer}"
fi
# NOTE: with --pass, no \n is printed out to the STDOUT, due to '-s' option of 'read'
[ \( "${no_echo}" = 'true' -o "${do_pass}" = 'true' \) -a "${no_print}" = 'false' ] && BR
[ -n "${__ANSWER_LOG_FILE__}" ] && printf "%s\n" "${answer}" >> "${__ANSWER_LOG_FILE__}"
answer=$( printf "%s" "${answer}" | sed -e 's/["]/\\"/g' )
eval "${variable}=\"${answer}\"";
}
fi # enf of: if [ "${__LIB_ASK__}" != 'Loaded' ]; then