-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconfig_interface.sh
491 lines (442 loc) · 12.9 KB
/
config_interface.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# Start of config ng interface
module_options+=(
["set_colors,author"]="@Tearran"
["set_colors,ref_link"]=""
["set_colors,feature"]="set_colors"
["set_colors,desc"]="Change the background color of the terminal or dialog box"
["set_colors,example"]="set_colors 0-7"
["set_colors,doc_link"]=""
["set_colors,status"]="Active"
)
#
# Function to set the tui colors
#
function set_colors() {
local color_code=$1
if [ "$DIALOG" = "whiptail" ]; then
set_newt_colors "$color_code"
#echo "color code: $color_code" | show_infobox ;
elif [ "$DIALOG" = "dialog" ]; then
set_term_colors "$color_code"
else
echo "Invalid dialog type"
return 1
fi
}
#
# Function to set the colors for newt
#
function set_newt_colors() {
local color_code=$1
case $color_code in
0) color="black" ;;
1) color="red" ;;
2) color="green" ;;
3) color="yellow" ;;
4) color="blue" ;;
5) color="magenta" ;;
6) color="cyan" ;;
7) color="white" ;;
8) color="black" ;;
9) color="red" ;;
*) return ;;
esac
export NEWT_COLORS="root=,$color"
}
#
# Function to set the colors for terminal
#
function set_term_colors() {
local color_code=$1
case $color_code in
0) color="\e[40m" ;; # black
1) color="\e[41m" ;; # red
2) color="\e[42m" ;; # green
3) color="\e[43m" ;; # yellow
4) color="\e[44m" ;; # blue
5) color="\e[45m" ;; # magenta
6) color="\e[46m" ;; # cyan
7) color="\e[47m" ;; # white
*)
echo "Invalid color code"
return 1
;;
esac
echo -e "$color"
}
#
# Function to reset the colors
#
function reset_colors() {
echo -e "\e[0m"
}
module_options+=(
["parse_menu_items,author"]="@viraniac"
["parse_menu_items,ref_link"]=""
["parse_menu_items,feature"]="parse_menu_items"
["parse_menu_items,desc"]="Parse json to get list of desired menu or submenu items"
["parse_menu_items,example"]="parse_menu_items 'menu_options_array'"
["parse_menu_items,doc_link"]=""
["parse_menu_items,status"]="Active"
)
#
# Function to parse the menu items
#
parse_menu_items() {
local -n options=$1
while IFS= read -r id; do
IFS= read -r description
IFS= read -r condition
# If the condition field is not empty and not null, run the function specified in the condition
if [[ -n $condition && $condition != "null" ]]; then
# If the function returns a truthy value, add the menu item to the menu
if eval $condition; then
options+=("$id" " - $description")
fi
else
# If the condition field is empty or null, add the menu item to the menu
options+=("$id" " - $description ")
fi
done < <(echo "$json_data" | jq -r '.menu[] | '${parent_id:+".. | objects | select(.id==\"$parent_id\") | .sub[]? |"}' select(.status != "Disabled") | "\(.id)\n\(.description)\n\(.condition)"' || exit 1)
}
module_options+=(
["generate_top_menu,author"]="@Tearran"
["generate_top_menu,ref_link"]=""
["generate_top_menu,feature"]="generate_top_menu"
["generate_top_menu,desc"]="Build the main menu from a object"
["generate_top_menu,example"]="generate_top_menu 'json_data'"
["generate_top_menu,status"]="Active"
)
#
# Function to generate the main menu from a JSON object
#
generate_top_menu() {
local json_data="$1"
local status="$ARMBIAN $KERNELID ($DISTRO $DISTROID)"
local backtitle="$BACKTITLE"
while true; do
local menu_options=()
parse_menu_items menu_options
local OPTION=$($DIALOG --backtitle "$backtitle" --title "$TITLE" --menu "$status" 0 80 9 "${menu_options[@]}" \
--ok-button Select --cancel-button Exit 3>&1 1>&2 2>&3)
local exitstatus=$?
if [ $exitstatus = 0 ]; then
[ -z "$OPTION" ] && break
[[ -n "$debug" ]] && echo "$OPTION"
generate_menu "$OPTION"
fi
done
}
module_options+=(
["generate_menu,author"]="@Tearran"
["generate_menu,ref_link"]=""
["generate_menu,feature"]="generate_menu"
["generate_menu,desc"]="Generate a submenu from a parent_id"
["generate_menu,example"]="generate_menu 'parent_id'"
["generate_menu,status"]="Active"
)
#
# Function to generate the submenu
#
function generate_menu() {
local parent_id="$1"
local top_parent_id="$2"
local backtitle="$BACKTITLE"
local status=""
while true; do
# Get the submenu options for the current parent_id
local submenu_options=()
parse_menu_items submenu_options
local OPTION=$($DIALOG --backtitle "$BACKTITLE" --title "$top_parent_id $parent_id" --menu "$status" 0 80 9 "${submenu_options[@]}" \
--ok-button Select --cancel-button Back 3>&1 1>&2 2>&3)
local exitstatus=$?
if [ $exitstatus = 0 ]; then
[ -z "$OPTION" ] && break
# Check if the selected option has a submenu
local submenu_count=$(jq -r --arg id "$OPTION" '.menu[] | .. | objects | select(.id==$id) | .sub? | length' "$json_file")
submenu_count=${submenu_count:-0} # If submenu_count is null or empty, set it to 0
if [ "$submenu_count" -gt 0 ]; then
# If it does, generate a new menu for the submenu
[[ -n "$debug" ]] && echo "$OPTION"
generate_menu "$OPTION" "$parent_id"
else
# If it doesn't, execute the command
[[ -n "$debug" ]] && echo "$OPTION"
execute_command "$OPTION"
fi
fi
done
}
module_options+=(
["execute_command,author"]="@Tearran"
["execute_command,ref_link"]=""
["execute_command,feature"]="execute_command"
["execute_command,desc"]="Needed by generate_menu"
["execute_command,example"]="execute_command 'id'"
["execute_command,doc_link"]=""
["execute_command,status"]="Active"
)
#
# Function to execute the command
#
function execute_command() {
local id=$1
# Extract commands
local commands=$(jq -r --arg id "$id" '
.menu[] |
.. |
objects |
select(.id == $id) |
.command[]?' "$json_file")
# Check if a about exists
local about=$(jq -r --arg id "$id" '
.menu[] |
.. |
objects |
select(.id == $id) |
.about?' "$json_file")
# If a about exists, display it and wait for user confirmation
if [[ "$about" != "null" && $INPUTMODE != "cmd" ]]; then
get_user_continue "$about\nWould you like to continue?" process_input
fi
# Execute each command
for command in "${commands[@]}"; do
[[ -n "$debug" ]] && echo "$command"
eval "$command"
done
}
module_options+=(
["show_message,author"]="@Tearran"
["show_message,ref_link"]=""
["show_message,feature"]="show_message"
["show_message,desc"]="Display a message box"
["show_message,example"]="show_message <<< 'hello world' "
["show_message,doc_link"]=""
["show_message,status"]="Active"
)
#
# Function to display a message box
#
function show_message() {
# Read the input from the pipe
input=$(cat)
# Display the "OK" message box with the input data
if [[ $DIALOG != "bash" ]]; then
$DIALOG --title "$TITLE" --msgbox "$input" 0 0
else
echo -e "$input"
read -p -r "Press [Enter] to continue..."
fi
}
module_options+=(
["show_infobox,author"]="@Tearran"
["show_infobox,ref_link"]=""
["show_infobox,feature"]="show_infobox"
["show_infobox,desc"]="pipeline strings to an infobox "
["show_infobox,example"]="show_infobox <<< 'hello world' ; "
["show_infobox,doc_link"]=""
["show_infobox,status"]="Active"
)
#
# Function to display an infobox with a message
#
function show_infobox() {
export TERM=ansi
local input
local BACKTITLE="$BACKTITLE"
local -a buffer # Declare buffer as an array
if [ -p /dev/stdin ]; then
while IFS= read -r line; do
buffer+=("$line") # Add the line to the buffer
# If the buffer has more than 10 lines, remove the oldest line
if ((${#buffer[@]} > 18)); then
buffer=("${buffer[@]:1}")
fi
# Display the lines in the buffer in the infobox
TERM=ansi $DIALOG --title "$TITLE" --infobox "$(printf "%s\n" "${buffer[@]}")" 16 90
sleep 0.5
done
else
input="$1"
TERM=ansi $DIALOG --title "$TITLE" --infobox "$input" 6 80
fi
echo -ne '\033[3J' # clear the screen
}
module_options+=(
["show_menu,author"]="@Tearran"
["show_menu,ref_link"]=""
["show_menu,feature"]="show_menu"
["show_menu,desc"]="Display a menu from pipe"
["show_menu,example"]="show_menu <<< armbianmonitor -h ; "
["show_menu,doc_link"]=""
["show_menu,status"]="Active"
)
#
#
#
show_menu() {
# Get the input and convert it into an array of options
inpu_raw=$(cat)
# Remove the lines before -h
input=$(echo "$inpu_raw" | sed 's/-\([a-zA-Z]\)/\1/' | grep '^ [a-zA-Z] ' | grep -v '\[')
options=()
while read -r line; do
package=$(echo "$line" | awk '{print $1}')
description=$(echo "$line" | awk '{$1=""; print $0}' | sed 's/^ *//')
options+=("$package" "$description")
done <<< "$input"
# Display the menu and get the user's choice
[[ $DIALOG != "bash" ]] && choice=$($DIALOG --title "$TITLE" --menu "Choose an option:" 0 0 9 "${options[@]}" 3>&1 1>&2 2>&3)
# Check if the user made a choice
if [ $? -eq 0 ]; then
echo "$choice"
else
exit 0
fi
}
module_options+=(
["get_user_continue,author"]="@Tearran"
["get_user_continue,ref_link"]=""
["get_user_continue,feature"]="get_user_continue"
["get_user_continue,desc"]="Display a Yes/No dialog box and process continue/exit"
["get_user_continue,example"]="get_user_continue 'Do you wish to continue?' process_input"
["get_user_continue,doc_link"]=""
["get_user_continue,status"]="Active"
)
#
# Function to display a Yes/No dialog box
#
function get_user_continue() {
local message="$1"
local next_action="$2"
if $($DIALOG --yesno "$message" 15 80 3>&1 1>&2 2>&3); then
$next_action
else
$next_action "No"
fi
}
menu_options+=(
["get_user_continue,author"]="@Tearran"
["get_user_continue,ref_link"]=""
["get_user_continue,feature"]="process_input"
["get_user_continue,desc"]="used to process the user's choice paired with get_user_continue"
["get_user_continue,example"]="get_user_continue 'Do you wish to continue?' process_input"
["get_user_continue,status"]="Active"
["get_user_continue,doc_link"]=""
)
#
# Function to process the user's choice paired with get_user_continue
#
function process_input() {
local input="$1"
if [ "$input" = "No" ]; then
# user canceled
echo "User canceled. exiting"
exit 0
fi
}
module_options+=(
["get_user_continue_secure,author"]="@Tearran"
["get_user_continue_secure,ref_link"]=""
["get_user_continue_secure,feature"]="get_user_continue_secure"
["get_user_continue_secure,desc"]="Secure version of get_user_continue"
["get_user_continue_secure,example"]="get_user_continue_secure 'Do you wish to continue?' process_input"
["get_user_continue_secure,doc_link"]=""
["get_user_continue_secure,status"]="Active"
)
#
# Secure version of get_user_continue
#
function get_user_continue_secure() {
local message="$1"
local next_action="$2"
# Define a list of allowed functions
local allowed_functions=("process_input" "other_function")
# Check if the next_action is in the list of allowed functions
found=0
for func in "${allowed_functions[@]}"; do
if [[ "$func" == "$next_action" ]]; then
found=1
break
fi
done
if [[ "$found" -eq 1 ]]; then
if $($DIALOG --yesno "$message" 10 80 3>&1 1>&2 2>&3); then
$next_action
else
$next_action "No"
fi
else
echo "Error: Invalid function"
exit 1
fi
}
module_options+=(
["see_current_apt,author"]="@Tearran"
["see_current_apt,ref_link"]=""
["see_current_apt,feature"]="see_current_apt"
["see_current_apt,desc"]="Check when apt list was last updated and suggest updating or update"
["see_current_apt,example"]="see_current_apt or see_current_apt update"
["see_current_apt,doc_link"]=""
["see_current_apt,status"]="Active"
)
#
# Function to check when the package list was last updated
#
see_current_apt() {
# Number of seconds in a day
local update_apt="$1"
local day=86400
local ten_minutes=600
# Get the current date as a Unix timestamp
local now=$(date +%s)
# Get the timestamp of the most recently updated file in /var/lib/apt/lists/
local update=$(stat -c %Y /var/lib/apt/lists/* 2>/dev/null | sort -n | tail -1)
# Check if the update timestamp was found
if [[ -z "$update" ]]; then
echo "No package lists found."
return 1 # No package lists exist
fi
# Calculate the number of seconds since the last update
local elapsed=$((now - update))
# Check if any apt-related processes are running
if ps -C apt-get,apt,dpkg > /dev/null; then
echo "A package manager is currently running."
export running_pkg="true"
return 1 # The processes are running
else
export running_pkg="false"
fi
# Check if the package list is up-to-date
if ((elapsed < ten_minutes)); then
[[ "$update_apt" != "update" ]] && echo "The package lists are up-to-date."
return 0 # The package lists are up-to-date
else
[[ "$update_apt" != "update" ]] && echo "Update the package lists." # Suggest updating
[[ "$update_apt" == "update" ]] && apt_install_wrapper apt-get update
return 0 # The package lists are not up-to-date
fi
}
module_options+=(
["sanitize_input,author"]="@Tearran"
["sanitize_input,ref_link"]=""
["sanitize_input,feature"]="sanitize_input"
["sanitize_input,desc"]="sanitize input cli"
["sanitize_input,example"]="sanitize_input"
["sanitize_input,status"]="Review"
)
#
# sanitize input cli
#
sanitize_input() {
local sanitized_input=()
for arg in "$@"; do
if [[ $arg =~ ^[a-zA-Z0-9_=]+$ ]]; then
sanitized_input+=("$arg")
else
echo "Invalid argument: $arg"
exit 1
fi
done
echo "${sanitized_input[@]}"
}