-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzothelper
executable file
·489 lines (414 loc) · 15.8 KB
/
zothelper
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
#!/bin/env bash
VER='0.16'
#---------------------------------------------------#
# Name: zothelper #
# #
# Purpose: prompt user for zopen command options #
# #
# Dependencies: gum and zopen and bash #
# #
# Syntax: zothelper #
# #
# Author/Contributors: #
# Lionel B. Dyck ([email protected]) #
# Igor Todorovski #
# #
# Change History (current on top): #
# 2025/01/24 LBD - 0.16 Remove blank selection #
# 2025/01/23 LBD - 0.15 Support zopen generate #
# 2025/01/23 LBD - 0.14 Sort zopen options #
# 2025/01/23 LBD - 0.13 Add support for info #
# 2024/10/29 LBD - 0.12 Change to zopen #
# 2024/07/04 LBD - 0.11 Use gsed instead of sed #
# 2024/02/20 LBD - 0.10 Update list to remove -d #
# 2024/02/18 LBD - 0.09 Major restructure #
# 2024/02/17 LBD - 0.08 Support zopen list/query #
# 2024/02/12 LBD - 0.07 Don't default clean to all#
# 2024/02/12 LBD - 0.06 Major refinement (thx IT) #
# Move remove to subroutine #
# 2024/02/12 IT - 0.05 Esc to go back #
# Move install to subroutine#
# 2024/02/12 LBD - 0.04 Remove extraneous newlines#
# from echos #
# 2024/02/12 LBD - 0.03 Enhance clean options #
# 2024/02/11 LBD - 0.02 Add zopen upgrade #
# 2024/02/11 LBD - 0.01 Creation #
#---------------------------------------------------#
# Repository: github.com/lbdyck/prompters #
#---------------------------------------------------#
# Copyleft by Lionel B. Dyck 2024-2025 #
#---------------------------------------------------#
#----------------------------------------------------------------#
# Define the color for the banner #
# 5 - purple #
# 12 - blue shade #
# 33 - darker blue #
# 46 - green #
# 51 - turq shade #
#----------------------------------------------------------------#
COLOR=51
#----------------------------------------------------------------#
# Define the spinner #
# line, dot, minidot, jump, pulse, points, globe, moon, #
# monkey, meter, hamburger #
#----------------------------------------------------------------#
SPIN="line"
#SPIN="dot"
#SPIN="minidot"
#SPIN="jump"
#SPIN="pulse"
#SPIN="points"
#SPIN="globe"
#SPIN="moon"
#SPIN="monkey"
#SPIN="meter"
#SPIN="hamburger"
#----------------------------------------------------------#
# Define our temp and tempw variables #
#----------------------------------------------------------#
TEMP="/tmp/zopen_list"
TEMPW="/tmp/zopen_listw"
#----------------------------------------------------------#
# Define the MANDB variable - null to not run #
#----------------------------------------------------------#
MANDB="mandb"
#MANDB=""
#----------------------------------------------------------#
# Define the gum environment variables #
#----------------------------------------------------------#
export GUM_INPUT_PROMPT_FOREGROUND="#$COLOR"
export GUM_INPUT_PLACEHOLDER=". . ."
export GUM_IMPORT_PROMPT="* "
export TERM=xterm-256color
#----------------------------------------------------------------#
# Define the TAG REDIR for our needs #
#----------------------------------------------------------------#
export _TAG_REDIR_IN=txt
export _TAG_REDIR_OUT=txt
export _TAG_REDIR_ERR=txt
#----------------------------------------------------------#
# Define our gum choose prompt text #
#----------------------------------------------------------#
HEADSEL="(ESC: back, Tab/Space: Toggle select, Up/Down: Navigate, Enter: Submit):"
#----------------------------------------------------------#
# Common Routines - do a Find for ##Start for the start #
# of the actual code #
#----------------------------------------------------------#
#----------------------#
# Display our banner #
#----------------------#
banner()
{
clear
gum style \
--foreground $COLOR --border-foreground $COLOR --border double \
--align center --width 50 --margin "1 2" --padding "2 4" \
'zopen Helper' $VER
#----------------------------------------------------------#
# Prompt the user for the zopen options #
#----------------------------------------------------------#
ZOT=$(zopen help | gsed -n '/Command:/,/Options:/ {//!p}' | \
sed '$d' | \
sort | \
gum choose --header.foreground="$COLOR" \
--height=18 \
--header "Select zopen command $HEADSEL")
ZOT=$(echo $ZOT | cut -d " " -f1 )
#----------------------------------------------------------#
# Test for any selections #
#----------------------------------------------------------#
if [ -z "$ZOT" ]; then
echo 'No selections made - exiting . . .'
exit 0
fi
#----------------------------------------------------------#
# Process the Selected Command #
#----------------------------------------------------------#
case "$ZOT" in
#----------------------------------------------------------#
# zopen clean command - use all the options #
#----------------------------------------------------------#
clean )
CLEAN=$(gum choose "cache" "dangling" "metadata" "unused" "all" \
--no-limit --header="Select zopen clean option $HEADSEL" \
--height=18 \
--header.foreground="$COLOR")
if [ -z "$CLEAN" ]; then
echo "No clean option selected - terminating..."
exit 0
fi
CLEAN=$(echo "$CLEAN" | sed -e 's/[^ ]*/--&/' -e 's/^-- //' | tr '\n' ' ')
CLEAN="zopen clean $CLEAN --verbose"
echo -e "Generated command: \n$CLEAN"
# If gum confirm Yes then process the next command
gum confirm "Execute Generated zopen command:" && $CLEAN
exit 0
;;
#----------------------------------------------------------#
# generate a new zopen project #
#----------------------------------------------------------#
generate )
zopen generate
gum input --placeholder "Generate complete. Press Enter to continue." \
--cursor.foreground "#FF0" \
--prompt.foreground "#0FF"
banner
;;
#----------------------------------------------------------#
# install available packages #
#----------------------------------------------------------#
install )
install
;;
#----------------------------------------------------------#
# Info - display information about installed package #
#----------------------------------------------------------#
info )
info
;;
#----------------------------------------------------------#
# Remove any installed packages #
#----------------------------------------------------------#
remove )
remove
;;
#----------------------------------------------------------#
# List packages #
#----------------------------------------------------------#
list )
list
;;
#----------------------------------------------------------#
# Query packages (alias of list) #
#----------------------------------------------------------#
query )
list
;;
#----------------------------------------------------------#
# Upgrade any installed packages #
#----------------------------------------------------------#
upgrade )
UPGRADE="zopen upgrade -y"
# report what we have generated for the user
echo -e "Generated command: \n$UPGRADE"
# If gum confirm Yes then process the next command
gum confirm "Execute Generated zopen upgrade:" && $UPGRADE && $MANDB
exit 0
;;
esac
echo "$ZOT selected but sadly that command has not been implemented within"
echo "the zothelper as yet."
echo ' '
echo 'Here is the help information for the command to help for now:'
echo ' '
zopen $ZOT -h
exit 0
}
#----------------------------------------------------------#
# routine for the zopen install option #
#----------------------------------------------------------#
install()
{
# Ask the user if they want to filter the packages
FILTER=$(gum input --prompt "Enter text to filter the available packages or blank:" \
--placeholder="e.g. ssh" --value=" " --header.foreground="$COLOR")
RC=$?
# Escape pressed, go back to the beginning
if [ "$RC" -eq 130 ] ; then
banner
fi
# Test if there is a filter and if so then clean up working file
if [ ! -z $FILTER ]; then
FILTERC="--remote-search $FILTER"
else
FILTERC=""
fi
# spinning something while we collect the list of all available packages
gum spin --title "Generating list of available packages" -s "$SPIN" \
--show-output --spinner.foreground="$COLOR" --title.foreground="$COLOR" \
-- zopen query --list "$FILTERC" > "$TEMP"
cat "$TEMP" | grep Not | cut -d " " -f1 > "$TEMPW"
if [ ! -s "$TEMPW" ]; then
echo "No packages found to match $FILTER"
rm "$TEMP" "$TEMPW"
FILTER=""
FILTERC=""
install
fi
# Prompt for package selections - any number allowed
# but first remove all installed packages and only use
# the package name
SELECTED=$(cat "$TEMPW" | \
gum choose --no-limit \
--height=18 \
--header "Select packages to install $HEADSEL" --header.foreground="$COLOR")
RC=$?
# Remove the work files
rm "$TEMP" "$TEMPW"
# Escape pressed, go back to the beginning
if [ "$RC" -eq 130 ] ; then
install
fi
# Test if any packages selected - must have at least one
if [ -z "$SELECTED" ]; then
echo "No selections - try again. . ."
install
fi
# Remove new line from SELECTED variable
SELECTED=$(echo "$SELECTED" | tr '\n' ' ')
# Generate the zopen install command
INSTALL="zopen install -y $SELECTED"
# report what we have generated for the user
echo -e "Generated command: \n$INSTALL"
# If gum confirm Yes then process the next command
gum confirm "Execute Generated zopen install:" && $INSTALL && $MANDB
exit 0
}
#----------------------------------------------------------#
# routine for the zopen remove option #
#----------------------------------------------------------#
remove()
{
# spinning something while we collect the list of all available packages
gum spin --title "Generating list of all installed packages" -s "$SPIN" \
--show-output --spinner.foreground="$COLOR" --title.foreground="$COLOR" \
-- zopen query --installed > "$TEMP"
# Ask the user if they want to filter the packages
FILTER=$(gum input --prompt "Enter text to filter the installed packages or blank:" \
--placeholder="e.g. ssh" --value=" " --header.foreground="$COLOR")
# Test if there is a filter and if so then clean up working file the
# Prompt for package selections - any number allowed
# but first only use the package name
if [ ! -z $FILTER ]; then
grep -i $FILTER $TEMP > $TEMPW
SELECTED=$(cat "$TEMPW"| cut -d " " -f1 | \
gum choose --no-limit \
--height=18 \
--header "Select packages to remove $HEADSEL" --header.foreground="$COLOR")
else
SELECTED=$(cat "$TEMP"| tail -n +2 | cut -d " " -f1 | \
gum choose --no-limit \
--header "Select packages to remove $HEADSEL" --header.foreground="$COLOR")
fi
RC=$?
# Escape pressed, go back to the beginning
if [ "$RC" -eq 130 ] ; then
remove
fi
# Test if any packages selected - must have at least one
if [ -z "$SELECTED" ]; then
echo "No selections - exiting...."
banner
fi
# Remove the work files
rm $TEMP
# Remove new line from SELECTED variable
SELECTED=$(echo "$SELECTED" | tr '\n' ' ')
# Generate the zopen remove command
REMOVE="zopen remove -y $SELECTED"
# report what we have generated for the user
echo -e "Generated command: \n$REMOVE"
# If gum confirm Yes then process the next command
gum confirm "Execute Generated zopen remove:" && $REMOVE && $MANDB
exit 0
}
#----------------------------------------------------------#
# routine for the zopen info option #
#----------------------------------------------------------#
info()
{
# spinning something while we collect the list of all available packages
gum spin --title "Generating list of all installed packages" -s "$SPIN" \
--show-output --spinner.foreground="$COLOR" --title.foreground="$COLOR" \
-- zopen query --installed > "$TEMP"
# Ask the user if they want to filter the packages
FILTER=$(gum input --prompt "Enter text to filter the installed packages or blank:" \
--placeholder="e.g. ssh" --value=" " --header.foreground="$COLOR")
# Test if there is a filter and if so then clean up working file the
# Prompt for package selections - any number allowed
# but first only use the package name
if [ ! -z $FILTER ]; then
grep -i $FILTER $TEMP > $TEMPW
SELECTED=$(cat "$TEMPW"| cut -d " " -f1 | \
gum choose --no-limit \
--height=18 \
--header "Select packages for INFO $HEADSEL" --header.foreground="$COLOR")
else
SELECTED=$(cat "$TEMP"| tail -n +2 | cut -d " " -f1 | \
gum choose --no-limit \
--header "Select packages for INFO $HEADSEL" --header.foreground="$COLOR")
fi
RC=$?
# Escape pressed, go back to the beginning
if [ "$RC" -eq 130 ] ; then
info
fi
# Test if any packages selected - must have at least one
if [ -z "$SELECTED" ]; then
echo "No selections - exiting...."
banner
fi
# Remove new line from SELECTED variable
SELECTED=$(echo "$SELECTED" | tr '\n' ' ')
# Generate the zopen info command
INFO="zopen info $SELECTED"
# report what we have generated for the user
echo -e "Generated command: \n$INFO"
$INFO | gum pager
banner
}
#------------------------------#
# Routine for zopen list #
#------------------------------#
list()
{
LIST=$(gum choose "list" "remote-search" "installed" \
--header "Select list option" --header.foreground="$COLOR")
RC=$?
# Escape pressed then start over
if [ "$RC" -eq 130 ] ; then
banner
fi
#-------------------#
# update list parm #
#-------------------#
FILTER=''
case "$LIST" in
list)
LIST="--list"
;;
remote-search)
LIST="--remote-search"
FILTER=1
;;
installed)
LIST="--installed"
;;
esac
if [ "$FILTER" == 1 ]; then
ARG=$(gum input --prompt "Enter a package name or prefix to search for:" \
--placeholder="e.g. ssh" --value=" " --header.foreground="$COLOR")
if [ -z "$ARG" ]; then
echo "Search required."
list
fi
LIST="$LIST $ARG"
fi
# spinning something while we collect the list of all available packages
gum spin --title "Generating list results" -s "$SPIN" \
--show-output --spinner.foreground="$COLOR" --title.foreground="$COLOR" \
-- zopen list "$LIST" > "$TEMP"
if [ "$FILTER" == 1 ]; then
LINES=$(wc -l < "$TEMP")
if [ "$LINES" -lt 3 ]; then
echo "No packages found for remote-search list."
rm "$TEMP"
banner
fi
fi
cat "$TEMP" | gum pager
rm "$TEMP"
banner
}
##Start of the working code
banner