-
Notifications
You must be signed in to change notification settings - Fork 0
/
provii
executable file
·1273 lines (1136 loc) · 31.4 KB
/
provii
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh -eE
cleanup() {
if [ "$RESET_TABS" ]; then
tabs -8
fi
if [ "$CLEAN_CACHE_EXIT_ON" ] && [ "$PROVII_CACHE" ]; then
rm -rf $PROVII_CACHE || true
fi
}
if command -v tabs >/dev/null 2>&1; then
tabs -4 && RESET_TABS=1
trap 'cleanup' EXIT
fi
PROVII_REPO=kriipke/provii
PROVII_BRANCH=master
PROVII_VERSION='0.3.0'
CLEAN_CACHE_ON_EXIT=1
#JQ_TARGET_DIR="$HOME/.local/bin"
CHAR_NL=\\012
CHAR_TAB=\\011
CHAR_SPCE=\\040
GH=https://api.github.com
if [ "$DEBUG" ]; then
set -x
unset CLEAN_CACHE_EXIT_ON
VERBOSE=1
PS4='+ $(date "+%s.%N")\011 @line $LINENO\011'
fi
PROVII_VARS='\(OS\|SCOPE\|LIBC\|BASH_COMP\|ZSH_COMP\|MAN\|NAME\|CACHE\|ARCH\|BIN\|LOG\)'
check_current_version() {
API_REQ=$GH/repos/kriipke/provii/tags
LATEST_VERSION="$( curl -s "$API_REQ" | $JQ -r '.[0].name' )"
THIS_SCRIPTS_DIRNAME="$(dirname "$0")"
case "$LATEST_VERSION" in
"$PROVII_VERSION")
true ;;
v*)
if [ -w "$THIS_SCRIPTS_DIRNAME" ]; then
printf '\nNew provii release found!\n\nPress Enter to install %s or CTRL-C to Cancel.\n' "$LATEST_VERSION"
read RESPONSE
printf 'Upgrading to %s...\n' "$LATEST_VERSION"
PROVII_BIN="$THIS_SCRIPTS_DIRNAME" install provii
else
printf "\nNew provii release found!\n\nSadly you'll have to re-run provii with administrator privileges to upgrade to %s. Press Ctrl-C to exit or Enter to continue with the current version.\n" "$LATEST_VERSION"
read RESPONSE
fi
;;
*)
echo "Update check failed! Failed to determine current provii release." ;;
esac
}
if command -v tput >/dev/null 2>&1; then
TAB="$(tput ht 2>/dev/null || echo '')"
if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
RED="$(tput setaf 3 2>/dev/null || echo '')"
MAGENTA="$(tput setaf 5 2>/dev/null || echo '')"
CYAN="$(tput setaf 6 2>/dev/null || echo '')"
UNDERLINE="$(tput smul 2>/dev/null || echo '')"
NO_UNDERLINE="$(tput rmul 2>/dev/null || echo '')"
STYLE_RESET="$(tput sgr0 2>/dev/null || echo '')"
fi
STYLE_ERROR="$MAGENTA"
STYLE_ECHO="$MAGENTA"
STYLE_VAR="$MAGENTA"
STYLE_FILES="$CYAN"
STYLE_HEADER="${UNDERLINE}${CYAN}"
STYLE_INFO="$RED"
fi
_help='
provii - command line utility installer
usage:
provii install [-vi] APPLICATION
provii env APPLICATION
provii cat APPLICATION
provii ls
provii help SUBCOMMAND
OPTIONS:
-h, --help print this message
-b, --branch choose which provii branch to work with
-i, --interactive confirm any changes to the system before they are made
-v, --verbose prints various information depending on subcommand'
print_usage() {
printf %s\\n\\n "$_help"
exit
}
has_cmd() {
if command -v "$1" >/dev/null; then
return 0
else
return 1
fi
}
create_dir() {
mkdir -p "$1" || {
warn Failed to create "$1"
return 1
}
}
style() {
case "${1:?}" in
ERROR) STYLE="$MAGENTA" ;;
COLOR) STYLE="$MAGENTA" ;;
VAR) STYLE="$MAGENTA" ;;
FILE) STYLE="$CYAN" ;;
DIR) STYLE="$CYAN" ;;
HEADER) STYLE="${UNDERLINE}${CYAN}" ;;
INFO) STYLE="$RED" ;;
*) [ '\e' = "$(echo "$1" | cut -c1)" ] && STYLE="$1" ;;
esac
printf '%s%s%s' "${STYLE:?}" "${2:-"$(cat)"}" "$STYLE_RESET"
}
prompt() {
printf %s "${@:-"$(cat)"}"
unset REPLY
}
# installs jq to $HOME/.local/bin/jq, where it will be looked
#- for in the event that it cannot be found by searching $PATH
install_jq() {
JQ_VERSION="1.6"
JQ_URL="https://github.com/stedolan/jq/releases/download/jq-$JQ_VERSION"
JQ_TARGET_DIR="${JQ_TARGET_DIR:-$HOME/.local/bin}"
set_provii_os
set_provii_arch
if [ "$PROVII_OS" = 'darwin' ]; then
JQ_FILE="jq-osx-amd64"
else
case "$PROVII_ARCH" in
x86_64 | amd64 )
JQ_FILE="jq-linux64"
;;
arch64 )
curl -sSLo "$JQ_TARGET_DIR/jq" https://dl.spencersmolen.com/linux/aarch64/jq
return 0
;;
i386 | i586 | i686)
JQ_FILE="jq-linux32"
;;
esac
fi
[ -d "$JQ_TARGET_DIR" ] || create_dir "$JQ_TARGET_DIR"
curl -sSLo "$JQ_TARGET_DIR/jq" "$JQ_URL/$JQ_FILE"
chmod ug+rx "$JQ_TARGET_DIR/jq"
JQ="$JQ_TARGET_DIR/jq"
export JQ
if [ -z "$JQ" ]; then
err 'Function install_jq failed to set $JQ to path of jq.'
else
return 0
fi
}
source_proviirc() {
if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/proviirc" ]; then
PROVIIRC="${XDG_CONFIG_HOME-$HOME/.config}/proviirc"
printf '%s sourcing %s\n' \
"$(style INFO "INFO")" \
"$(style FILE "$PROVIIRC")"
if [ "$VERBOSE" ]; then
_PROVIIRC_VARS_RE=$(printf '^provii_%s=\\b' "$PROVII_VARS")
printf '%s%s\n' "$TAB" "$(grep "$_PROVIIRC_VARS_RE" "$PROVIIRC")"
fi
. "$PROVIIRC"
fi
}
set_provii_cache() {
if [ -n "$PROVII_CACHE" ]; then
[ ! -w "$(dirname "$PROVII_CACHE")" ] && err \
'The value provided for $PROVII_CACHE either does' \
'note exist or is not writable, please fix.'
else
PROVII_CACHE="${XDG_CACHE_HOME-$HOME/.cache}/provii"
fi
INSTALLER_CACHE="$PROVII_CACHE${INSTALLER:+/$INSTALLER}"
[ -d "$INSTALLER_CACHE" ] && rm -rf "$INSTALLER_CACHE"
create_dir "$INSTALLER_CACHE" || err "Failed to create cache."
}
set_provii_bin() {
if [ "$PROVII_BIN" ]; then
return 0
fi
case "${PROVII_SCOPE:?}" in
system)
if [ -n "$PROVII_SYSTEM_BIN" ]; then
PROVII_BIN="$PROVII_SYSTEM_BIN"
else
BIN_FALLBACK=/usr/local/bin
if has_cmd systemd-path; then
BIN_DEFAULT=$(systemd-path system-binaries)
PATH_DIRS="$(systemd-path search-binaries | sed 's/:/\n/g')"
else
BIN_DEFAULT=/usr/bin
PATH_DIRS="$(echo "$PATH" | sed 's/:/\n/g')"
fi
if echo "$PATH_DIRS" | grep -q "$BIN_DEFAULT"; then
create_dir "$BIN_DEFAULT"
PROVII_BIN="$BIN_DEFAULT"
elif echo "$PATH_DIRS" | grep -q "$BIN_FALLBACK"; then
create_dir "$BIN_FALLBACK"
PROVII_BIN="$BIN_FALLBACK"
else
err 'Value selected for $PROVII_BIN, '"$PROVII_BIN"', not in PATH'
fi
fi
;;
user)
if [ -n "$PROVII_USER_BIN" ]; then
PROVII_BIN="$PROVII_USER_BIN"
else
BIN_FALLBACK=$HOME/bin
if has_cmd systemd-path; then
BIN_DEFAULT=$(systemd-path user-binaries)
PATH_DIRS="$(systemd-path search-binaries | sed 's/:/\n/g')"
else
BIN_DEFAULT=$HOME/.local/bin
PATH_DIRS="$(echo "$PATH" | sed 's/:/\n/g')"
fi
if echo "$PATH_DIRS" | grep -q "$BIN_DEFAULT"; then
PROVII_BIN="$BIN_DEFAULT"
elif echo "$PATH_DIRS" | grep -q "$BIN_FALLBACK"; then
PROVII_BIN="$BIN_FALLBACK"
elif echo "$PATH_DIRS" | grep -m 1 "^$HOME"; then
PROVII_BIN="$( echo "$PATH_DIRS" | grep -m 1 "^$HOME" )"
else
err Neither the default target for binaries, \
"$(style DIR "$BIN_DEFAULT")", nor the fallback target, \
"$(style DIR "$BIN_FALLBACK")", was found in PATH. Either \
Add one of the above directories to your PATH, add a \
directory to your PATH that resides in your home directory, \
or re-run provii. Exiting without making any changes.
fi
fi
;;
esac
create_dir "${PROVII_BIN:?}"
if ! [ -w $PROVII_BIN ]; then
err Target directory for binary files, "$(style DIR "$PROVII_BIN")",\
is not writable, please correct this or change default target and \
re-run. Exiting without making any changes.
fi
}
set_provii_data() {
case "${PROVII_SCOPE:?}" in
system) PROVII_DATA=${SYS_DATA-/usr/local/share} ;;
user) PROVII_DATA=${USER_DATA-$HOME/.local/share} ;;
esac
create_dir "${PROVII_DATA:?'Failed to set variable $DATA'}"
}
has_package() {
if [ "$#" -eq 2 ] && [ "$1" = '--version' ] && [ -n "$2" ]; then
ECHO_PACKAGE_VERSION=1
PACKAGE="$2"
elif [ "$#" -eq 1 ] && [ -n "$1" ]; then
PACKAGE="$1"
else
err "Usage: has_package [--version] PACKAGE"
fi
if has_cmd rpm; then
QUERYPKG_CMD_FMT='rpm -qa --queryformat %s name=%s'
elif has_cmd dpkg-query; then
QUERYPKG_CMD_FMT='dpkg-query --showformat=%s --show %s'
else
err "Function has_package failed to locate " \
"either (1) rpm or (2) dpkg-query commands."
fi
PACKAGE_VERSION="$($(printf "$QUERYPKG_CMD_FMT" '%{VERSION}\n' "$PACKAGE"))"
if [ -n "$PACKAGE_VERSION" ]; then
[ "$ECHO_PACKAGE_VERSION" ] && echo "$PACKAGE_VERSION"
return 0
fi
}
rm_app() {
set_provii_log
APP="${1:?}"
APP_FILES="$(awk -v app="$APP" '$3 == app { print $4 }' "$PROVII_LOG")"
if [ -n "$APP_FILES" ]; then
if [ "$INTERACTIVE" ]; then
printf "\nThe following files associated with %s were found:\n" "$1"
printf '\t%s\n' "$APP_FILES" | style FILE
printf '\n\nDelete all files associated with %s? [Y|n] ' "$1" | prompt
read -r
case "$REPLY" in
[Nn]*)
echo "Exiting without removing $1."
exit
;;
esac
fi
awk -v app="$1" '{
if ( $3 == app )
system("rm " $4)
else
print
}' "$PROVII_LOG" >"$PROVII_LOG.new"
mv "$PROVII_LOG.new" "$PROVII_LOG"
else
echo "No files for $1 listed in $PROVII_LOG, exiting without removing anything"
exit 1
fi
}
set_provii_log() {
PROVII_LOG=${PROVII_LOG:-"$HOME/.provii.log"}
touch "$PROVII_LOG"
}
set_provii_man() {
case "${PROVII_SCOPE:?}" in
system)
[ -n "$PROVII_SYSTEM_MAN" ] && {
PROVII_MAN="$PROVII_SYSTEM_MAN"
return 0
}
MAN_DIR_PREFERRED="/usr/local/share/man"
MAN_DIR_ALT="/usr/share/man"
if has_cmd manpath; then
MANPATH_DIRS="$(manpath -g | tr : "$CHAR_NL")"
if echo "$MANPATH_DIRS" | grep -q "$MAN_DIR_PREFERRED"; then
PROVII_MAN="$MAN_DIR_PREFERRED"
elif echo "$MANPATH_DIRS" | grep -q "$MAN_DIR_ALT"; then
PROVII_MAN="$MAN_DIR_ALT"
fi
else
if has_package 'man-db'; then
PROVII_MAN="$MAN_DIR_ALT"
else
warn 'Package not installed: man-db. Failed to set $BASH_COMP variable.'
fi
fi
;;
user)
[ -n "$PROVII_USER_MAN" ] && {
PROVII_MAN="$PROVII_USER_MAN"
return 0
}
MAN_DIR_PREFERRED="${XDG_DATA_HOME:-$HOME/.local/share}/man"
if has_cmd manpath; then
MANPATH_DIRS="$(manpath | tr ":" "$CHAR_NL")"
if echo "$MANPATH_DIRS" | grep -q "$MAN_DIR_PREFERRED"; then
PROVII_MAN="$MAN_DIR_PREFERRED"
elif echo "$MANPATH_DIRS" | grep -q "$HOME"; then
PROVII_MAN="$(echo "$MANPATH_DIRS" | grep -m 1 "$HOME")"
else
# else, add MAN_DIR_PREFERRED to manpath
PROVII_MAN="${XDG_DATA_HOME:-$HOME/.local/share}/man"
echo "MANDATORY_MANPATH $PROVII_MAN" >"$HOME/.manpath"
fi
else
if has_package man-db; then
# else, add MAN_DIR_PREFERRED to manpath
PROVII_MAN="${XDG_DATA_HOME:-$HOME/.local/share}/man"
echo "MANDATORY_MANPATH $PROVII_MAN" >"$HOME/.manpath"
else
warn 'Package not installed: man-db. Failed to set $BASH_COMP variable.'
fi
fi
;;
esac
if [ -n "$PROVII_MAN" ]; then
create_dir "$PROVII_MAN"/man1
create_dir "$PROVII_MAN"/man5
create_dir "$PROVII_MAN"/man8
fi
}
set_provii_bash_comp() {
case "$PROVII_SCOPE" in
system)
PROVII_BASH_COMP=${SYS_BASH_COMP-/etc/bash_completion.d}
if has_cmd manpath; then
for MAN_PATH in $(manpath -g | tr : $'\n'); do
if [ -d "$MAN_PATH" ] && [ -w "$MAN_PATH" ]; then
PROVII_MAN="$MAN_PATH"
break
fi
done
fi
;;
user)
# bash-completion 2.9 introduced the follow as a directory for completions:
# ${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion.d
BASH_COMP_VER="$(has_package --version 'bash-completion')"
if [ -n "$BASH_COMP_VER" ]; then
XDG_ENABLED_BASH_COMP_VER='2.9'
if expr \( "$BASH_COMP_VER" \< 2.9 \) >/dev/null &&
[ -n "$XDG_DATA_HOME" ] && [ -n "$XDG_CONFIG_HOME" ]; then
# see, https://github.com/scop/bash-completion/tree/2.8
PROVII_BASH_COMP="${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion.d"
PROVII_BASH_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" &&
create_dir "$XDG_CONFIG_HOME"
else
# see, https://github.com/scop/bash-completion/tree/2.9
PROVII_BASH_COMP="$HOME/.bash-completion.d"
PROVII_BASH_CONFIG="$HOME/.bash_completion"
fi
create_dir "$PROVII_BASH_COMP"
# make sure there's a line in bash_completion (config) to
# source the files in bash-completion.d gets sourced
BASH_COMP_DIR_ESCAPED="$(printf %s "$PROVII_BASH_COMP" | sed 's/\./\\./g')"
COMP_DIR_RE="$(printf '^[^#].*%s' "$BASH_COMP_DIR_ESCAPED")"
if ! grep -q "$COMP_DIR_RE" "$PROVII_BASH_CONFIG" 2>/dev/null; then
printf '
for i in %s/*; do
if [ -r "$i" ]; then
. "$i"
fi
done' "$PROVII_BASH_COMP" >>"$PROVII_BASH_CONFIG"
fi
fi
;;
esac
}
set_provii_zsh_comp() {
if has_cmd zsh; then
case "${PROVII_SCOPE:?}" in
system)
ZSH_DIR="$(zsh -c '
. /etc/zshrc &>/dev/null
echo $ZSH'
)"
SYSTEM_FPATH="$( export CHAR_NL CHAR_SPCE; zsh -c '
. /etc/zshrc &>/dev/null
echo "$fpath" | tr $CHAR_SPCE $CHAR_NL'
)"
ZSH_COMP_DEFAULT=/usr/local/share/zsh/site-functions
ZSH_COMP_FALLBACK=/usr/share/zsh/site-functions
ZSH_DIR_COMPS="$(printf '%s/completions' "$ZSH_DIR")"
ZSH_DIR_CUSTOM="$(printf '%s/custom' "$ZSH_DIR")"
case "$SYSTEM_FPATH" in
*$ZSH_DIR_COMPS*)
if ! echo "$ZSH_DIR" | grep -q "^$HOME"; then
PROVII_ZSH_COMP="$ZSH_DIR_COMPS"
fi
;;
*$ZSH_DIR_CUSTOM*)
if ! echo "$ZSH_DIR" | grep -q "^$HOME"; then
PROVII_ZSH_COMP="$ZSH_DIR_CUSTOM"
fi
;;
*$ZSH_COMP_DEFAULT*) PROVII_ZSH_COMP="$ZSH_DIR_DEFAULT" ;;
*) PROVII_ZSH_COMP="$ZSH_DIR_FALLBACK" ;;
esac
;;
user)
USER_FPATH="$(export CHAR_NL CHAR_SPCE; zsh -c '
. /etc/zshrc &>/dev/null
. ${ZDOTDIR:-$HOME}/.zshrc &>/dev/null
echo "$fpath" | tr $CHAR_SPCE $CHAR_NL'
)"
ZDOTDIR="$( zsh -c '
. /etc/zshrc &>/dev/null
. ${ZDOTDIR:-$HOME}/.zshrc &>/dev/null
echo ${ZDOTDIR:-$HOME}'
)"
# leave without setting PROVII_ZSH_COMP if no dirs
# in fpath located in user's home directory
if echo "$USER_FPATH" | grep -m1 '^$HOME'; then
USER_FPATH="$(echo "$USER_FPATH" | grep -m1 '^$HOME')"
ZDOTDIR_COMPS="$(printf '%s/completions' "$ZDOTDIR")"
ZDOTDIR_CUSTOM="$(printf '%s/custom' "$ZDOTDIR")"
ZSH_COMP_DEFAULT="$(echo "$USER_FPATH" | grep -m1 "^$HOME")"
case "$USER_FPATH" in
*$ZDOTDIR_COMPS*) PROVII_ZSH_COMP="$ZDOTDIR_COMPS" ;;
*$ZDOTDIR_CUSTOM*) PROVII_ZSH_COMP="$ZDOTDIR_CUSTOM" ;;
*) PROVII_ZSH_COMP="$ZSH_COMP_DEFAULT" ;;
esac
fi
;;
esac
${PROVII_ZSH_COMP:+`create_dir "$PROVII_ZSH_COMP"`}
fi
}
set_provii_libc() {
if command -v ldd >/dev/null 2>&1; then
LDD_OUTPUT="$(
ldd --version 2>/dev/null \
| head -1 \
| tr "[:upper:]" "[:lower:]"
)"
case "$LDD_OUTPUT" in
*musl*) PROVII_LIBC=musl ;;
*gnu* | *glibc*) PROVII_LIBC=gnu ;;
*) err "failed to determine libc version, exiting." ;;
esac
else
err "provii requires command 'ldd' to run, exiting."
fi
}
set_provii_os() {
# see, https://en.wikipedia.org/wiki/Uname#Examples
PROVII_OS="${PROVII_OS:-"$(uname -s | tr "[:upper:]" "[:lower:]")"}"
}
set_provii_arch() {
PROVII_ARCH="${PROVII_ARCH:-"$(uname -m | tr "[:upper:]" "[:lower:]")"}"
}
set_provii_scope() {
if [ "$(id -u)" -eq "0" ]; then
PROVII_SCOPE=system
else
PROVII_SCOPE=user
fi
}
get_installer() {
GITHUB_API_QUERY="$(
printf 'https://api.github.com%s%s' \
"/repos/$PROVII_REPO/contents/repository/$INSTALLER" \
"${PROVII_BRANCH:+?ref=$PROVII_BRANCH}"
)"
curl -sSL "$GITHUB_API_QUERY" \
| "$JQ" '.download_url' \
| xargs curl -sSL
}
# TO-DO: account for the fact that $BIN is not referenced when using install fn
get_installer_vars() {
[ -z "$INSTALLATION_SCRIPT" ] && err \
"Installation script has not been downloaded yet"
VARS_RE="$(printf %s "$PROVII_VARS" | sed -E 's/(\\[(|])/\1$/g')"
printf '\n%s' "$(echo "$INSTALLATION_SCRIPT" | grep -o "$VARS_RE")" \
| sort | uniq
}
show_installer_targets() {
_INSTALLER_VARS="$(get_installer_vars "$INSTALLER")"
SRC_DST=$(echo "$INSTALLATION_SCRIPT" \
| awk -e '/install.*\$BIN/ {printf "%s,%s\n", $2, gensub(/^.*"?\$BIN\/?(.*)$/,"\1","g")}')
target_exe=''
for line in $SRC_DST; do
SRC_PATH="${line%*,}"
DST_PATH="${line#,*}"
SRC_FILE="${SRC_PATH##*/}"
DST_FILE="${DST_PATH##*/}"
output_line="$(
printf "%s,%s/%s" "$(style VAR "EXECUTABLE")" \
"$PROVII_BIN" "${DST_FILE:-$SRC_FILE}"
)"
target_exe="$(printf '%s\n%s' "$target_exe" "$output_line")"
done
if [ "$PROVII_MAN" ] && echo "$_INSTALLER_VARS" | grep -q '$MAN'; then
SRC_DST=$(echo "$INSTALLATION_SCRIPT" \
| awk -e '/cp.*\$MAN/ {printf "%s,%s\n", $2, gensub(/^.*"?\$MAN\/?(.*)$/,"\1","g")}')
target_man=''
for line in $SRC_DST; do
SRC_PATH="${line%*,}"
SRC_FILE="${SRC_PATH##*/}"
DST_PATH="${line#,*}"
DST_FILE="${DST_PATH##*/}"
TARGET_FILE="${DST_FILE:-$SRC_FILE}"
output_line="$(
printf "%s,%s/man%s/%s" "$(style VAR "MAN PAGE")" \
"$PROVII_MAN" "$(echo "${TARGET_FILE##*.}" | head -c1)" "$TARGET_FILE"
)"
target_man="$(printf '%s\n%s' "$target_man" "$output_line")"
done
fi
if [ "$PROVII_BASH_COMP" ]; then
if echo "$_INSTALLER_VARS" | grep -q '$BASH_COMP'; then
target_bash_comp="$(
printf "%s,%s/%s.bash-completion\n" "$(style VAR "BASH COMPLETIONS")" \
"$PROVII_BASH_COMP" "$INSTALLER"
)"
fi
fi
if [ "$PROVII_ZSH_COMP" ]; then
if echo "$_INSTALLER_VARS" | grep -q '$ZSH_COMP'; then
target_zsh_comp="$(
printf "%s,%s/_%s\n" "$(style VAR "ZSH COMPLETIONS")" \
"$PROVII_ZSH_COMP" "$INSTALLER"
)"
fi
fi
OLD_IFS="$IFS"
export IFS='
'
target_dirs="$(
printf '\n%s' "$target_exe" "$target_man" \
"$target_zsh_comp" "$target_bash_comp"
)"
_TABLE_WIDTH=0
for line in $target_dirs; do
[ "${#line}" -gt "$_TABLE_WIDTH" ] && _TABLE_WIDTH="${#line}"
done
_TABLE_WIDTH="$(expr \( "$_TABLE_WIDTH" - \
\( "${#STYLE_VAR}" + "${#STYLE_RESET}" - 2 \) \))"
header_fmt="$(
printf '\n%s%%-%is\n\n%s' \
"${STYLE_HEADER}" "$_TABLE_WIDTH" "${STYLE_RESET}"
)"
printf "$header_fmt" "$INSTALLER"
printf %s\\n "$target_dirs" | column -t -s,
if [ "$INTERACTIVE" ]; then
prompt 'Press Enter to continue.'
fi
# IFS="$OLD_IFS"
}
ls_installers() {
GITHUB_QUERY_LS="$(printf \
"https://api.github.com/repos/$PROVII_REPO/contents/repository%s" \
"${PROVII_BRANCH:+?ref=$PROVII_BRANCH}")"
JQ_QUERY='.[] | [ .name, .download_url ] | @csv'
for line in $(curl -sSL "$GITHUB_QUERY_LS" | "$JQ" -r "$JQ_QUERY" | tr -d '"'); do
(
APP="${line%,*}"
curl -sSL "${line#*,}" |
grep -E '(^# ).+' |
sed -n 's/^# //p' |
awk -v app="$APP" \
-v style_url="${BLUE}${UNDERLINE}" \
-v style_reset_url="${STYLE_RESET}${NO_UNDERLINE}" \
-v style_name="${STYLE_RESET}${MAGENTA}" \
-v style_reset="${STYLE_RESET}${STYLE_RESET}" \ '
BEGIN{
RS=""
FS="\n"
}{
printf "%s%s%s|", style_name,app,style_reset
for(i=2; i<=NF; i++) {
if ($i ~ /^http/) url=$i
printf("%s ", $(i-1))
}
printf "|%s%s%s\n", style_url,url,style_reset_url
}'
) &
done | column -t -s\|
wait
exit
}
fn___dl_github_tarball=$(
cat <<'EOF'
BASH_FUNC___dl_github_tarball%%=() {
REPO=$1
FQDN='https://github.com'
URL="$FQDN/$REPO/tarball/${BRANCH:-master}"
printf '\n%s/%s\n' "$PWD" "${CYAN}${URL##*/}${STYLE_RESET}"
curl -#L "$URL" | tar -xzf - --strip=1
}
EOF
)
fn___dl_github_asset=$(
cat <<'EOF'
BASH_FUNC___dl_github_asset%%=() {
local RE FQDN URI ASSET_URL RELEASE MSG
FQDN='https://api.github.com'
RELEASE="${3+tags/$3}"
RE="${2//\\/\\\\}"
REPO="$1"
URI="/repos/$REPO/releases/${RELEASE:-latest}"
JQ_CMD='[.assets[] | { asset:.name, url:.browser_download_url }]'
ASSETS="$( curl -sSL "$FQDN$URI" | "$JQ" "$JQ_CMD" )"
if [ $VERBOSE ]; then
printf "Assets found for %s:\n\n%s\n" "$NAME" "$ASSETS"
fi
JQ_CMD=$( printf '.[] | select( .asset | test("%s"; "i")).url' "$RE" )
ASSET_URL=$( echo "$ASSETS" | "$JQ" -r "$JQ_CMD" )
# if asset not found, search search using compatible architectures
if [ -z "$ASSET_URL" ]; then
case "$ARCH" in
*amd64*)
ALT_RE_1="${RE//amd64/x86_64}" ;;
*x86_64*)
ALT_RE_1="${RE//x86_64/(amd64|AMD64)}" ;;
aarch64)
ALT_RE_1="${RE//aarch64/(arm64|ARM64)}" ;;
aarch64_be)
ALT_RE_1="${RE//aarch64_be/(aarch64|AARCH64|arm64|ARM64)}" ;;
armv8b)
ALT_RE_1="${RE//armv8b/(aarch64|AARCH64|arm64|ARM64)}" ;;
armv8l)
ALT_RE_1="${RE//armv8b/(aarch64|AARCH64|arm64|ARM64)}" ;;
*i386*)
ALT_RE_1="${RE//i386/(i?386|i?686)}" ;;
*i586*)
ALT_RE_1="${RE//i586/(i?386|i?686)}" ;;
*i686*)
ALT_RE_1="${RE//i686/(i?386|i?686)}" ;;
*)
err "Machine architecture $ARCH not currently supported by provii." \
"Failed to download binary for $NAME." ;;
esac
if [ $VERBOSE ]; then
echo "Searching assets using architectures compatible with $ARCH" \
"because using default value for ARCH, $ARCH, yeilded nothing." \
"\nUsing the following regular expression: $ALT_RE_1."
fi
JQ_CMD=$( printf '.[] | select( .asset | test("%s"; "i")).url' "$ALT_RE_1" )
ASSET_URL=$( echo "$ASSETS" | "$JQ" -r "$JQ_CMD" )
fi
# if asset not found, search using memory addressing scheme (32/64) for architecture
if [ -z "$ASSET_URL" ]; then
re64='x?64(bit)?'
re32='32(bit)?|x86[^_]'
case "$ARCH" in
amd64)
ALT_RE_2="${RE//amd64/"$re64"}" ;;
*x86_64*)
ALT_RE_2="${RE//x86_64/"$re64"}" ;;
aarch64)
ALT_RE_2="${RE//aarch64/"$re64"}" ;;
aarch64_be)
ALT_RE_2="${RE//aarch64_be/"$re64"}" ;;
armv8b)
ALT_RE_2="${RE//armv8b/"$re64"}" ;;
armv8l)
ALT_RE_2="${RE//armv8l/"$re64"}" ;;
*i386*)
ALT_RE_2="${RE//i386/"$re32"}" ;;
*i586*)
ALT_RE_2="${RE//i586/"$re32"}" ;;
*i686*)
ALT_RE_2="${RE//i686/"$re32"}" ;;
*)
err "Machine architecture $ARCH not currently supported by provii." \
"Failed to download binary for $NAME." ;;
esac
if [ $VERBOSE ]; then
echo "Searching assets using 64bit or 32bit based on value for ARCH, $ARCH," \
"because searching assets using comptabile architecture names, $ALT_RE_1 failed." \
"\nUsing the following regular expression: $ALT_RE_2."
fi
JQ_CMD=$( printf '.[] | select( .asset | test("%s"; "i")).url' "$ALT_RE_2" )
ASSET_URL=$( echo "$ASSETS" | "$JQ" -r "$JQ_CMD" )
fi
# if asset not found, all options have been exhausted at this point
if [ -z "$ASSET_URL" ]; then
RESPONSE_1A="No Github asset found for $REPO that matches the following regular expressions:"
RESPONSE_1B="$( printf '\n\t\t%s' \
"${CYAN}$RE${STYLE_RESET}" \
"${ALT_RE_1:+${CYAN}$ALT_RE_1${STYLE_RESET}}" \
"${ALT_RE_2:+${CYAN}$ALT_RE_2${STYLE_RESET}}" \
)"
RESPONSE_2A="$(printf '%s%s' \
"$_ERROR_SPACER" \
"The following assets were found for repository $REPO:" \
)"
RESPONSE_2B="$( printf '\n\t\t%s' \
$(echo "$ASSETS" | "$JQ" -r '.[].asset' ) \
)"
MSG="$( printf '%s\n%s\n\n%s\n%s' \
"$RESPONSE_1A" "$RESPONSE_1B" \
"$RESPONSE_2A" "$RESPONSE_2B" \
)"
err "$MSG"
fi
if [ "$(echo "$ASSET_URL" | wc -w)" -gt 1 ]; then
SUBMSG=$(printf \
'Regular expression %s matched more than one Github asset.' \
"${CYAN}${RE}${STYLE_RESET}" )
JQ_CMD=$( printf '.[] | select( .asset | test("%s"; "i")).asset' "$RE" )
ASSET_NAMES=$( echo "$ASSETS" | "$JQ" -r "$JQ_CMD" )
MSG="$( printf '%s Assets matched:\n\n%s\n' "$SUBMSG" "$ASSET_NAMES" )"
err "$MSG"
fi
printf '\n%s/%s\n' "$PWD" "${CYAN}${ASSET_URL##*/}${STYLE_RESET}"
curl -#LO "$ASSET_URL"
}
EOF
)
fn___dl_github_file__=$(
cat <<'EOF'
BASH_FUNC___dl_github_file__%%=() {
FQDN='https://api.github.com'
BRANCH="${3+\?ref=$3}"
URI="$FQDN/repos/$1/contents/$2$3"
while read -r FILE; do
PATH_DEPTH=$(echo "$FILE" | tr / ' ' | wc -w)
if [ $PATH_DEPTH -gt 1 ]; then
mkdir -p $(dirname "$FILE")
fi
read -r URL
# download URL shows 'null' when $FILE is a directory
if [ "$URL" == 'null' ]; then
__dl_github_file__ "$REPO" "$FILE"
fi
printf '\n%s/%s\n' "$PWD" "${CYAN}${URL##*/}${STYLE_RESET}"
curl -#L -o "$FILE" "$URL"
done < <( curl -sSL "$URI" | "$JQ" -r '
if type == "object"
then .path,.download_url
else .[] | .path, .download_url
end' )
}
EOF
)
fn_github=$(
cat <<'EOF'
BASH_FUNC_github%%=() {
local REPO BRANCH PATH_TO_FILE ASSET_RE
JQ="$( command -v jq || echo "$JQ_DEFAULT_DIR/jq" )"
PATH_TO_FILE=$(echo "$1" | cut -d/ -f3-)
REPO=$(echo "$1" | cut -d/ -f-2)
# ...are we downloading repo files?
if [ "$PATH_TO_FILE" ]; then
case $# in
1) __dl_github_file__ "$REPO" "$PATH_TO_FILE" ;;
2)
BRANCH="$2"
__dl_github_file__ "$REPO" "$PATH_TO_FILE" "$BRANCH"
;;
*)
echo "$_github_usage"
return 1
;;
esac
# ...or repo assets?
else
case $# in
2)
ASSET_RE="$2"
__dl_github_asset "$REPO" "$ASSET_RE"
;;
3)
BRANCH="$2"
ASSET_RE="$3"
__dl_github_asset "$REPO" "$ASSET_RE" "$BRANCH"
;;
1)
REPO="$1"
__dl_github_tarball "$REPO"
;;
*)
echo "$_github_usage"
return 1
;;
esac
fi
}
EOF
)
log() {
true
}
warn() {
echo "warn:" "$@"
}
_ERROR_PREFIX='Error: '
_ERROR_SPACER="$(printf '%*s' "$(printf %s "$_ERROR_PREFIX" | wc -c)" ' ')"
export _ERROR_PREFIX _ERROR_SPACER
err() {
printf "\n%s%s" "${STYLE_ERROR}$_ERROR_PREFIX${STYLE_RESET}" "$*" | fmt -w 80
exit 1
}
fn_log="$(
cat <<'EOF'
BASH_FUNC_log%%=() {
FMT='+%Y-%m-%d %T'
if [ $1 == '--created' ]; then
# shift && printf '%s %s %s\n' "$(date "$FMT")" created_file "$@" >> $LOG
true
else
printf '%s %s %s' "$(date "$FMT")" info "$@" >> $LOG
fi
}
EOF
)"
fn_warn="$(
cat <<'EOF'
BASH_FUNC_warn%%=() {
echo "Warning:" "$@"
}
EOF
)"
fn_err="$(
cat <<'EOF'
BASH_FUNC_err%%=() {
printf "\n%s%s" "${STYLE_ERROR}$_ERROR_PREFIX${STYLE_RESET}" "$*" | fmt -w 80
exit 1
}
EOF
)"
PRINT_VARIABLES="$(
cat <<'EOF'
set | grep -e ^NAME -e ^SCOPE -e ^OS -e ^ARCH -e ^LIBC \
-e ^BIN -e ^MAN -e ^BASH_COMP -e ^ZSH_COMP -e ^CACHE -e ^LOG | awk \
-v style_hdr=$STYLE_HEADER \
-v style_vars=$STYLE_VARS \
-v no_style=$STYLE_RESET \
'
BEGIN {
FS="="
}{
vars[$1] = $2
}
END {
fmt_hdr=sprintf("\n%s%%s%%*s%s\n\n", style_hdr, no_style)
max_width_1 = 0
max_width_2 = 0
for (i in vars) {
width_1 = length(i)
if ( width_1 > max_width_1 ) max_width_1 = width_1
width_2 = length(vars[i])
if ( width_2 > max_width_2 ) max_width_2 = width_2
}