-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
1170 lines (977 loc) · 32.6 KB
/
.functions
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
#----- header -----
[ "${0##*/}" != "${BASH_SOURCE##*/}" ] || { >&2 echo -e "ERROR\tfile must be sourced ($0)"; return 2; }
[[ "$BASH_VERSION" =~ 5|4.[2-4] ]] || { >&2 echo -e 'ERROR\tbash ~> 4.2 required'; exit 126; }
#------------------
set -o pipefail
shopt -s extglob
#pedant, ref: http://mywiki.wooledge.org/glob#nullglob
shopt -u nullglob
#NOTE 'command' is unsuitable since returns aliases and functions
function __WHICH() { \which "$@"; }
function __READLINK() { \readlink ${VERBOSE:+ -v} "$@"; }
#function __YQ() { \yq ??? "$@"; }
function __JQ() { \jq --exit-status "$@"; }
function __JQR() { \jq "$@" | \jq --exit-status --raw-output '. // empty'; }
#TODO call it merge1 since it ONLY handles 1st level
function __JQ_merge() { \jq --slurp 'reduce .[] as $item ({}; . * $item)'; }
#TODO merge_deep(er)
#jq -s '[.[] | to_entries] | flatten | reduce .[] as $dot ({}; .[$dot.key] += $dot.value)'
#TODO when key1: valu1, key1: valu2 -> key1: [ valu1, valu2 ]
# also maybe something like group_by(.key) | map({key:.[0].key,value:(map(.value) | join(" "))})
#def merge_at_key(separator):
# reduce .[] as $o
# ([];
# $o["key"] as $k
# | (map(.key) | index($k)) as $i
# | if $i then (.[$i] | .value) += (separator + $o["value"])
# else . + [$o] end);
# I think we can rewrite ^^ better
#
# Recursively meld a and b,
# concatenating arrays and
# favoring b when there is a conflict
#def meld(a; b):
# a as $a | b as $b
# | if ($a|type) == "object" and ($b|type) == "object"
# then reduce ([$a,$b]|add|keys_unsorted[]) as $k ({};
# .[$k] = meld( $a[$k]; $b[$k]) )
# elif ($a|type) == "array" and ($b|type) == "array"
# then $a+$b
# elif $b == null then $a
# else $b
# end;
# then invoked as: jq -f meld.jq 1.json 2.json 'reduce inputs as $i (.; meld(.; $i))
#
# one-shot function definition and invocation
#$ jq -s 'def deepmerge(a;b):
# reduce b[] as $item (a;
# reduce ($item | keys_unsorted[]) as $key (.;
# $item[$key] as $val | ($val | type) as $type | .[$key] = if ($type == "object") then
# deepmerge({}; [if .[$key] == null then {} else .[$key] end, $val])
# elif ($type == "array") then
# (.[$key] + $val | unique)
# else
# $val
# end)
# );
# deepmerge({}; .)' file1.json file2.json
function __CURL() { \curl --connect-timeout 3 --fail --silent --location ${VERBOSE:+ '--verbose' '--progress-bar'} "$@"; }
# override 'exit on error' program flow
function __continue() { [ -n "$CONTINUE" ]; }
function pause() {
local _default='Press [ENTER] to continue ... '
read -p "${*:-$_default}"
}
function confirm() {
read -n 1 -t 15 -p 'Are you sure (y/N)? '
echo
[ "${REPLY^^}" = "Y" ]
}
# mimic 'set -x' and send STDERR to file
function runv() {
local indent=`printf '%.0s+' {1..$SHLVL}`
#TODO wrap arguments in " for easy copy+paste
>&2 printf '%s %s\n' "$indent" "$*"
# set to <blank> is insufficient
${NOOP:+return 0}
if [ `to_int $DEBUG` -gt 1 -o `to_int $TRACE` -eq 1 ]; then
local command=${FUNCNAME[1]:-$1}
local outf=`mktemp -t "${command##*/}-XXXXXXXX"`
exec > >( tee "$outf" ) 2> >( tee "${outf}.err" >&2 )
>&2 caller 1
fi
"$@"
}
# likely to be invoked in subshell
export -f runv
#TODO is_what() returns 'type -t' or 'declare -p' mapped back to English
#function is_what
function __is_type() { #WARN! selective short-circuit on first error
local -Ar __types=(
['a']='array' ['array']='a'
['A']='hash' ['hash']='A'
['f']='function' ['function']='f'
['i']='integer' ['integer']='i'
['l']='lower' ['lower']='l'
['n']='nref' ['nref']='n'
['-']='string' ['string']='-'
['r']='readonly' ['readonly']='r'
['u']='upper' ['upper']='u'
)
local keyword=${FUNCNAME[1]#is_}
local DEBUG QUIET=1 VERBOSE
local -i use_stdin=0
local -i OPTIND; local opt OPTARG long_opts=()
while getopts ':hdqvS' opt; do
case "$opt" in
d) DEBUG=1 ;;
q) QUIET=1 ;;
v) VERBOSE=1;;
S) use_stdin=1 ;;
:) log.error "missing argument (${!OPTIND})" ;;&
\-) [[ "$OPTARG" =~ \= ]] || log.notice "assuming flag (--${OPTARG})"
long_opts+=( "--${OPTARG}" )
(( OPTIND++ ))
;;
\?) #long_opts+=( "-${OPTARG}" ) ;;
log.error "unsupported option (-${OPTARG})" ;&
h|*) >&2 cat << EOF
Usage: $FUNCNAME ...
EOF
esac
done
shift $((OPTIND - 1))
#FIXME naked '-' implies problem with getopts
[ "$1" != '-' ] || { log.error "naked dash (-) argument"; return; }
# evaluate verbosity flags
[ -n "${DEBUG}${VERBOSE}" ] && unset QUIET
# read STDIN (pipe) if no args
(( ${use_stdin:-0} )) && set -- $( < /dev/stdin )
(( $# )) || return
case "${keyword:?}" in
dir*|file)
while (( $# )); do
if [[ "$keyword" =~ dir ]]; then [ -d "$1" ]; else [ -f "$1" ]; fi || {
[ -n "$QUIET" ] || log.error "$keyword not found (${1:?})"; return 1; }
shift
done
return
;;
exec*) local bin
while (( $# )); do
#WARN 'command' returns matches on alias and functions
bin=$( __WHICH "${1:?}" 2>/dev/null ) || {
[ -n "$QUIET" ] || log.error "$keyword not found (${1:?})"; return 1; }
${VERBOSE:+echo "$bin"}
shift
done
return
;;
func*) while (( $# )); do
declare -F "$1" &>/dev/null || {
[ -n "$QUIET" ] || log.error "$keyword not found ($1)"; return 1; }
${VERBOSE:+whereis_function "$1"}
shift
done
return
;;
read*) local -i _readonly=1 ;;
*) [ -n "${__types[$keyword]}" ] || {
log.error "unsupported type ($keyword)"; return 2; }
esac
local whatami flag=
while (( $# )); do
# naked '-' not absorbed by getopts()
[ "$1" != '-' ] || return
# suss out functions during 'readonly'
[ "$( type -t "$1" )" = 'function' ] && flag='-F'
whatami=$( declare -p $flag "$1" 2>/dev/null | awk '{ print $2; }' ) || {
log.error "type detection failed ($1)"; return 1
}
(( $_readonly )) && whatami=${whatami//[^r]/} || whatami=${whatami//[r-]/}
[[ "${whatami#-}" =~ ${__types[$keyword]} ]] || {
[ -n "$QUIET" ] || log.error "type mismatch ($1: ${__types[${whatami#-}]} != $keyword)"
return 1
}
shift
done
}
# data-type wrappers
for f in array dir{,ectory} exec{,utable} file hash integer string function readonly nref; do
eval function is_$f '{ __is_type "$@"; }'
done
#alt: grep -qE '^0x[0-9a-f]+$|[0-9]+$' - -- <<< "$@"
function is_number() { to_int $1 &>/dev/null; }
function is_interactive() { [[ $- =~ i ]] || tty -s || [ -n "`tset -q`" ]; }
#DO NOT MOVE!!
# Properly belongs in '.functions_os[.cygwin]' but required for various
# scripts and helper functions. Leaving it here simplifies includes.
function is_windows() {
if [ $# -eq 0 ]; then
[[ "${OSTYPE:-`uname -o`}" =~ [cC]ygwin|[mM]sys ]]
return
fi
local bin
#WARN multi-arg supported, but not recommended
while (( $# )); do
for op in 'echo' 'readlink -e' 'is_exec -v'; do
bin=`$op "$1"` || continue
if [ "${bin:0:1}" = '/' ]; then break; fi
done
[[ "${bin:?}" =~ ^/cygdrive ]] || { file "$bin" | grep -q 'for MS Windows'; } || return
shift
done
}
readonly -f is_windows
function __is_markup() {
: ${cmd:?detection command} # magic injection
local -i rc
{ # logging wrapper
if [ $# -eq 0 ]; then
"${cmd[@]}"
else
while (( $# )); do
if [ -f "$1" ] || [[ "${1:0:1}" =~ \.|/ ]]; then
"${cmd[@]}" < "$1"
elif [ -n "$1" ]; then
"${cmd[@]}" <<< "$1"
else
false
fi || { rc=$?; break; }
shift
done
fi
: ${rc=$?}
} 2>&1 >/dev/null | log.debug
# fancy-pants I/O redirection above will SIGPIPE (128+13) on no error
#ref: https://unix.stackexchange.com/a/254747, signal(7)
[ $rc -eq 0 -o $? -eq 141 ] || {
[ -n "${VERBOSE}" ] && log.error "invalid input" "$1"
return 1
}
}
#NOTE 'yaml' module accepts both JSON and self
function is_yaml() {
cmd=( python '-c' 'import yaml, sys; yaml.safe_load(sys.stdin)' ) \
__is_markup "$@"
}
function is_json() {
#alt: jq --exit-status . <file|string>
cmd=( python '-c' 'import json, sys; json.load(sys.stdin)' ) \
__is_markup "$@"
}
function define() {
# Assign a HEREDOC to a variable.
# To collapse space/tab indentation use dash in indirection like so '<<-_EOF'
# Contents will be expanded unless marker ('_EOF') is quoted.
#
# Usage: define VAR <<_EOF ...
#TODO? </dev/stdin
IFS=$'\n' read -r -d '' "$1" || true
}
#TODO? extend to other types
function whereis_function() (
shopt -s extdebug
while (( $# )); do
declare -F "${1:?}"
shift
done | awk '{ printf("%-25s\t%-50s #%d\n", $1, $3, $2); }'
)
function list_functions() (
local format='%-25s\t%-50s #%d\n'
if [ $# -eq 0 ]; then
shopt -s extdebug; declare -F `compgen -A function`
else
while (( $# )); do
[ -s "$1" ] || { shift; continue; }
env -i $SHELL --noprofile --norc -s 2>/dev/null <<- _EOF
source "$1"
shopt -s extdebug
# sub-shell loses sourced contents
compgen -A function | while read; do declare -F "\${REPLY}"; done
_EOF
shift
done
fi | awk -v fmt="$format" '{ printf fmt, $1, $3, $2; }'
)
function copy_function() {
# arg1: source name or fully declared format
# argN: destination
#
# Does NOT support recursion since fails easily or in unintended ways.
# Using SED with BOL, EOL or whitespace detection isn't reliable either.
local fname=${1:?source} body
local -i overwrite=0
shift
#FIXME flip IF test over but check length of $1, also remove assignment at top
#also the formatting assumptions is wildly dangerous!
if grep --quiet -e '() {' - -- <<< "$fname" ; then
body=$fname
fname=`awk '{print $1; exit}' <<< "$body"`
else
# allow side-channel injection ???
: ${body:=`declare -f "$fname"`}
fi
[ -n "$fname" -a -n "$body" ] || return
for target in "$@"; do
# ignore badly-formed arguments
[ -n "$target" ] || continue
#log.* may not be defined as yet
if declare -F "$target" &>/dev/null; then
if [ ${overwrite:-0} -eq 1 ]; then
# is_readonly "$target" && {
# [ -n "$VERBOSE" ] && log "ERROR\toverwrite read-only function ($target)"
# return 1
# }
[ -n "$VERBOSE" ] && log "NOTICE\toverwriting function ($target)"
else
[ -n "$VERBOSE" ] && log "WARN\tfunction exits ($target)"
continue
fi
fi
#alt: "function $target ${body#*\(\)}"
eval "${body/$fname/$target}"
done
}
function rename_function() {
copy_function "${1:?source}" "${2:?dest}" && unset -f "$1"
}
function convert_path() {
# Usage: caller use 'while read()' one entry per line for whitespace
# management if providing multiple arguments, assign to array, or set --
#
#WARN validity/existence of the path is NOT checked!
# also makes NO attempt to detect/expand wildcard patterns
#
# Cygwin handles whitespace in "$PATH" and they must remain un-escaped.
# This *magical* behavior does NOT extend to other PATH-like variables!
# eg. GOPATH, RUBYPATH, RUBYLIB, PUPPET_MODULE_PATH, JAVA_HOME
#
# However, interactive use of paths must be inside quotes or escaped
# since the $SHELL parser is not so gifted.
#
# cygpath only modifies the first occurance of '/cygdrive' or '[A-Z]:'
# unless '-p' but delimiter in input MUST consistently match opposite
# of desired output format (';' for Windows, ':' for Unix). Any intermix
# or requesting Windows output from Windows input yields garbage. But
# Unix output from Unix input is usually benign.
local QUOTE quote flags=()
# do NOT default quote character
[ -n "${QUOTE+X}" ] && : ${quote=$QUOTE}
local -i escape=1 abs=0
local -i OPTIND; local opt OPTARG long_opts=()
while getopts ':haeEq:t:-:' opt; do
case "$opt" in
a) abs=1 ;;
e) escape=1 ;; # default
E) escape=0 ;;
q) quote=$OPTARG; readonly escape=0 ;;
t) long_opts+=( '--type' "$OPTARG" ) ;;
\-) long_opts+=( "--${OPTARG}" )
[[ "$OPTARG" =~ \= ]] || log.notice "assuming flag (--${OPTARG})"
;;
:) log.error "missing argument (${!OPTIND})" ;;&
# pass-thru unhandled args to cygpath
\?) long_opts+=( "-${OPTARG}" ) ;;
h|*) >&2 cat << EOF
Usage: $FUNCNAME ...
EOF
esac
done
shift $((OPTIND - 1))
local _format="${quote}%s${quote}\n"
[ ${escape:-0} -eq 1 ] && _format='%q\n'
local IFS=$'\n'
[ ${abs:-0} -eq 1 ] && set -- `readlink -m "$@"`
if is_windows; then
set -- `cygpath "${long_opts[@]}" -- "$@" 2>/dev/null`
fi
printf "$_format" "$@"
}
#TODO
#function convert_path=os.filepath.print
#TODO has_value(s), has_key(s)
#FIXME keys/values use same nref detection
function array.contains() {
#Usage: <array|hash> value(s) ...
(( $# > 1 )) || return 2
local -n object
is_nref "${1:?array}" && eval object="\${!$1}" || object=$1
is_array -v "${!object}" || return
shift
#compute once
local elements=${object[*]}
#if keys() then ${!object[*]}
while (( $# )); do
#NOTE grep matches on blank which is not helpful
[ -n "$1" ] || { shift; continue; }
grep --quiet --word-regexp "$1" -- <<< "$elements" || return
shift
done
}
function string.contains() {
#Usage: <object> substring(s) ...
local DELIM delim op flags=()
[ -n "${DELIM+X}" ] && : ${delim=$DELIM}
local -i skip_blank=0 use_stdin=0 reverse_arg=0 exact=0
#FIXME rename flags to grep_opts, and use assoc array of flags([use_stdin]=0 etc
local -i OPTIND; local OPTARG opt #long_opts=()
while getopts ':hD:o:RSxz' opt; do
case "$opt" in
D) delim=$OPTARG ;;
o) op=$OPTARG ;;
S) use_stdin=1 ;;
R) reverse_arg=1 ;;
x) exact=1 ;;
z) skip_blank=1 ;;
:) log.error "missing argument (-$OPTARG)" ;;&
#WARN getopts blindly consumes 1 char at a time, ignores word splitting
# so will walk an argument string with embedded '-' instead of stopping
#
# assume unhandled flags are 'grep' options
\?) : ${op=grep}
#FIXME test OPTIND for longopt
#[[ "${!OPTIND}" =~ ^--[a-z] ]] with '='?
flags+=( "-$OPTARG" )
;;
h|*) >&2 cat << _EOF
Usage: $FUNCNAME [ options ] <object> [ substring ... ]
-D delimiter char(s)
-o method of comparison: grep, pe (parameter expansion), regex (bash)
-S use STDIN for input string(s)
-z ignore empty strings, else will ERROR
-R reverse argument order; <substring> [ object ... ]
...
_EOF
esac
done
shift $((OPTIND - 1))
#TODO handle nref like array.contains()
local object=${1:?object}; shift
(( $# )) || { [ ${use_stdin:-0} -eq 1 ] && set -- $( < /dev/stdin ); }
(( $# )) || return 2
function __compare() {
case "$op" in
pe) [ "${2#*$1}" != "$2" ] ;;
regex) [[ "$2" =~ $1 ]] ;;
exact) [[ "$2" == "$1" ]] ;;
*) [[ "$2" == *"$1"* ]]
esac
}
(( $reverse_arg )) && local _save=$object
while read sub; do
# empty string is 'false' but can be ignored
if [ -z "$sub" ]; then
(( $skip_blank )) && continue || return
fi
(( $reverse_arg )) && { object=$sub; sub=$_save; }
if [ "$op" == 'grep' ]; then
[ -n "$delim" ] && sub="${delim}\?${sub}${delim}\?"
grep --quiet "${flags[@]}" ${delim:+'--extended-regexp'} "$sub" - -- <<< "$object"
elif [ -n "$delim" ]; then
local _IFS=$delim
(( ${#delim} > 1 )) && _IFS=$'§' #(shell: Alt+0167, vi: C-v+167)
IFS=$_IFS read -r -a tokens <<< "${object//$delim/$_IFS}"
local -i found=0
for tok in "${tokens[@]}"; do
__compare "$sub" "$tok" && { found=1; break; }
done
(( $found ))
else __compare "$sub" "$object"; fi || return
done < <( IFS=$'\n'; echo "$*" )
}
#TODO define is_substring() to call above
function addPath() {
# pre-/post-pend [multiple] elements to an environment variable
# but does NOT sub-divide arguments!
#TODO use string.join() to handle arbitrary delimiters and whitespace
#TODO? rewrite callers as PATH=`DELIM=$delim string.join -v <VARNAME> $1 $2`
local PREPEND k
local -i prepend=0 use_stdin=0
[ -n "${PREPEND+X}" ] && prepend=1
local -i OPTIND; local OPTARG opt
while getopts ':hk:PS' opt; do
case "$opt" in
k) k=$OPTARG ;;
P) prepend=1 ;;
S) use_stdin=1 ;;
# [dvq]) dqv+=( "-$OPTARG" )
:) log.error "missing argument (-$OPTARG)" ;;&
\?) log.error "unsupported option (-$OPTARG)" ;&
h|*) >&2 cat <<_EOF
Usage: $FUNCNAME -k VAR path...
_EOF
return 2
esac
done
shift $((OPTIND - 1))
(( $# )) || { [ ${use_stdin:-0} -eq 1 ] && set -- $( < /dev/stdin ); }
(( $# )) || return 2
local delim flags=()
case "${k:-$1}" in
''|?(-|.)*/*) k=PATH ;&
# '-a' unroll symlinks for accurate comparison
PATH) delim=':'; flags+=( '-Epa' ) ;;
#TODO GOPATH, RUBYPATH etc. use PATHSEP
esac
: ${delim:=${PATHSEP:-':'}}
local -n kval=${k:?VAR_name}
#FIXME detect if new path already has ^/cygpath and don't invoke cygpath()
local -ir __prepend=${prepend:-0} #save
while read item; do
[ -n "$item" ] || { shift; continue; }
prepend=$__prepend #restore
is_dir "$item" || continue
#TODO set IFS=$delim instead or use explicit arg
if ! string.contains -D "$delim" "$kval" "$item"; then
[ $prepend -eq 1 ] && kval="${item}${delim}${kval}" || kval+="${delim}${item}"
log.debug "add element to $k ($item)"
fi
shift
done < <( convert_path "${flags[@]}" "$@" )
# remove leading, trailing and any leftover repeated delimiter
kval=${kval##$delim}; kval=${kval%%$delim}
kval=${kval//${delim}${delim}/$delim}
}
#TODO simply treat as DELETE=1 addPath
function rmPath() {
: ${1:?VAR_name or path}
local k delim
case "${k:=$1}" in
# VAR unspecified
?(.)/*) k=PATH ;&
PATH) delim=':' ;;
#GOPATH, RUBYPATH etc. use PATHSEP
esac
: ${delim:=${PATHSEP:-':'}}
local -n kval=${k:?VAR_name}
# split into tokens. unset array element to erase
#alt: eval $k=`loop`
kval=$( IFS=$delim
read -ra items <<< "${!k}"
while read line; do
for i in "${!items[@]}"; do
if [ "${items[i]}" = "$line" ]; then unset 'items[i]'; log.debug "remove element ($i, $line)"; fi
done
done < <( readlink -m "$@" )
echo "${items[*]}"
)
# remove leading, trailing and any leftover repeated delimiter
kval=${kval##$delim}; kval=${kval%%$delim}
kval=${kval//${delim}${delim}/$delim}
}
#TODO? if QUOTE=1 use printf '%q'
function string.join() {
local DELIM delim QUOTE quote ESCAPE escape
[ -n "${DELIM+X}" ] && delim=$DELIM
[ -n "${QUOTE+X}" ] && quote=$QUOTE
[ -n "${ESCAPE+X}" ] && escape=$ESCAPE
local flags=
local -i OPTIND; local opt OPTARG
while getopts ':d:e:lq:uz' opt; do
case "$opt" in
d) delim=$OPTARG ;;
e) escape=$OPTARG ;;
l) flags+=l ;; # to_lower
q) quote=$OPTARG ;;
u) flags+=u ;; # to_upper
z) flags+=z ;; # remove blanks
:) log.error "missing argument (-$OPTARG)" ;;&
\?) log.error "unsupported option (-$OPTARG)" ;&
h|*) >&2 cat <<_EOF
Usage: $FUNCNAME [ options ] <str> [<str> ...]
-l to lower case
-u to upper case
-z remove blank args
_EOF
return 2
esac
done
shift $((OPTIND - 1))
# legacy invocation
[ -n "${delim+X}" ] || { delim=$1; shift; }
# escape whitespace, or even delimiter (rare)
if [ -n "$quote" ]; then
[ "$quote" = "$delim" ] && escape=$delim
else
: ${escape=${IFS:0:1}}
fi
# unset empty args and remove holes
if [[ $flags =~ z ]]; then
local -a args=( "$@" )
for i in "${!args[@]}"; do
[ -n "${args[$i]}" ] || unset 'args[i]'
done
set -- "${args[@]}"
fi
[ -n "$escape" ] && set -- "${@//$escape/\\$escape}"
#WARN unintended side-effects
[[ "${escape}${delim}${quote}" =~ [a-zA-Z] && "$flags" =~ l|u ]] &&
log.warn "case change poses severe risk (escape=${escape}, delim=${delim}, quote=${quote})"
[[ "$flags" =~ l ]] && set -- "${@,,}"
[[ "$flags" =~ u ]] && set -- "${@^^}"
# shortcut
[ -z "$quote" -a ${#delim} -le 1 ] && { local IFS=$delim; echo "$*"; return; }
# prepend each word during expansion
local IFS=
local str=${*/#/${quote}${delim}${quote}}
# strip leading 'delim' and tack on trailing 'quote'
echo "${str#${quote}${delim}}$quote"
}
# backwards compat
function join_string() { log.warn "DEPRECATED! use 'string.join()'"; string.join "$@"; }
function join_quote() { log.warn "DEPRECATED! use 'string.join()'"; QUOTE=\' string.join "$@"; }
# using 'echo' requires IFS='\n'. TODO
function to_upper() { if (( $# )); then local IFS=$'\n'; echo "${*^^}"; else tr '[:lower:]' '[:upper:]'; fi }
function to_lower() { if (( $# )); then local IFS=$'\n'; echo "${*,,}"; else tr '[:upper:]' '[:lower:]'; fi }
function bool_to_int() {
# Usage: [-u] <value|VAR_NAME> ...
local -i to_bool unset
local -i OPTIND; local opt OPTARG
while getopts ':r' opt; do
case "$opt" in
# u) unset=1 ;;
r) to_bool=1 ;;
# :)
\?) log.error "unsupported option (-$OPTARG)" ;&
h|*) >&2 cat <<_EOF
Usage: $FUNCNAME ...
_EOF
return 2
esac
done
shift $((OPTIND - 1))
case "${1,,}" in
0|false) if [ ${to_bool:-0} -eq 1 ]; then echo 'false'; else echo 0; false; fi ;;
1|true) if [ ${to_bool:-0} -eq 1 ]; then echo 'true'; else echo 1; true; fi ;;
# # assign truthiness to named variable, if false optionally unset it
# [a-z]*) if [[ "$BASH_VERSION" =~ 5|4.[3-9] ]]; then
# local -n vref; vref=$1 || return
# vref=`$FUNCNAME "$vref"`
# else
# local v=${1^^}
# $FUNCNAME ${!v}
# fi
# [ ${unset:-0} -eq 1 ] && unset $1
*) return 2
esac
}
function to_bool() { bool_to_int -r "$1"; }
function to_int() { printf '%d\n' "${@:-X}" 2>/dev/null; }
#function __GETOPT() {
#https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
#https://gist.github.com/kaushalmodi/74e9875d5ab0a2bc1010447f1bee5d0a
#getopt --longoptions 'file:,verbose' --options '+vf:' -- --verbose -f foobar another thing
#yields: --verbose -f 'foobar' -- 'another' 'thing'
#
#Usage: __GETOPT [--options ${getopts_string without leading ':'}] [--longoptions <longopt] --name ${0:-$FUNCNAME} -- "$@"
# for any items that LOOK like options (leading '-') either put them behind an *obvious* not an option, OR put '--' between arg sequence you want parsed and "ignore everything after"
#consume via:
# set -- `__GETOPT ...` || return 2 from parse error or getopt is the old version
#otherwise parsed_args=( `__GETOPT ...` )
#
# while (($#)); do
# case "$1" in
# --) shift; break; # reached end of short+long options. tack $@ onto downstream command
# -s|--long) stuff; items+=( $2 ); shift
# esac
# shift
# done
#
# http://mywiki.wooledge.org/BashFAQ/035
# IFS='
# '
# if [[ $option =~ (\[((no|dont)-?)\]). ]]; then
# option2=${option/"${BASH_REMATCH[1]}"/}
# option2=${option2%%[<{().[]*}
# printf '%s\n' "${option2/=*/=}"
# option=${option/"${BASH_REMATCH[1]}"/"${BASH_REMATCH[2]}"}
# fi
# option="${option%%[<{().[]*}"
# printf '%s\n' "${option/=*/=}"
# }
function version_to_int() (
shopt -s nocaseglob
: ${1:?}
# superficial input sanitation
set -- "${@//-/.}"; set -- "${@//[^0-9]/}"
while (( $# )); do
printf '%.3d' ${1//./ } #deliberate un-quoted
echo; shift
done
)
# dpkg --compare-versions $A <OP> $B
#
# use parameter expansion to replace dot by space, how about '-rcX'?
#
#xarr=(${CurrV//./ })
#yarr=(${ExpecV//./ })
#
#
# suppose that ExpecV is newer (bigger) or equal to CurrV version:
#
#isnewer=true
#
#
# loop over array keys:
#
#for i in "${!xarr[@]}"; do
# if [ ${yarr[i]} -gt ${xarr[i]} ]; then
# break
# elif [ ${yarr[i]} -lt ${xarr[i]} ]; then
# isnewer=false
# break
# fi
#done
#
#function compare_versions {
# local a=${1%%.*} b=${2%%.*}
# [[ "10#${a:-0}" -gt "10#${b:-0}" ]] && return 1
# [[ "10#${a:-0}" -lt "10#${b:-0}" ]] && return 2
# a=${1:${#a} + 1} b=${2:${#b} + 1}
# [[ -z $a && -z $b ]] || compare_versions "$a" "$b"
#}
#
#simple greater-than
# [ bigger = `printf '%s\n' $ver1 $ver2 | sort --check=quiet --version-sort --stable | head -n 1` ]
#
# ref: https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
# handle x.y.z with optional '-XX##' or 1.4.0b2
# see also https://dazuma.github.io/versionomy/
#version_compare() {
# if [[ $1 =~ ^([0-9]+\.?)+$ && $2 =~ ^([0-9]+\.?)+$ ]]; then
# local l=(${1//./ }) r=(${2//./ }) s=${#l[@]}; [[ ${#r[@]} -gt ${#l[@]} ]] && s=${#r[@]}
#
# for i in $(seq 0 $((s - 1))); do
# [[ ${l[$i]} -gt ${r[$i]} ]] && return 1
# [[ ${l[$i]} -lt ${r[$i]} ]] && return 2
# done
#
# return 0
# else
# echo "Invalid version number given"
# exit 1
# fi
#}
#
#function compare_versions() {
# # Trivial v1 == v2 test based on string comparison
# [[ "$1" == "$2" ]] && return 0
#
# # Local variables
# local regex="^([0-9]+.*)-r([0-9]*)$" va1=() vr1=0 va2=() vr2=0 len i IFS="."
#
# # Split version strings into arrays, extract trailing revisions
# if [[ "$1" =~ ${regex} ]]; then
# va1=(${BASH_REMATCH[1]})
# [[ -n "${BASH_REMATCH[2]}" ]] && vr1=${BASH_REMATCH[2]}
# else
# va1=($1)
# fi
# if [[ "$2" =~ ${regex} ]]; then
# va2=(${BASH_REMATCH[1]})
# [[ -n "${BASH_REMATCH[2]}" ]] && vr2=${BASH_REMATCH[2]}
# else
# va2=($2)
# fi
#
# # Bring va1 and va2 to same length by filling empty fields with zeros
# (( ${#va1[@]} > ${#va2[@]} )) && len=${#va1[@]} || len=${#va2[@]}
# for ((i=0; i < len; ++i)); do
# [[ -z "${va1[i]}" ]] && va1[i]="0"
# [[ -z "${va2[i]}" ]] && va2[i]="0"
# done
#
# # Append revisions, increment length
# va1+=($vr1)
# va2+=($vr2)
# len=$((len+1))
#
# # *** DEBUG ***
# #echo "TEST: '${va1[@]} (?) ${va2[@]}'"
#
# # Compare version elements, check if v1 > v2 or v1 < v2
# for ((i=0; i < len; ++i)); do
# if (( 10#${va1[i]} > 10#${va2[i]} )); then
# return 1
# elif (( 10#${va1[i]} < 10#${va2[i]} )); then
# return 2
# fi
# done
#
# # All elements are equal, thus v1 == v2
# return 0
#}
function min() {
local flag=()
while (( $# )); do
[ -n "$1" ] || { shift; continue; }
#FIXME use getopts
[[ $1 = -[a-zA-Z] ]] && flag+=( "$1" ) || break
shift
done
local IFS=$'\n'
sort --numeric-sort "${flag[@]}" "$*" | head -n 1
}
function max() { min -r "$@"; }
# Array or Hash
#TODO bash 4.2.46 doesn't support 'nref', use 'eval ${$1}'
# bash 4.4 has "${!nref[@]@Q}" but that puts single-quotes around each element
#
if [[ "$BASH_VERSION" =~ 5|4.4 ]]; then
#----
function keys() { values "$@"; }
function values() {
local format keys
local -i OPTIND; local opt OPTARG
while getopts ':hf:lqu' opt; do
case "$opt" in
f) format=${OPTARG} ;;
l) format='@L' ;;
q) format='@Q' ;;
u) format='@U' ;;
:) log.error "missing argument (${!OPTIND})" ;;&
\?) log.error "unsupported option (-$OPTARG)" ;&
h|*) >&2 cat << EOF
Usage: $FUNCNAME ...
EOF
return 2
esac
done
shift $((OPTIND - 1))
#FIXME
# local -n object
# is_nref "${1:?array}" && eval object="\${!$1}" || object=$1
# is_array -v "${!object}" || return
local -n nref=${1:?varname}
if [ "${FUNCNAME[1]}" = keys ]; then
keys='!nref'; LOG_LEVEL=WARN is_hash "${!nref}"
set -- # remove args to force '*'
fi
case "$format" in
'') : ;;
\@L|,,) [ -n "${keys+X}" ] && format=to_lower ;;&
\@U|^^) [ -n "${keys+X}" ] && format=to_upper ;;&
# fake 'keys' to use pipe
?(to_)@(upper|lower)) : ${keys=''} ;;&
#NOTE adds quotes when empty which breaks [ -n "`value ...`" ]
\@[kQ]) local IFS= ;;&
# keys & format can not be combined
*) [ -z "$keys" ] || {
log.error "unsupported format ($format) ${keys+with keys}"
return 2; }
esac