-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_reef-pi
More file actions
436 lines (387 loc) · 10.7 KB
/
Copy path_reef-pi
File metadata and controls
436 lines (387 loc) · 10.7 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/bin/bash
###############################################################
# Bash completion script for reef-pi command line function
# it should be installed in
# /usr/share/bash-completion/completions/_reef-pi
# so that it's available in all bash terminal shells
###############################################################
_set_option_variables() {
TOP_LEVEL_OPTIONS=("db daemon restore-db reset-password install --help -version")
DB_OPTIONS="buckets list show create update delete --help"
DAEMON_OPTIONS=("-config" "--help")
RESET_PASSWORD_OPTIONS=("-user" "-password" "-config" "--help")
RESTORE_DB_OPTIONS=("-backup" "-current" "-new" "--help")
RESTORE_DB_BACKUP_OPTIONS=("/var/lib/reef-pi/reef-pi.db.old")
RESTORE_DB_CURRENT_OPTIONS=("/var/lib/reef-pi/reef-pi.db")
RESTORE_DB_NEW_OPTIONS=("/var/lib/reef-pi/reef-pi.db.new")
INSTALL_OPTIONS=("-version")
INPUT_OPTION="-input"
OUTPUT_OPTION="-output"
STORE_OPTION="-output"
REEF_PI_LIST_ITEMS_CMD="sudo reef-pi db list "
REEF_PI_LIST_REEF_PI_CMD="sudo reef-pi db list reef-pi "
REEF_PI_BUCKETS_CMD="sudo reef-pi db buckets "
REEF_PI_VERSION_CMD="sudo reef-pi -version "
REEF_PI_CONFIG_PATH="/etc/reef-pi/reef-pi.yml"
USER_ENTRY=0
PASSWORD_ENTRY=1
}
# helper logging method
_e() { echo "$1" >> log; }
_get_buckets()
{
BUCKETS=("$(${REEF_PI_BUCKETS_CMD} | tr '\n' ' ')")
}
_parse_reef_pi_json_for_user_pwd() {
PYTHON_CODE_TO_EXTRACT_USER_PWD=$(cat <<END_PY_USER
import json
import sys
stdin_str = ""
for line in sys.stdin:
stdin_str += line
resp = json.loads(stdin_str)
print("%s"%(resp['credentials']['user']))
print("%s"%(resp['credentials']['password']))
END_PY_USER
)
USER_PWD_SUGGESTION="$(echo ${REEF_PI_JSON} | python3 -c "$PYTHON_CODE_TO_EXTRACT_USER_PWD" )"
}
_get_user_pwd_suggestion_array()
{
local USER_PWD_SUGGESTION
_parse_reef_pi_json_for_user_pwd
local IFS=$'\n' # Change IFS to new line - convert new lines to array
SUGGESTIONS=($USER_PWD_SUGGESTION)
}
_parse_reef_pi_json_for_user()
{
local SUGGESTIONS
_get_user_pwd_suggestion_array
USER_SUGGESTION=${SUGGESTIONS[USER_ENTRY]}
}
_parse_reef_pi_json_for_password()
{
local SUGGESTIONS
_get_user_pwd_suggestion_array
PASSWORD_SUGGESTION=${SUGGESTIONS[PASSWORD_ENTRY]}
}
_parse_reef_pi_json_for_items() {
PYTHON_CODE_TO_EXTRACT_ID_AND_NAME=$(cat <<END_PY_ITEM
import json
import sys
stdin_str = ""
for line in sys.stdin:
stdin_str += line
resp = json.loads(stdin_str)
for key in resp:
print("%s <%s>"%(resp[key]['id'],resp[key]['name']))
END_PY_ITEM
)
CONFIGURED_ITEM_SUGGESTIONS="$(echo ${CONFIGURED_ITEMS_JSON} | python3 -c "$PYTHON_CODE_TO_EXTRACT_ID_AND_NAME" )"
}
_get_item_type()
{
case ${ITEM} in
ph_calibration | ph_readings) #convert the oddball ph entries
ITEM_TYPE="phprobes"
;;
reef-pi | system | errors) #buckets without items
ITEM_TYPE=""
;;
*)
ITEM_TYPE=$ITEM
;;
esac
ITEM_TYPE=${ITEM_TYPE%_*} #truncate after underscore
}
_get_reef_pi_configured_items()
{
local CONFIGURED_ITEMS_JSON
if [[ ! -z "${ITEM_TYPE}" ]]; then
CONFIGURED_ITEMS_JSON="$($REEF_PI_LIST_ITEMS_CMD $ITEM_TYPE)"
_parse_reef_pi_json_for_items
fi
}
_get_configured_item_suggestions()
{
local CONFIGURED_ITEM_SUGGESTIONS
_get_reef_pi_configured_items
local IFS=$'\n' # Change IFS to new line - convert new lines to array
local suggestions=($CONFIGURED_ITEM_SUGGESTIONS)
if [ "${#suggestions[@]}" == "1" ]; then #If array is size one, then get the leading number
local number="${suggestions[0]/%\ */}"
COMPREPLY=("$number")
else
for i in "${!suggestions[@]}"; do # pad entries to screen width for 1 option per line
suggestions[$i]="$(printf '%*s' "-$COLUMNS" "${suggestions[$i]}")"
done
COMPREPLY=("${suggestions[@]}")
fi
}
_get_item_options()
{
local ITEM_TYPE ITEM
ITEM=${PREV}
_get_item_type
_get_configured_item_suggestions
}
_list_buckets()
{
_get_buckets
COMPREPLY=($(compgen -W "$BUCKETS" -- ${CUR}))
}
_db_list_handler()
{
case ${COMP_CWORD} in
3) # completing 3rd term
_list_buckets
;;
*)
COMPREPLY=()
;;
esac
}
_db_create_handler()
{
case ${COMP_CWORD} in
3) # completing 3rd term
_list_buckets
;;
4) # completing 4th term
COMPREPLY=($(compgen -W "$INPUT_OPTION" -- ${CUR}))
;;
5) # completing 5th term
COMPREPLY=($(compgen -f -- "${CUR}")) #files for input
;;
*)
COMPREPLY=()
;;
esac
}
_bucket_item_handler()
{
case ${COMP_CWORD} in
3) # completing 3rd term
_list_buckets
;;
4) # completing 4th term
_get_item_options
;;
*)
COMPREPLY=()
;;
esac
}
_db_delete_handler()
{
_bucket_item_handler
}
_db_show_handler()
{
_bucket_item_handler
}
_db_update_handler()
{
case ${COMP_CWORD} in
3) # completing 3rd term
_list_buckets
;;
4) # completing 4th term
_get_item_options
;;
5) # completing 5th term
COMPREPLY=($(compgen -W "$INPUT_OPTION" -- ${CUR}))
;;
6) # completing 6th term
COMPREPLY=($(compgen -f -- "${CUR}")) #files for input
;;
*)
COMPREPLY=()
;;
esac
}
_db_handler()
{
local action
if [[ "${#COMP_WORDS[@]}" -gt "3" ]]; then
action=${COMP_WORDS[2]}
fi
case ${COMP_CWORD} in
2) # two arguments
COMPREPLY=($(compgen -W "$DB_OPTIONS" -- ${CUR})) ;;
3 | 4 | 5 | 6 ) # three or more
case ${action} in
buckets) return ;;
list) _db_list_handler ;;
show) _db_show_handler ;;
create) _db_create_handler ;;
delete) _db_delete_handler ;;
update) _db_update_handler ;;
esac
;;
*)
COMPREPLY=()
;;
esac
}
_get_reef_pi_user()
{
local USER_SUGGESTION
local REEF_PI_JSON="$($REEF_PI_LIST_REEF_PI_CMD)"
_parse_reef_pi_json_for_user
COMPREPLY=($(compgen -W "$USER_SUGGESTION" -- ${CUR}))
}
_get_reef_pi_password()
{
local PASSWORD_SUGGESTION
local REEF_PI_JSON="$($REEF_PI_LIST_REEF_PI_CMD)"
_parse_reef_pi_json_for_password
COMPREPLY=($(compgen -W "$PASSWORD_SUGGESTION" -- ${CUR}))
}
_remove_elements_from_array()
{
declare -n source_array="$1"
for target in "${ELEMENTS_TO_REMOVE[@]}"; do
for i in "${!source_array[@]}"; do
if [[ $target = ${source_array[i]} ]]; then
unset 'source_array[i]'
fi
done
done
}
_build_used_arguments_in_current_command_line()
{
declare -n options_array="$1"
# iterate throught the existing command line and remove options already used
for argument in ${COMP_WORDS[*]}; do
for i in "${!options_array[@]}"; do
if [[ ${options_array[i]} = ${argument} ]]; then
ELEMENTS_TO_REMOVE+=(${argument})
fi
done
done
}
_get_valid_options()
{
declare -n options="$1"
local ELEMENTS_TO_REMOVE=()
_build_used_arguments_in_current_command_line options
_remove_elements_from_array options
}
_reset_password_user_handler()
{
case ${PREV} in
-user) _get_reef_pi_user ;;
*) COMPREPLY=() ;;
esac
}
_reset_password_password_handler()
{
case ${PREV} in
-password) _get_reef_pi_password ;;
*) COMPREPLY=() ;;
esac
}
_reset_password_config_handler()
{
case ${PREV} in
-config) COMPREPLY=($(compgen -f -- "/etc/reef-pi/"; compgen -f -- ${CUR})) ;;
*) COMPREPLY=() ;;
esac
}
_reset_password_handler()
{
case ${PREV} in
-user) _reset_password_user_handler ;;
-password) _reset_password_password_handler ;;
-config) _reset_password_config_handler ;;
*)
_get_valid_options RESET_PASSWORD_OPTIONS
COMPREPLY=($(compgen -W "${RESET_PASSWORD_OPTIONS[*]}" -- ${CUR}))
;;
esac
}
_restore_db_backup_handler()
{
case ${PREV} in
-backup) COMPREPLY=($(compgen -W "${RESTORE_DB_BACKUP_OPTIONS}" -- ${CUR})) ;;
*) COMPREPLY=() ;;
esac
}
_restore_db_current_handler()
{
case ${PREV} in
-current) COMPREPLY=($(compgen -W "${RESTORE_DB_CURRENT_OPTIONS}" -- ${CUR})) ;;
*) COMPREPLY=() ;;
esac
}
_restore_db_new_handler()
{
case ${PREV} in
-new) COMPREPLY=($(compgen -W "${RESTORE_DB_NEW_OPTIONS}" -- ${CUR})) ;;
*) COMPREPLY=() ;;
esac
}
_restore_db_handler()
{
case ${PREV} in
-backup) _restore_db_backup_handler ;;
-current) _restore_db_current_handler ;;
-new) _restore_db_new_handler ;;
*)
_get_valid_options RESTORE_DB_OPTIONS
COMPREPLY=($(compgen -W "${RESTORE_DB_OPTIONS[*]}" -- ${CUR}))
;;
esac
}
_install_handler()
{
case ${PREV} in
-version)
local current_version=$($REEF_PI_VERSION_CMD)
COMPREPLY=($(compgen -W "${current_version}" -- ${CUR}))
;;
*)
_get_valid_options INSTALL_OPTIONS
COMPREPLY=($(compgen -W "${INSTALL_OPTIONS[*]}" -- ${CUR}))
;;
esac
}
_daemon_handler()
{
case ${PREV} in
-config)
COMPREPLY=($(compgen -W "${REEF_PI_CONFIG_PATH}" -- ${CUR}))
;;
*)
_get_valid_options DAEMON_OPTIONS
COMPREPLY=($(compgen -W "${DAEMON_OPTIONS[*]}" -- ${CUR}))
;;
esac
}
_reef-pi_completions()
{
_set_option_variables
local CUR PREV category
CUR=${COMP_WORDS[COMP_CWORD]}
PREV=${COMP_WORDS[COMP_CWORD-1]}
case ${COMP_CWORD} in
1) # one argument
COMPREPLY=($(compgen -W "$TOP_LEVEL_OPTIONS" -- ${CUR}))
;;
2 | 3 | 4 | 5 | 6 | 7 ) # two or more arguments
category=${COMP_WORDS[1]}
case ${category} in
daemon) _daemon_handler ;;
db) _db_handler ;;
install) _install_handler ;;
reset-password) _reset_password_handler ;;
restore-db) _restore_db_handler ;;
esac
;;
*)
COMPREPLY=()
;;
esac
}
complete -F _reef-pi_completions reef-pi