-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathask
114 lines (95 loc) · 3.42 KB
/
ask
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
#!/bin/bash
# ask for user input interactively
# (@todo either on image bootstrap or on first boot)
# @example
# ask user for current mood and store answer in RPI_MOOD variable, default answer is preset
# RPI_ASK_QUESTION0="How are you?"
# RPI_ASK_VARIABLE0="RPI_MOOD"
# RPI_ASK_DEFAULT0="I'm fine!"
# @example
# ask user for mood but use dialog (TUI) instead of read (CLI)
# RPI_ASK_QUESTION0="How are you?"
# RPI_ASK_VARIABLE0="RPI_MOOD"
# RPI_ASK_METHOD="dialog"
# @example
# ask user for mood and pass custom parameters to zenity
# RPI_ASK_QUESTION0="How are you?"
# RPI_ASK_VARIABLE0="RPI_MOOD"
# RPI_ASK_ARGUMENTS0="--entry,--entry-text=totally\ fine,--title=the\ big\ questions..."
# @todo
# RPI_ASK_METHOD="${RPI_ASK_METHOD:=read}"
RPI_ASK_ON="${RPI_ASK_ON:=bootstrap}"
function rpi_ask_prerun() {
[[ -n "${RPI_ASK_QUESTION0}" ]] || error "RPI_ASK_QUESTION0 not set"
[[ -n "${RPI_ASK_VARIABLE0}" ]] || error "RPI_ASK_VARIABLE0 not set"
return 0
}
function rpi_ask_run() {
local i=0
local destvar
local argsvar
local defaultvar
local questionvar="RPI_ASK_QUESTION0"
while [[ -n "${!questionvar+x}" ]] ; do
destvar="RPI_ASK_VARIABLE${i}"
argsvar="RPI_ASK_ARGUMENTS${i}"
defaultvar="RPI_ASK_DEFAULT${i}"
ask "${!questionvar}" "${!defaultvar}" "${argsvar}"
# export target variable
log "setting: ${!destvar}=\"${answer}\""
export "${!destvar}=\"${answer}\""
# next question
i=$((i+1))
questionvar="RPI_ASK_QUESTION${i}"
done
}
function rpi_ask_description() {
echo "ask for input"
}
function rpi_ask_help_params() {
help_param "RPI_ASK_METHOD" "the asking method (One of: read, dialog, zenity)"
# @todo
#help_param "RPI_ASK_ON" "when to ask question: \"bootstrap\" or \"boot\""
help_param "RPI_ASK_QUESTIONn" "question number n (starting from 0)"
help_param "RPI_ASK_DEFAULTn" "preset answer to question n"
help_param "RPI_ASK_VARIABLEn" "name of variable to store answer to question n"
help_param "RPI_ASK_ARGUMENTSn" "method specific arguments to pass to method (s. manual of specific method for possible arguments)"
}
# ---------------------------------------------------------------------
function ask() {
# receive question, default answer and cmdline arguments and
# put results into $answer
local question="$1"
local default_answer="$2"
local argsvar="$3"
local args
declare -n args="${argsvar}"
commarray "${argsvar}"
case "${RPI_ASK_METHOD}" in
"read")
echo -n "${question} : "
read -r answer
;;
"dialog")
[[ -n "${args[*]}" ]] || args=("--inputbox" "${question}" "0" "0")
[[ -z "${default_answer}" ]] || args+=("${default_answer}")
# open fd 3 for dialog to output
exec 3>&1
answer=$(dialog "${args[@]}" 2>&1 1>&3)
exitcode=$?
# close fd 3
exec 3>&-
# clear screen
clear
[[ ${exitcode} == 0 ]] || error "dialog aborted or"
;;
"zenity")
[[ -n "${args[*]}" ]] || args=("--entry")
[[ -z "${default_answer}" ]] || args+=("--entry-text" "${default_answer}")
answer=$(zenity "${args[@]}" --text "$1") || error "zenity aborted or"
;;
?|*)
error "unknown RPI_ASK_METHOD: \"${OPTARG}\""
;;
esac
}