-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashf.sh
executable file
·1260 lines (1210 loc) · 28.4 KB
/
bashf.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
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/bash
#
# Use this in your script:
# source ./bashf.sh || exit 1
#
# When executed, shows the function list and help for them.
# Features: logging, prompting, checking values, utils (argument parsing)
#
# Variables read:
# - BATCH_MODE (bool) - sets non-interactive mode
# - COLOR_MODE (bool) - see color_enable() / color_disable()
# - VERBOSE_MODE (int) - sets verbosity
# - OUTPUT_REDIRECT (optional) - set by log_redirect_to
# Variables set:
# - SCRIPT_* - information about current script
# - LINE_SEP - separator
# - HOSTNAME, OSTYPE, TMPDIR - defined if not already known
#
# You can either define usage() for your script or one will get defined by
# reading the header of your script.
#
# Strict mode is enabled by default.
# In this mode, script stops on error, undefined variable or pipeline fails.
#
# Additional fd's are open.
# 3 for logging messages, by default redirected on 1 (stdout).
# 5 for debug tracing, by default redirected to 2 (stderr).
[ -z "${BASHF:-}" ] || return 0 # already sourced
readonly BASHF="$(dirname "${BASH_SOURCE[0]}")"
# ---------------------------------------------------------
# Logging and output
_log() {
# usage: marker color text...
local IFS=$' ' mark=$1 color=$2
shift 2
printf '%s%-6s%s: %s\n' "$color" "$mark" "$COLOR_RESET" "$*" >&3
}
log_debug() {
# Show only when verbose.
(( VERBOSE_MODE )) || return 0
_log DEBUG "${COLOR_DIM}" "$@"
}
log_info() {
_log INFO "${COLOR_GREEN}" "$@"
}
log_warn() {
_log WARN "${COLOR_YELLOW}" "$@"
}
log_error() {
_log ERROR "${COLOR_RED}" "$@"
}
log_cmd() {
# Log command and run it
_log CMD "${COLOR_BLUE}" "$(quote "$@")"
"$@"
}
log_cmd_debug() {
# log_cmd only in verbose mode
if (( VERBOSE_MODE ))
then
log_cmd "$@"
else
"$@"
fi
}
log_var() {
# $1: variable name
# $2: value (optional, default: variable is read)
local _val _t=''
if [ $# -eq 1 ]
then
local _decl="$(quiet_err declare -p "$1" || true)"
[[ -z "$_decl" || "$_decl" == *=* ]] || _decl=uninitialized
case "$_decl" in
'')
_val="${COLOR_DIM}undefined${COLOR_RESET}";;
'uninitialized')
_val="${COLOR_DIM}uninitialized${COLOR_RESET}";;
'declare -a'*)
_t=a
_val="(array)";;
'declare -A'*)
_t=A
_val="(map)";;
*)
_val=${!1};;
esac
else
_val=$2
fi
_log VAR "${COLOR_CYAN}" "$(printf "%-20s: %s" "$1" "$_val")"
# print array contents
if [ -n "$_t" ]
then
eval "declare -$_t _arr=${_decl#*=}"
for i in "${!_arr[@]}"
do
log_var " [$i]" "${_arr[$i]}"
done
fi
}
log_section() {
# Show section separator with text.
local IFS=$' '
echo "${COLOR_BOLD}****** ${COLOR_UNDERLINE}$*${COLOR_RESET}" >&3
printf ' %(%F %T)T\n' >&3
}
log_script_info() {
# Log information about the running scripts.
# $@: arguments for main
is_main || return 0
(
local IFS=$' '
log_section "$SCRIPT_NAME"
log_var "Directory" "$(pwd)"
log_var "User" "$SCRIPT_USER"
log_var "Host" "$HOSTNAME [$OSTYPE]"
[ -z "$*" ] || log_var "Arguments" "$(quote "$@") "
) 3>&1 | indent_block >&3
}
log_redirect_output_to() {
# Call this function only once to redirect all input to a file.
# Sets OUTPUT_REDIRECT.
# $1: file to log to
if has_var OUTPUT_REDIRECT
then
log_warn "Already logging (pid: $OUTPUT_REDIRECT)"
return 1
fi
local fifo="$(mktemp -u --tmpdir ".$$pipe-XXX")"
mkfifo "$fifo" || die "Failed to open fifo for logging"
tee -ia "$1" < "$fifo" &
OUTPUT_REDIRECT=$!
exec &> "$fifo"
log_info "Logging to file [$1] started..."
rm -f "$fifo"
}
echo() {
# Safe echo without flags
local IFS=$' '
printf '%s\n' "$*"
}
readline() {
# Read entire line
local IFS=$'\n'
read -r "$@"
}
indent() {
# $1: prefix (default: tab)
sed -u "s:^:${1:-\t}:" || true
}
indent_block() {
echo "$HASH_SEP"
indent '# ' | rtrim
echo "$HASH_SEP"
}
indent_date() {
# Prefix lines with a timestamp.
# $1: date format (optional)
local format="${1:-%T}" line now
while readline line
do
printf -v now '%(%s)T'
printf "%($format)T: %s\n" "$now" "$line"
done
return 0
}
quote() {
# $@: arguments to quote
local out=''
while [ $# -gt 0 ]
do
[ -z "$out" ] || printf ' '
if [[ "$1" =~ " " ]]
then
out="'${1//\'/\'\\\'\'}'"
printf "%s" "$out"
else
out=1
printf "%q" "$1"
fi
shift
done
printf '\n'
}
trim() {
sed -u 's/^\s\+//; s/\s\+$//' || true
}
rtrim() {
sed -u 's/\s\+$//' || true
}
color_enable() {
# Enable COLOR_* variables
COLOR_MODE=1
COLOR_RESET=$'\e[0m'
COLOR_BLACK=$'\e[30m'
COLOR_RED=$'\e[31m'
COLOR_GREEN=$'\e[32m'
COLOR_YELLOW=$'\e[33m'
COLOR_BLUE=$'\e[34m'
COLOR_MAGENTA=$'\e[35m'
COLOR_CYAN=$'\e[36m'
COLOR_GRAY=$'\e[37m'
COLOR_DEFAULT=$'\e[39m'
COLOR_BOLD=$'\e[1m'
COLOR_DIM=$'\e[2m'
COLOR_UNDERLINE=$'\e[4m'
COLOR_REVERSE=$'\e[7m'
}
color_disable() {
# Clear COLOR_* variables
COLOR_MODE=0
COLOR_RESET=''
COLOR_BLACK=''
COLOR_RED=''
COLOR_GREEN=''
COLOR_YELLOW=''
COLOR_BLUE=''
COLOR_MAGENTA=''
COLOR_CYAN=''
COLOR_GRAY=''
COLOR_DEFAULT=''
COLOR_BOLD=''
COLOR_DIM=''
COLOR_UNDERLINE=''
COLOR_REVERSE=''
}
# ---------------------------------------------------------
# Checks
is_executable() {
# Check argument is executable.
quiet type "$@"
}
has_var() {
# Check whether the variable is defined and initialized.
local _v
_v="$(quiet_err declare -p "$1")" && [[ "${_v}" == *=* ]]
}
has_val() {
# Check whether the variable is not empty.
has_var "$1" && [ -n "${!1:-}" ]
}
has_flag() {
# Check whether the variable is true.
has_var "$1" && is_true "${!1:-}"
}
is_true() {
# Test argument value for a boolean.
case "${1,,}" in
y|yes|t|true|1|on) return 0;;
n|no|f|false|0|off) return 1;;
esac
! is_integer "$1" || return 0
return 2
}
is_integer() {
# Test whether argument is a positive integer.
[[ "$1" =~ ^[0-9]+$ ]]
}
is_number() {
# Test whether argument is a number.
[[ "$1" =~ ^-?[0-9]+(\.[0-9]*)?$ ]]
}
has_env() {
# Check whether the variable is in env
env | quiet grep "^$1="
}
index_of() {
# Print the index to stdout.
# $1: argument
# $2..: list to check against
local opt=$1 index=0 i
shift
for i in "$@"
do
[[ "$opt" == "$i" ]] && echo $index && return 0 || true
(( ++index ))
done
return 1
}
first_match() {
# Print first argument that succeeds a test.
# $1: test operation (or argument to test command)
# $2..: list of arguments to test
local check=("$1") val=
[[ $check != -* ]] || check=(test "$check")
shift
for val in "$@"
do
"${check[@]}" "$val" && printf '%s\n' "$val" && return 0 || true
done
return 1
}
strict() {
# Strict mode.
# errexit, errtrace (-eE)
# nounset (-u)
# pipefail, functrace
set -eEu -o pipefail -o functrace
}
non_strict() {
# Disable strict.
set +eEu +o pipefail +o functrace
}
debug_trace() {
# Enable tracing on fd 5 (redirected to 2)
log_debug "Enable tracing."
PS4='+|${FUNCNAME[0]:0:15}|${LINENO}| '
(printf '' >&5) &>/dev/null || exec 5>&2
BASH_XTRACEFD=5
set -x
}
debug_repl() {
# Simple loop with eval, use as breakpoint in scripts.
local REPLY
while true
do
read -e -p "debug-${PS3:-> }" || break
eval $REPLY || log_warn "repl: error ($?)"
done
}
stacktrace() {
# Print stacktrace to stdout.
# $1: mode (full or short) (default: full)
# $2: number of frames to skip (default: 1)
local mode="${1:-full}" skip="${2:-1}"
local i=
for (( i=skip; i<${#FUNCNAME[@]}; i++))
do
local name="${FUNCNAME[$i]:-??}" line="${BASH_LINENO[$i-1]}"
case "$mode" in
short)
printf ' > %s:%s' "$name" "$line"
;;
*)
printf ' at: %s (%s:%d)\n' "$name" "${BASH_SOURCE[$i]:-(unknown)}" "$line"
;;
esac
done
}
_on_exit_callback() {
# $@: $PIPESTATUS
# called by default in bashf on exit
# log FATAL error if command failed in strict mode
local ret=$? cmd="$BASH_COMMAND" pipestatus=("$@")
[[ "$cmd" != exit* ]] || cmd=""
if [[ $- == *e* && "$ret" != 0 ]]
then
[[ "$cmd" != return* ]] || cmd=""
local msg="${cmd:-Command} failed"
if [ ${#pipestatus[@]} -gt 1 ]
then
msg+=" (pipe: ${pipestatus[*]})"
else
msg+=" ($ret)"
fi
msg+="$(stacktrace short 2)"
_log FATAL "${COLOR_BOLD}${COLOR_RED}" "$msg"
fi
}
trap_add() {
# Add command to trap EXIT.
# $*: command to run
local handle="$(trap -p EXIT \
| sed "s:^trap -- '::" \
| sed "s:' EXIT\$::")"
local IFS=$' '
trap "$*; $handle" EXIT
}
trap_default() {
# Set default trap on ERR in bashf.
trap '_on_exit_callback "${PIPESTATUS[@]}"' ERR
}
die() {
# Log error and exit with failure.
log_error "$@"
log_debug "$(stacktrace short 2)" >&2
exit 1
}
die_usage() {
# Log error, usage and exit with failure.
log_error "$@"
usage >&2
exit 1
}
die_return() {
# Log error and exit with given code.
# $1: exit code
# $2..: message
local e="$1"
shift
log_error "$@"
log_debug "$(stacktrace short 2)" >&2
exit "$e"
}
# ---------------------------------------------------------
# Input
prompt() {
# Prompt user.
# usage: variable_name [ options ]
# --text|-t: prompt
# --def|-d: default value
# --non-empty|-n: force having a reply
# --silent|-s: for password prompting
# anything else is passed to `read`
local _name=$1 _text='' _def='' _req=0 _silent=0
shift
[ -n "$_name" ] || die "prompt(): No variable name set"
eval $(arg_eval \
text t _text=:val \
def d _def=:val \
non-empty n _req=1 \
silent s _silent=1 \
--invalid-break \
)
if is_true "$BATCH_MODE"
then
[ -n "$_def" ] || die "prompt(): Default value not set for $_name"
log_debug "Use default value: $_name=$_def"
eval "$_name=\$_def"
return
fi
# Read from input
[ -n "$_text" ] || _text="Enter $_name"
[ -z "$_def" ] || _text+=" ${COLOR_DIM}[$_def]${COLOR_RESET}"
[ $# -eq 0 ] || log_debug "read arguments: $*"
! is_true "$_silent" || set -- -s "$@"
while true
do
local _end=N
! has_var OUTPUT_REDIRECT || sleep 0.1
if read "$@" -r -p "${_text}: " "${_name?}"
then
! (has_var OUTPUT_REDIRECT || quiet index_of -s "$@" ) \
|| echo
else
case $? in
1|142) # EOF | timeout
echo;;
*)
die "prompt(): Failed to read input! ($?)";;
esac
_end=Y
fi
[ -n "${!_name}" ] || eval "$_name=\$_def"
[ "$_req" -gt 0 ] && [ -z "${!_name}" ] || break
! has_flag _end || die "prompt(): Reached end of input"
done
}
confirm() {
# Ask for a boolean reply.
# usage: [ text_prompt ] [ options ]
# --def|-d: default value (Y/N)
# Rest passed to `prompt`.
local confirmation='' _text='' _def=''
if [ $# -gt 0 ] && [ "${1:0:1}" != '-' ]
then
_text=$1
shift
else
_text='Confirm'
fi
_text+=' (y/n)'
eval $(arg_eval \
def d _def=:val \
--invalid-break \
)
if [ -n "$_def" ]
then
set -- --def "${_def^^}" "$@"
fi
while true
do
prompt confirmation --text "$_text" "$@"
is_true "$confirmation" && confirmation=0 || confirmation=$?
case "$confirmation" in
0) return 0;;
1) return 1;;
esac
done
}
prompt_choice() {
# Prompt with multiple choices.
# usage: variable_name [ options ] -- menu choices
# --text|-t: prompt
# --def|-d: default reply
# menu choices are in format 'value|text'
local _name=$1 _text='' _def=''
shift
[ -n "$_name" ] || die "prompt(): No variable name set"
eval $(arg_eval --partial \
text t _text=:val \
def d _def=:val \
)
[ "$1" == '--' ] && shift
[ -n "$_text" ] || _text="Select $_name"
if is_true "$BATCH_MODE"
then
prompt "$_name" --text "$_text" --def "$_def"
return
fi
local _mvalue=() _mtext=() _item _i
for _item
do
_mvalue+=("${_item%%|*}")
_mtext+=("$(echo "${_item#*|}" | xargs)")
done
# Select
[ -z "$_def" ] || _def=$(( $(index_of "$_def" "${_mvalue[@]}") + 1 ))
! has_var OUTPUT_REDIRECT || sleep 0.1
printf '%s\n' "$_text"
_i=0
for _item in "${_mtext[@]}"
do
(( ++_i ))
printf " %2d) %s\n" "$_i" "$_item"
done
while true
do
prompt _item --text 'Choice' --def "$_def"
if is_integer "$_item" && (( 0 < _item && _item <= ${#_mvalue[@]} ))
then
_item=${_mvalue[$_item-1]}
break
elif quiet index_of "$_item" "${_mvalue[@]}"
then
break
else
log_warn 'Invalid choice'
fi
done
eval "$_name=\$_item"
}
menu_loop() {
# Prompt in a loop.
# usage: [ prompt_text ] -- menu entries
# menu entries are 'function|text'
local _text=Menu _item IFS=' '
eval $(arg_eval_rest ? _text --partial)
if is_true "$BATCH_MODE"
then
log_info "${_text}"
for _item
do
_item="${_item%%|*}"
# evaluate item (not quoted)
log_cmd $_item
done
return
fi
# Menu
local REPLY
while true
do
prompt_choice REPLY --text "${COLOR_BOLD}$_text${COLOR_RESET}" \
-- "$@" "break|Exit" || break
[ "$REPLY" != break ] || break
# evaluate item (not quoted)
log_cmd_debug $REPLY
done
}
wait_user_input() {
# Wait for user confirmation.
confirm "Proceed..." --def Y
}
# ---------------------------------------------------------
# Various
# - argument parsing
# - execution (mask output, cd, is_main, retry)
# - http
# - pager
# - job control
# - file locking
arg_eval() {
# Generate parser for arguments (starting with a -).
# Must be used inside a function.
# usage:
# eval $(arg_eval var=:val text t --desc='Set text to 1' =1)
# eval $(arg_eval help h --desc='Help' '{ help }')
# --partial: can leave unparsed arguments
# --opt-var=name: adds standalone options to an array variable
# --opt-break: break on first option (imply partial)
# --invalid-break: break on first invalid argument (imply partial)
# --var=name: local temp variable (default: $_arg)
# --desc=text: description for next command
# name: alias for next command (single letter is short option)
# x=v or { code }: command to execute
# if x is empty, last alias is used as variable name
# if v contains :val, the next argument is used as the value
local _alias='' _name='' _i _arg_partial=F _arg_opts='' _arg_invalid_break=F _var=_arg
for _i in "$@"
do
case "$_i" in
--partial) _arg_partial=T;;
--opt-break) _arg_opts='break'; _arg_partial=T;;
--opt-var=*) _arg_opts=${_i#*=};;
--invalid-break) _arg_invalid_break=T; _arg_partial=T;;
--var=*) _var=${_i#*=};;
--desc=*) ;;
-*) die "arg_eval: invalid option [$_i]";;
esac
done
# start parser
echo "local $_var;"
echo 'while [ $# -gt 0 ];'
echo 'do'
echo " $_var=\$1;"
# check for equal sign
echo " if [[ \"\$$_var\" == -*=* ]]; then"
echo " shift;"
echo " set -- \"\${$_var%=*}\" \"\${$_var#*=}\" \"\$@\";"
echo " $_var=\$1;"
echo ' fi;'
# multi short options
echo " if [[ \"\$$_var\" =~ ^-[a-zA-Z]{2,} ]]; then"
echo " shift;"
echo " set -- \"\${$_var:0:2}\" \"-\${$_var:2}\" \"\$@\";"
echo " $_var=\$1;"
echo ' fi;'
# check options
echo ' case "$1" in'
echo ' --) break;;'
for _i in "$@"
do
case "$_i" in
-*) continue;;
*=*|{*})
[ -n "$_name" ] || \
die "arg_eval: missing option name for [$_i]"
# check if '=...'
[[ "$_i" != '='* ]] || \
_i="${_name}${_i}"
# with and without value
if [[ "$_i" == *:val* ]]
then
_i=${_i/:val/\$2}
_i='[ $# -gt 1 ] || die "Missing argument for ['"$_alias"']"; '"$_i"
_i+='; shift 2'
else
_i+='; shift'
fi
echo " $_alias) $_i;;"
_alias=''
_name=''
;;
*)
[ -z "$_alias" ] || _alias+='|'
[ "${#_i}" -eq 1 ] && _alias+='-' || _alias+='--'
_alias+=$_i
_name=$_i
;;
esac
done
[ -z "$_alias" ] || die "arg_eval: invalid spec, assign a value"
# invalid arguments
if is_true "$_arg_invalid_break"
then
echo ' -?*) break;;'
else
echo ' -?*) die "Invalid argument [$1]";;'
fi
# other options
case "$_arg_opts" in
break) echo ' *) break;;';;
'') echo ' *) die "Unexpected argument [$1]";;';;
*) echo ' *) '"$_arg_opts"'+=("$1"); shift;;';;
esac
echo ' esac;'
echo 'done;'
is_true "$_arg_partial" || \
echo '[ $# -eq 0 ] || die "Too many arguments, next [$1]";'
}
arg_eval_rest() {
# Generate parser for named non-arguments (rest of parameters)
# Usage: eval $(arg_eval_rest arg1 ? arg2 [ options ])
# --partial: can leave unparsed arguments (after '--')
# --opt-var=name: array to which to append other arguments
# --opt-min=N: minimum number of arguments in list
# --partial-var=name: array to which to assign all arguments
# after '--', implies --partial
# --var=name: local temp variable (default: $_arg)
# name: name of the argument
# ?: after this point arguments are optional
local _i _arg_partial=F _arg_optional=F _var=_arg
local _arg_var_list='' _arg_list_min=0 _arg_var_partial=''
# output parser
while [ $# -gt 0 ]
do
_i=$1
shift
case "$_i" in
--) break;;
--partial) _arg_partial=T;;
--partial-var=*) _arg_var_partial=${_i#*=}; _arg_partial=T;;
--opt-var=*) _arg_var_list=${_i#*=};;
--opt-min=*) _arg_list_min=${_i#*=};;
--var=*) _var=${_i#*=};;
-*) die "arg_eval_rest: invalid option [$_i]";;
\?) _arg_optional=T;;
*)
# named arguments
echo 'if [ $# -gt 0 ] && [ "$1" != -- ]; then'
echo " $_i=\$1; shift;"
if ! is_true "$_arg_optional"
then
echo 'else'
echo ' die "Expecting '"$_i"' argument";'
fi
echo 'fi;'
;;
esac
done
# list arguments
if [ -n "$_arg_var_list" ]
then
echo "local $_var=\$(index_of '--' \"\$@\" || true);"
echo "if [ -n \"\$$_var\" ]; then"
echo " $_arg_var_list=(\"\${@:1:\$$_var}\");"
echo " shift \${$_var};"
echo 'else'
echo " $_arg_var_list=(\"\$@\");"
echo ' set --;'
echo 'fi;'
fi
[ "$_arg_list_min" -eq 0 ] || \
echo "[ \${#${_arg_var_list}[@]} -ge $_arg_list_min ] || " \
"die 'Not enough arguments ($_arg_list_min needed)';"
# partial arguments
if is_true "$_arg_partial"
then
printf '%s' '[ $# -eq 0 ] || { [ "$1" == "--" ] && shift; }'
echo ' || die "Unexpected argument [$1]";'
if [ -n "$_arg_var_partial" ]
then
echo "$_arg_var_partial=(\"\$@\");"
echo 'set --;'
fi
else
echo '[ $# -eq 0 ] || die "Too many arguments, next [$1]";'
fi
}
declare -a ARG_PARSE_OPTS=() ARG_PARSE_REST=()
arg_parse_reset() {
# Reset the parser by defining ARG_PARSE_OPTS and ARG_PARSE_REST.
# Arguments can be:
# color: color and no-color options
# help: Show the usage and exit (-h)
# quiet: Suppress output
# trace: See debug_trace function
# verbose: Show verbose message
# v: verbose (short option)
# default: help verbose color
ARG_PARSE_OPTS=()
ARG_PARSE_REST=()
local i
if [ "${1:-}" == "default" ]
then
shift
set -- help verbose color "$@"
fi
for i in "$@"
do
case "$i" in
help) ARG_PARSE_OPTS+=(help h --desc='Show help' '{ usage; exit; }');;
v) ARG_PARSE_OPTS+=(v '{ (( ++VERBOSE_MODE )) }');;
verbose) ARG_PARSE_OPTS+=(verbose
--desc='Show debug messages' '{ (( ++VERBOSE_MODE )); }'
);;
color) ARG_PARSE_OPTS+=(
color '{ color_enable; }'
no-color '{ color_disable; }'
);;
trace) ARG_PARSE_OPTS+=(trace '{ debug_trace; }');;
quiet) ARG_PARSE_OPTS+=(quiet
'{ exec 2>/dev/null 3>/dev/null; VERBOSE_MODE=0; }'
);;
*) log_warn "Unknown parser preset [$i]";;
esac
done
}
arg_parse() {
# Run the parser, give "$@" as arguments.
# Combines `arg_eval` with arguments ARG_PARSE_OPTS
# and `arg_eval_rest` with arguments ARG_PARSE_REST.
local _arg_parse_opts=()
eval $(arg_eval --partial --opt-var=_arg_parse_opts "${ARG_PARSE_OPTS[@]}")
[ "${#_arg_parse_opts[@]}" -eq 0 ] || set -- "${_arg_parse_opts[@]}" "$@"
eval $(arg_eval_rest "${ARG_PARSE_REST[@]}")
}
usage_arg_parse() {
# Print usage message with options list based on ARG_PARSE_* variables.
# -U: print default usage line
# -u opts: print usage line
# -t text: print text
# -: print from stdin
local IFS=' ' usage=0
local i alias desc
while [ $# -gt 0 ]
do
case "$1" in
-U)
printf "Usage: ${COLOR_BOLD}%s${COLOR_RESET}" "$SCRIPT_NAME"
[ "${#ARG_PARSE_OPTS[@]}" -eq 0 ] || printf ' [ options ]'
local optional=F partial=''
alias=''
for i in "${ARG_PARSE_REST[@]}"
do
case "$i" in
--) break;;
--partial) partial=${partial:-rest};;
--partial-var=*) partial=${i#*=};;
--opt-var=*) alias=${i#*=};;
-*) ;;
\?) optional=T;;
*)
if is_true "$optional"
then
printf ' [%s]' "$i"
else
printf ' %s' "$i"
fi
;;
esac
done
[ -z "$alias" ] || printf ' %s...' "$alias"
[ -z "$partial" ] || printf ' [ -- %s ]' "$partial"
echo
(( usage+=1 ))
shift;;
-u)
[ "$usage" -gt 0 ] \
&& printf ' ' \
|| printf 'Usage:'
printf " ${COLOR_BOLD}%s${COLOR_RESET} %s\n" "$SCRIPT_NAME" "$2"
(( usage+=1 ))
shift 2;;
-t)
printf '%s\n' "$2"
shift 2;;
-)
cat -
echo
shift;;
*)
die "usage_arg_parse(): unknown option [$1]";;
esac
done
# Print option list
alias=''
desc=''
for i in "${ARG_PARSE_OPTS[@]}"
do
case "$i" in
--desc=*) desc=${i##*=};;
-*) ;;
*=*|{*})
[ -z "$desc" ] || \
printf " %-18s %s\n" "$alias" "$desc"
desc=''
alias=''
;;
*)
[ -z "$alias" ] || alias+='|'
[ "${#i}" -eq 1 ] && alias+='-' || alias+='--'
alias+=$i
;;
esac
done
}
_read_functions_from_file() {
# Print "$function_name:$line" for each function in file.
# $1: file to read
local file="$1"
[ -r "$file" ] || die "Cannot read file [$file]"
(grep -n '^\(function \)\?[a-z]\w*\s\?()' "$file" || true) |
sed 's/^\([0-9]\+\):\(function\s*\)\?\([a-z]\w*\)\s\?().*$/\3:\1/' |
sort
}
read_function_help() {
# Print first comment block in a function.
# $1: function name
# stdin: function/file code
local func=$1
local i
while readline i
do
if [[ "$i" =~ ${func}\s?\(\) && "$i" != *'#'* ]]
then
trim | sed -n '/^{/n; /^[^#]/q; s/#\s\?//p'
break
fi
done
}
quiet() {
# Suppress output
"$@" &>/dev/null
}
quiet_err() {
# Suppress stderr
"$@" 2>/dev/null
}
pager() {
# Use a pager for displaying text.
# If parameters are given, the output is executed, piped and return code
# is returned.
local pager=() r
if has_val PAGER
then
pager=("$PAGER")
elif is_executable less
then
pager=(less --RAW-CONTROL-CHARS --no-init --quit-if-one-screen)
fi
if [ $# -eq 0 ]
then
# run pager
"${pager[@]}"
elif [ -n "$pager" ]
then
# pipe and return exit code
"$@" | "${pager[@]}" && r=0 || r=("${PIPESTATUS[@]}")
return $r
else
# no pager, just run
"$@"
fi
}
is_main() {
# Check if current file is being called.
[[ "$0" == "${BASH_SOURCE[-1]:-}" ]]
}
is_tty() {
# Are we running in a tty?
[[ -t 1 ]]
}
HTTP_TOOL=""
_check_http_tool() {
[ -z "$HTTP_TOOL" ] || return 0 # already configured
HTTP_TOOL=$(first_match is_executable curl wget)
log_debug "HTTP tool is $HTTP_TOOL"
}
http_get() {
# Download using wget or curl
# $1: URL
# $2: output file (default to stdout)
local url=$1
local output="${2:-}"
[ -n "$url" ] || die "No URL provided"
_check_http_tool
case "$HTTP_TOOL" in
curl)
local opts=(--show-error --fail --location)
[ -z "$output" ] || opts+=(-o "$output")
if (( VERBOSE_MODE > 1 ))
then
opts+=(--verbose)
else
(( VERBOSE_MODE )) || opts+=(--silent)
fi
log_cmd_debug curl "${opts[@]}" "$url";;
wget)
local opts=(--no-cache)