forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-define-aliases
1206 lines (998 loc) · 38.5 KB
/
adpn-define-aliases
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
#
# adpn-define-aliases: some basic subroutines used across many of the adpn suite of scripts
#
# @version 2021.0807-1244
[[ -z "${SCRIPTDIR}" ]] && SCRIPTDIR="$( readlink --canonicalize "${BASH_SOURCE}" | xargs dirname )"
PATH="${SCRIPTDIR}:${PATH}"
CONFFILE="${SCRIPTDIR}/${SCRIPT}.defaults.conf"
JSONCONFFILE="${SCRIPTDIR}/adpnet.json"
SCRIPT="$(basename "${SCRIPTPATH}")"
ME="${SCRIPTPATH}"
ADPN="$( readlink --canonicalize "${SCRIPTDIR}/adpn" )"
ADPN_STASH="$( readlink --canonicalize "${SCRIPTDIR}/adpn-stash-do" )"
__ALIASFILE__="$( basename "${BASH_SOURCE}" )"
[[ -r "${HOME}/.adpn" ]] && _ADPN_CONF_DIR="${HOME}/.adpn" || _ADPN_CONF_DIR=""
[[ -r "${_ADPN_CONF_DIR}" ]] && [[ -r "${_ADPN_CONF_DIR}/adpnet.json" ]] && _ADPN_CONF_FILE="${_ADPN_CONF_DIR}/adpnet.json" || _ADPN_CONF_FILE=""
# adpn_parameter_from: retrieve and echo a property that may be found in one of several possible sources for parameter data, including
# command-line switches, JSON configuration files, live JSON data, user console input, and fall-back defaults.
# example: FOOBAR=$( adpn_parameter_from "--foo" "--bar" ".baz" "<FOO [bar]: " )
#
# --SWITCH: accept a parameter from the command-line switch named SWITCH
# .PROPERTY: accept a parameter from the key-value pair in the data provided by the standard configuration JSON file (typically adpnet.json)
# .PROPERTY<(adpn stash): accept a parameter from the key-value pair in the data provided by the temporary encrypted stash
# .PROPERTY<{JSON DATA}: accept a parameter from the key-value pair in the JSON DATA provided
# <PROMPT: accept a parameter interactively from user input, using the text in PROMPT as a prompt
# TEXT: use the literal text provided as a default value if previous sources have failed
# \TEXT: use the literal text provided as a default value if previous sources have failed; the backslash allows for text beginning with special characters like "--", ".", "<" or "\"
#
# @version 2021.0730
function adpn_parameter_from() {
RESULT="" ; EXITCODE=2
adpn_debug 500,adpn_parameter_from "${__ALIASFILE__}:${LINENO}" "%s(%s)" "adpn_parameter_from" "$( join_by ', ' "${@:1}" )"
while [[ "$#" -gt 0 && "${EXITCODE}" -gt 0 ]] ; do
local source="${1}" ; shift
adpn_debug 600,adpn_parameter_from "${__ALIASFILE__}:${LINENO}" "source='%s'" "$source"
if [[ "${source}" =~ ^(--|!!)(.*)$ ]] ; then
KEY="${BASH_REMATCH[2]}"
# in a civilized version of bash we could use test -v
# but we're not always in civilized places...
# technique from <https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash/13864829#13864829>
if [[ -n "${_PARAM[$KEY]+SET}" ]] ;then
RESULT="${_PARAM[$KEY]}" ; EXITCODE=0
fi
elif [[ "${source}" =~ ^[.]([^\<]+)([\<](.*))?$ ]] ; then
KEY="${BASH_REMATCH[1]}"
JSON="${BASH_REMATCH[3]}"
if [[ "${JSON}" == '(adpn stash)' ]] ; then
RESULT="$( "${ADPN_STASH}" get --output=application/json | adpn-json.py --output=text/plain --key="${KEY}" )" ; EXITCODE="$?"
elif [[ -n "${JSON}" ]] ; then
RESULT="$( printf "%s" "${JSON}" | adpn-json.py --output=text/plain --key="${KEY}" )" ; EXITCODE="$?"
else
RESULT="$( adpnprop "${KEY}" )" ; EXITCODE="$?"
fi
elif [[ "${source}" =~ ^[\<](.*)$ ]] ; then
read -p "${BASH_REMATCH[1]}" RESULT ; EXITCODE="$?"
[[ -z "${RESULT}" ]] && EXITCODE=1
elif [[ "${source}" =~ ^\\(.*)$ ]] ; then
RESULT="${BASH_REMATCH[1]}" ; EXITCODE=0
else
RESULT="${source}" ; EXITCODE=0
fi
done
printf "%s" "${RESULT}"
return "${EXITCODE}"
}
function adpn_get_user_email () {
adpn_get_email_from ".user/email" ".user/realname"
}
function adpn_get_email_from () {
local email_from="${1}" ; shift
local realname_from="${1}" ; shift
local email="$( adpn_parameter_from "${email_from}" )"
local realname="$( adpn_parameter_from "${realname_from}" )"
[[ -n "${realname}" ]] && email=$( printf "%s <%s>" "${realname}" "${email}" )
printf "%s" "${email}"
}
# adpnprop: retrieve and echo a property from the adpnet.json config file
# example: FOOBAR=$( adpnprop "${KEY}" )
#
# @param string key name to retrieve from JSON config file
# @return int exit code from adpn-json.py (0=success, 1=key not found)
#
# @version 2021.0406
adpnprop() {
local key="${1}"
local switch=""
local value=""
declare -a adpnprop_switches=()
declare -A adpnprop_params=()
while [[ "$#" -gt 0 && "${1}" =~ ^--([A-Za-z_0-9][^=]*)(=(.*))?$ ]] ; do
switch="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[3]}"
if [[ -z "${BASH_REMATCH[2]}" ]] ; then
value="${switch}"
fi
adpnprop_switches+=( "${1}" )
adpnprop_params["${switch}"]="${value}"
shift
key="${1}"
done
JSON="{}"
if [[ -r "${JSONCONFFILE}" ]] ; then
JSON=$( cat "${JSONCONFFILE}" )
fi
if [[ -z "${key}" ]] ; then
REPLY=$( echo "${JSON}" | adpn-json.py --output=text/plain "${adpnprop_switches[@]}")
JSONEXIT=$?
else
REPLY=$( echo "${JSON}" | adpn-json.py --key="${key}" "${adpnprop_switches[@]}")
JSONEXIT=$?
fi
printf "%s" "${REPLY}"
return "${JSONEXIT}"
}
# pluginprop: retrieve and echo a property from the text output of adpn-ingest-test, adpn-plugin-info
# and related tools that plug AU data into LOCKSS plugin parameters.
#
# @param string txt tab-separated value text containing Key <tab> Text values
# @param string key name of field to retrieve from plugin data
# @return int exit code from lockss-plugin-props-print-parameter.py
function pluginprop() {
local txt="${1}" ; shift
local key="${1}" ; shift
local regex=""
if [[ -z "${key}" ]] ; then
regex=".*"
else
regex="^${key}\t"
fi
cat "${txt}" | grep -P "${regex}" | lockss-plugin-props-print-parameter.py "$@"
return $?
}
function adpn_getpassword_from() {
RESULT="" ; EXITCODE=2
KDBX_SCHEME="keepass"
while [[ "$#" -gt 0 && "${EXITCODE}" -gt 0 ]] ; do
local source="${1}" ; shift
if [[ "${source}" =~ ^${KDBX_SCHEME}:/*(/([^/]+)+) ]] ; then
local FILENAME="$( printf "%s" "${source}" | sed -E 's/^[^:]+[:]\/+/\//' | sed -E 's/[?].*$//' )"
FILENAME="$( basename "${FILENAME}" )"
MD5=$( printf "%s" "${FILENAME}" | md5sum | sed -E 's/\s+.*$//g' )
PASSKEY="$( printf "AGK-Pass-%s" "${MD5}" )"
PASSPHRASE="$( "${ADPN_STASH}" get "${PASSKEY}" )"
local -a AGK_SW=( "adpn-get-keepass.py" "--single" )
AGK_SW+=("${source}")
if [[ -n "${PASSPHRASE}" ]] ; then
adpn_debug 500,adpn,stash "${__ALIASFILE__}:${LINENO}" "adpn_get_password_from: Using stashed passphrase: %s" "$( obscure_password "${PASSPHRASE}" )"
RESULT="$( printf "%s" "${PASSPHRASE}" | "${AGK_SW[@]}" )"
else
adpn_debug 500,adpn,stash "${__ALIASFILE__}:${LINENO}" "adpn_get_password_from: Using interactive request"
AGK_SW+=( "--interactive" )
AGK_SW+=( "--stash=${ADPN_STASH} post ${PASSKEY}" )
[[ -n "${DDBG}" ]] && AGK_SW+=( "${DDBG}" )
RESULT="$( "${AGK_SW[@]}" )"
fi
EXITCODE="$?"
elif [[ "${source}" =~ ^[\<](.*)$ ]] ; then
IFS='' read -s -p "${BASH_REMATCH[1]}" RESULT 1>&2 ; EXITCODE="$?"
[[ -z "${RESULT}" ]] && EXITCODE=1
elif [[ "${source}" =~ ^\\(.*)$ ]] ; then
RESULT="${BASH_REMATCH[1]}" ; EXITCODE=0
else
RESULT="${source}" ; EXITCODE=0
fi
done
printf "%s" "${RESULT}"
return "${EXITCODE}"
}
declare -a _ARGV=()
declare -A _PARAM=()
declare -A _SWITCHES=()
declare -a _CMD_REST=()
declare -g _CMD_REST_LAST_SWITCH=0
export ADPN_SWITCH_REGEX='^--([A-Za-z_0-9][^=]*)(=(.*))?$'
function adpn_command_line() {
push_input_loop $'\n'
local -a SRC_FILES=( $( readlink -e "$@" 2>/dev/null ) )
pop_input_loop
local switches_on=1
_ARGV=()
_PARAM=()
_SWITCHES=()
[[ "${#_ARGV}" -eq 0 ]] && _ARGV+=( "$0" )
local -A can_overwrite=()
local -A written_here=()
for SRC in "${SRC_FILES[@]}" ; do
adpn_debug 500,adpn_command_line "${__ALIASFILE__}:${LINENO}" "switch file: %s" "${SRC}"
written_here=()
if [[ -r "${SRC}" ]] ; then
push_input_loop
while IFS="" read -r SWITCH ; do # < "${SRC}"
if [[ "${switches_on}" -gt 0 && "${SWITCH}" =~ ^--$ ]] ; then
switches_on=0
elif [[ "${switches_on}" -gt 0 && "${SWITCH}" =~ ${ADPN_SWITCH_REGEX} ]] ; then
local KEY="${BASH_REMATCH[1]}"
local VALUE="${BASH_REMATCH[3]}"
if [[ -z "${BASH_REMATCH[2]}" ]] ; then
VALUE="$KEY"
fi
if [[ -z "${can_overwrite[${KEY}]}" ]] ; then
adpn_debug 500,adpn_command_line "${__ALIASFILE__}:${LINENO}" "%s: %s" "${KEY}" "$( declare -p _PARAM )"
IDX=1 ; ORIG_KEY="${KEY}"
while [[ -n "${_SWITCHES[${KEY}]}" ]] ; do
IDX=$(( IDX + 1 ))
KEY="${ORIG_KEY}#${IDX}"
adpn_debug 500,adpn_command_line "${__ALIASFILE__}:${LINENO}" "avoid collision on %s: %s into %s" "${ORIG_KEY}" "${KEY}" "$( declare -p _PARAM )"
done
else
can_overwrite["${KEY}"]=""
fi
_PARAM[$KEY]="${VALUE}"
_SWITCHES[$KEY]="${SWITCH}"
written_here["${ORIG_KEY}"]=1
elif [[ ! -z "${SWITCH}" ]] ; then
_ARGV+=("${SWITCH}")
fi
done < "${SRC}"
pop_input_loop
fi
can_overwrite=()
for key in "${!written_here[@]}" ; do
can_overwrite["${key}"]="${written_here[${key}]}"
done
done
_CMD_REST=()
[[ "${#_SWITCHES[@]}" -gt 0 ]] && _CMD_REST+=( "${_SWITCHES[@]}" ) ; _CMD_REST_SPLIT="${#_CMD_REST[@]}"
[[ "${#_ARGV[@]}" -gt 2 ]] && _CMD_REST+=( "--" "${_ARGV[@]:2}" ) && _CMD_REST_SPLIT=$((_CMD_REST_SPLIT+1))
}
function adpn_command_line_switches_to_tsv() {
local switches_on=1
FIFO="$( mktemp -d )"
touch "${FIFO}/switches"
touch "${FIFO}/argv"
for ARG in "$@" ; do
if [[ "${switches_on}" -gt 0 && "${ARG}" =~ ^--$ ]] ; then
switches_on=0
elif [[ "${switches_on}" -gt 0 && "${ARG}" =~ ${ADPN_SWITCH_REGEX} ]] ; then
local KEY="${BASH_REMATCH[1]}"
local VALUE="${BASH_REMATCH[3]}"
[[ -z "${BASH_REMATCH[2]}" ]] && VALUE="$KEY"
printf -- "%q\t%q\n" "${KEY}" "${VALUE}" >> "${FIFO}/switches"
else
printf -- "--\t%q\n" "${ARG}" >> "${FIFO}/argv"
fi
done
cat "${FIFO}/switches" "${FIFO}/argv"
rm -rf "${FIFO}"
}
function adpn_switch_from_tsv() {
local RESULT_STR
local GREP_STR
local result=0
for PREFIX in "$@" ; do
GREP_STR="$( cat - | grep -E "^${PREFIX}\s" )" ; result="$?"
push_input_loop
while IFS="" read -r LINE ; do
[[ -n "${LINE}" ]] && RESULT_STR="$( printf "%s\n" "${LINE}" | cut --field=2- )" || RESULT_STR=""
[[ -n "${RESULT_STR}" ]] && eval "RESULT_STR=${RESULT_STR}" && printf "%s" "${RESULT_STR}" || result="$?"
done < <(printf "%s\n" "${GREP_STR}")
pop_input_loop
done
return "${result}"
}
declare -x DDBG DDBGLEVEL
declare -ax DBGTAGS
function adpn_set_display_settings() {
VV="${_SWITCHES[verbose]}"
QQ="${_SWITCHES[quiet]}"
V="${_SWITCHES[verbose]}"
Q=""
[[ -n "${DDBG}" ]] && DBG="${DDBG}" || DBG="${_SWITCHES[debug]}"
DBGLEVEL="${_PARAM[debug]}"
if [[ "${DBGLEVEL}" =~ ^[0-9]+([,]|$) ]] ; then
DBGLEVEL="${DBGLEVEL}"
elif [[ -n "${DBGLEVEL}" ]] ; then
DBGLEVEL="1,${DBGLEVEL}"
fi
DBGTAGS=( $( printf "%s" "${DBGLEVEL}" | tr "," "\n" | tail -n +2 ) )
DBGLEVEL=$( printf "%s" "${DBGLEVEL}" | cut --delimiter="," --field=1 )
if [[ "${DBGLEVEL}" -gt 1 ]] ; then
local -a d_levels_down=( $(( DBGLEVEL - 1 )) "${DBGTAGS[@]}" )
DDBGLEVEL=$( join_by "," "${d_levels_down[@]}" )
DDBG="--debug=${DDBGLEVEL}"
else
DDBGLEVEL=""
DDBG=""
fi
if [[ -z "${_PARAM[verbose]}" ]] ; then
Q="--quiet"
fi
if [[ -n "${QQ}" ]] ; then
Q="--quiet"
fi
local script_cmd=$( printf "%s" "${SCRIPT} ${CMD}" | sed -E 's/\s+$//' )
SCRIPT_CMD_NAME=$( adpn_parameter_from "--context" "\\${script_cmd}" )
}
function adpn_script_handle_version_or_help() {
if [[ -n "${_PARAM[version]}" ]] ; then
VERSION=$(grep "^# @version" $0 | head --lines=1 | cut --only-delimited --fields=3- --delimiter=" ")
printf '%s version %s\n' "${SCRIPT_CMD_NAME}" "${VERSION}"
CMD="version"
fi
if [[ -n "${_PARAM[help]}" ]] ; then
if [[ "${_PARAM[help]}" != help ]] ; then
printf '%s\n' "${__DOC__}" | sed -E "s/\b${SCRIPT}\b/${_PARAM[help]}/"
else
printf '%s\n' "${__DOC__}"
fi
CMD="help"
fi
return 0
}
function on_drop_do() {
local -a args=( "$@" )
local result=0
REMOTE_SW="$( adpn_parameter_from "--remote" )"
[[ -z "${REMOTE_SW}" ]] && NODE_SERVER="$( adpn_parameter_from '.drop-server/ssh' )" || NODE_SERVER=""
[[ -z "${NODE_SERVER}" ]] && EXECUTE_AT="--node-server" || EXECUTE_AT="--remote=${NODE_SERVER}"
"${args[@]:0:1}" "${EXECUTE_AT}" "${args[@]:1}" || result="$?"
return "${result}"
}
function on_node_do() {
local -a args=( "$@" )
local result=0
REMOTE_SW="$( adpn_parameter_from "--remote" )"
[[ -z "${REMOTE_SW}" ]] && NODE_SERVER="$( adpn_parameter_from '.node-server/ssh' )" || NODE_SERVER=""
[[ -z "${NODE_SERVER}" || -n "${_PARAM[from-here]}" ]] && EXECUTE_AT="--as-node-server" || EXECUTE_AT="--remote=${NODE_SERVER}"
"${args[@]:0:1}" "${EXECUTE_AT}" "${args[@]:1}" || result="$?"
return "${result}"
}
function on_props_do() {
local -a args=( "$@" )
local result=0
MYSQL_HOST="$( adpn_parameter_from "--mysql-host" '.mysql/host' )"
[[ -n "${MYSQL_HOST}" ]] && EXECUTE_AT="--mysql-host=${MYSQL_HOST}" || EXECUTE_AT="--remote=$( adpnprop 'props-server/ssh' )"
"${args[@]:0:1}" "${EXECUTE_AT}" "${args[@]:1}" || result="$?"
return "${result}"
}
function adpn_script_handle_remote_execution() {
local -a ssh_flags=( ${1} ) ; shift
local -a _cmdline=( "$@" )
local result=0
local SSH_REMOTE="${_PARAM[remote]}"
local SSH_HOST="$( printf "%s" "${SSH_REMOTE}" | cut --field=1 --delimiter=":" )"
local SSH_CMD="$( printf "%s" "${SSH_REMOTE}" | cut --field=2 --delimiter=":" --only-delimited )"
if [[ -n "${SSH_HOST}" ]] ; then
[[ -z "${SSH_CMD}" ]] && SSH_CMD="bin/adpn-cli/adpn"
local SSH_CMD_SLUG="$( basename "${SSH_CMD}" )"
local SSH_CMDLINE="$( printf "%q" "${SSH_CMD}" )"
for ARG in "${_cmdline[@]}" ; do
[[ "${ARG}" =~ ^--remote(=|$) ]] || SSH_CMDLINE="$( printf "%s %q" "${SSH_CMDLINE}" "${ARG}" )"
done
[[ -z "${QQ}" ]] && adpn_notice "${__ALIASFILE__}:${LINENO}" "* Connecting to %s to execute: %s" "${SSH_HOST}" "${SSH_CMDLINE}"
ssh "${ssh_flags[@]}" "${SSH_HOST}" "${SSH_CMDLINE}"
result="$?"
[[ -z "${QQ}" ]] && adpn_notice "${__ALIASFILE__}:${LINENO}" "* Disconnected from %s" "${SSH_HOST}"
fi
return "${result}"
}
function adpn_set_pipeline_step () {
local default="${1}"
local context="${2}"
if [[ -n "${_PARAM[pipeline]}" ]] ; then
PIPELINE_SW="${_SWITCHES[pipeline]}"
PIPEDFROMTO="${_PARAM[pipeline]}"
else
PIPEDFROMTO="${default}"
PIPELINE_SW="--pipeline=${PIPEDFROMTO}"
fi
if [[ -n "${PIPEDFROMTO}" ]] ; then
PIPEDFROM=$( printf "%s" "${PIPEDFROMTO}" | cut --field=1 --delimiter=":" )
PIPEDTO=$( printf "%s" "${PIPEDFROMTO}" | cut --field=2 --delimiter=":" )
adpn_debug 100,adpn,"${context}",pipeline "SWITCH='%s', PIPEDFROMTO='%s': PIPEDFROM='%s', PIPEDTO='%s'" "${_SWITCHES[pipeline]}" "${PIPEDFROMTO}" "${PIPEDFROM}" "${PIPEDTO}"
fi
}
# adpn_check_dependency: check a list of dependencies; if at least one fails,
# maybe output instructions or suggestions about how to resolve the failure
#
# @version 2021.0407
function adpn_check_dependencies () {
local result=0
declare -a acds_argv=("${0}")
declare -A acds_switches=()
declare -A acds_params=()
while [[ "$#" -gt 0 ]] ; do
local param="${1}" ; shift
if [[ "${param}" =~ ^--([A-Za-z_0-9][^=]*)(=(.*))?$ ]] ; then
local switch="${BASH_REMATCH[1]}"
local value="${BASH_REMATCH[3]}"
if [[ -z "${BASH_REMATCH[2]}" ]] ; then
value="${switch}"
fi
acds_switches["${switch}"]="${param}"
acds_params["${switch}"]="${value}"
else
acds_argv+=("${param}")
fi
done
adpn_debug 10 "${__ALIASFILE__}:${LINENO}" "%s(%s) %s" "adpn_check_dependencies" "$( join_by ", " "${acds_argv[@]:1}" )" "$( join_by " " "${acds_switches[@]}" )"
local file="${acds_argv[1]}"
[[ -z "${file}" ]] && file=- # default to stdin
local my_feature="${acds_params[for]}"
[[ -z "${my_feature}" ]] && my_feature="any"
local -a my_features=()
IFS=$( printf ", " ) read -ra my_features<<<"${my_feature}"
IFS=""
# allow export of variables from the last command in a pipeline
push_input_loop
while read -r LINE ; do
local CONTENT="$( printf "%s\n" "${LINE}" | cut --delimiter="#" --field=1 )"
local CONTEXT="$( printf "%s\n" "${LINE}" | cut --delimiter="#" --only-delimited --fields=2- )"
local -a CONTEXTS=()
IFS=$( printf "# \t\n" ) read -ra CONTEXTS<<<"${CONTEXT}"
[[ "${#CONTEXTS[@]}" -gt 0 ]] && CONTEXTS+=( "any" )
[[ "${#CONTEXTS[@]}" -gt 0 ]] || CONTEXTS+=( ".*" )
local dependency=$( printf "%s\n" "${CONTENT}" | cut --field=1 )
local description=$( printf "%s\n" "${CONTENT}" | cut --field=2 --only-delimited )
local instruction=""
local i=3
local instruction_option=$( printf "%s\n" "${CONTENT}" | cut --field=${i} --only-delimited )
while [[ -n "${instruction_option}" ]] ; do
local packman=$( printf "%s\n" "${instruction_option}" | cut --field=1 --delimiter=" " )
which "${packman}" 2>/dev/null >/dev/null && instruction="${instruction_option}"
i=$(( i + 1 ))
instruction_option=$( printf "%s\n" "${CONTENT}" | cut --field=${i} --only-delimited )
done < "${file}"
local r=-1
for FEATURE in "${my_features[@]}" ; do
local CHECK="$( join_by "|" "${CONTEXTS[@]}" )"
if [[ "${r}" -lt 0 ]] ; then
if [[ "${FEATURE}" =~ ^(${CHECK})$ ]] ; then
if [[ -n "${dependency}" ]] ; then
local -a acd_sw=( "${dependency}" "${description}" "${instruction}" )
[[ -n "${acds_switches[interactive]}" ]] && acd_sw+=("${acds_switches[interactive]}" )
adpn_check_dependency "${acd_sw[@]}" ; r="$?"
if [[ "${r}" -gt "${result}" ]] ; then
result="${r}"
fi
fi
fi
fi
done
done
pop_input_loop
return "${result}"
}
# adpn_check_dependency: check for a dependency; in case of failure, maybe
# output instructions or suggestions about how to resolve the failure
#
# @version 2021.0407
function adpn_check_dependency () {
local -a acd_argv=("${0}")
local -a acd_switches=()
local -A acd_params=()
while [[ "$#" -gt 0 ]] ; do
local param="${1}" ; shift
if [[ "${param}" =~ ^--([A-Za-z_0-9][^=]*)(=(.*))?$ ]] ; then
local switch="${BASH_REMATCH[1]}"
local value="${BASH_REMATCH[3]}"
if [[ -z "${BASH_REMATCH[2]}" ]] ; then
value="${switch}"
fi
acd_switches+=( "${param}" )
acd_params["${switch}"]="${value}"
else
acd_argv+=("${param}")
fi
done
adpn_debug 15 "${__ALIASFILE__}:${LINENO}" "%s(%s) %s" "adpn_check_dependency" "$( join_by ", " "${acd_argv[@]:1}" )" "$( join_by " " "${acd_switches[@]}" )"
local dependency="${acd_argv[1]}"
local description="${acd_argv[2]}" ; shift
local instruction="${acd_argv[3]}" ; shift
local result=0
local dep_type=$( printf "%s" "${dependency}" | cut --field=1 --delimiter=":" --only-delimited )
local dep_name=$( printf "%s" "${dependency}" | cut --field=2 --delimiter=":" )
if [[ -n "${dependency}" ]] ; then
case "${dep_type}" in
"")
which "${dep_name}" > /dev/null || result="$?"
;;
"apt")
which "${dep_type}" &>/dev/null && if [[ -n "${dep_name}" ]] ; then
dpkg -s "${dep_name}" &> /dev/null ; result="$?"
fi
;;
"yum")
which "${dep_type}" &>/dev/null && if [[ -n "${dep_name}" ]] ; then
yum list installed "${dep_name}" &> /dev/null ; result="$?"
fi
;;
"python")
local PYTHON_IMPORT=$( printf "import %s" "${dep_name}" )
python3 -c "${PYTHON_IMPORT}" 2>/dev/null || result="$?"
;;
*)
;;
esac
fi
if [[ "${result}" -gt 0 ]] ; then
printf "Dependency Failure: %s." "${dep_name}" 1>&2
if [[ -n "${description}" ]] ; then
local s_desc=$( printf "${description}" "${dep_name}" )
printf " This script requires the %s." "${s_desc}" 1>&2
fi
printf "\n" 1>&2
if [[ -n "${instruction}" ]] ; then
if [[ -n "${acd_params[interactive]}" ]] ; then
read -n 1 -p "Install using command (${instruction}) (y/N)? " INST_YN < /dev/tty
INST_YN=$( printf "%s" "${INST_YN}" | tr "[:lower:]" "[:upper:]" )
printf "\n"
else
INST_YN="N"
fi
if [[ "Y" == "${INST_YN}" ]] ; then
sudo su -c "${instruction}" < /dev/tty ; result="$?"
else
printf "To resolve this failure, use: %s\n" "${instruction}" 1>&2
fi
fi
if [[ -n "${acd_params[or-die]}" ]] ; then
exit 255
fi
fi
return "${result}"
}
# adpn_help_notes: get help or usage notes included in the script comments,
# marked off with a @package or @method docblock directive
#
# @param string path to the script file; packages should be named the same
# as the command base name (e.g. script adpn = @package adpn)
# @param string optional, method name
# @param string optional, context of call, used in error messages
# @return int exit code, 0=success; 255=no help notes for package or method
#
# @version 2021.0407
function adpn_help_notes () {
local scriptpath="${1}" ; shift
local method="${1}" ; shift
local context="${1}" ; shift
if [[ -n "${context}" ]] ; then
context=" ${context}"
fi
local package=$(basename "${scriptpath}")
local directive="[@]package ${package}\b"
local HELPNOTES=""
if [[ ! -z "${method}" ]] ; then
directive="[@]method ${package} ${method}\b"
fi
HELPNOTES=$( \
sed --quiet "/^[#] ${directive}/,/^[#] @version/p" "${scriptpath}" \
| grep -E '^[#]' \
| sed -E 's/^[#]\s*//g' \
)
SEE_ALSO="$( printf '%s\n' "${HELPNOTES}" | grep -E "^[@]see" )"
if [[ -n "${SEE_ALSO}" ]] ; then
SEE_ALSO="$( printf '%s\n' "${SEE_ALSO}" | sed -E 's/^([@]\S+\s+)//g' )"
declare -a SEE_ALSO_CMD=()
IFS=$' \t\r\n' read -ra SEE_ALSO_CMD<<<"${SEE_ALSO}"
[[ -n "${SEE_ALSO_CMD[0]}" ]] && "${SEE_ALSO_CMD[0]}" --help="${SCRIPT_CMD_NAME}"
elif [[ -z "${HELPNOTES}" ]] ; then
echo "[${package}${context}] '${method}' help notes not yet implemented." 1>&2
EXITCODE=255
else
printf "%s\n" "${HELPNOTES}" | head -n -1 | tail -n +2
EXITCODE=0
fi
return "${EXITCODE}"
}
function adpn_notice() {
local line="${1}" ; shift
declare -a DBG_MESSAGE=( "$@" )
local line_source="${line}"
if [[ "${line_source}" =~ ^[0-9]+$ ]] ; then
line_source=$( printf "%s:%d" "${SCRIPT}" "${line}" )
fi
local message="$1"
if [[ "$#" -gt 1 ]] ; then
message="$( printf -- "$@" )"
fi
printf "[%s] %s (%s)\n" "${SCRIPT_CMD_NAME}" "${message}" "${line_source}" 1>&2
}
function adpn_debug() {
local level="${1}" ; shift
local line="${1}" ; shift
declare -a DBG_MESSAGE=( "$@" )
local -a tags=( $( printf "%s" "${level}" | tr "," "\n" | tail -n +2 ) )
level=$( printf "%s" "${level}" | cut --delimiter="," --field=1 )
local message="$1"
if [[ "$#" -gt 1 ]] ; then
message="$( printf -- "$@" )"
fi
local cmd="${SCRIPT_CMD_NAME}"
[[ -z "${cmd}" ]] && cmd="${SCRIPT}"
local line_source="${line}"
if [[ "${line_source}" =~ ^[0-9]+$ ]] ; then
line_source=$( printf "%s:%d" "${SCRIPT}" "${line}" )
fi
if [[ -n "${DBG}" ]] ; then
local dbg_tag_list=$( join_by "|" "${DBGTAGS[@]}" )
local dbg_tag_regex=$( printf "(%s)" "${dbg_tag_list}" )
local b_display=0
local s_why=""
[[ "${DBGLEVEL}" -ge "${level}" ]] && b_display=1 && s_why=$( printf "|D%03d" "${level}" )
for TAG in "${tags[@]}" ; do
[[ -n "${TAG}" && "${TAG}" =~ ^${dbg_tag_regex}$ ]] && b_display=1 && s_why=$( printf "%s/DBG=%s" "${s_why}" "${TAG}" )
done
for TAG in "${tags[@]}" ; do
[[ -n "${TAG}" && "-${TAG}" =~ ^${dbg_tag_regex}$ ]] && b_display=0
done
if [[ b_display -gt 0 ]] ; then
printf "[%s%s] %s (%s)\n" "${cmd}" "${s_why}" "${message}" "${line_source}" 1>&2
fi
fi
}
function echo_header() {
local title="${1}"
local underline="${2}"
UPTITLE=$( echo "${title}" | tr "[:lower:]" "[:upper:]" )
UNDERLINE=$( echo "${UPTITLE}" | sed "s/./${underline}/g" )
echo ""
echo "${UPTITLE}"
echo "${UNDERLINE}"
}
function adpn_read_json_packet_source() {
local PACKET_SOURCE="${1}" ; shift
local CONTEXT="${1}" ; shift
local V="${1}" ; shift
local result=0
declare -a AGGP_SW=()
AGJP_PACKETS="$(mktemp)"
GITLAB_ISSUE=$( "${ADPN}" gitlab parse "${PACKET_SOURCE}" "${CONTEXT_SW[@]}" )
if [[ -z "${PACKET_SOURCE}" || "${PACKET_SOURCE}" == "-" ]] ; then
cat - > "${AGJP_PACKETS}"
elif [[ -n "${GITLAB_ISSUE}" ]] ; then
[[ -n "${V}" ]] && printf "* Retrieving JSON for Gitlab issue: %s\n" "${GITLAB_ISSUE}" 1>&2
"${ADPN}" gitlab get packet "${GITLAB_ISSUE}" "${AGGP_SW[@]}" "${CONTEXT_SW[@]}" > "${AGJP_PACKETS}"
result="$?"
elif [[ "${PACKET_SOURCE}" =~ ^https?:// ]] ; then
[[ -n "${V}" ]] && printf "* Retrieving manifest HTML for URL: %s\n" "${PACKET_SOURCE}" 1>&2
curl --silent --fail "${PACKET_SOURCE}" > "${AGJP_PACKETS}"
result="$?"
elif [[ -d "${PACKET_SOURCE}" && -r "${PACKET_SOURCE}" ]] ; then
"${ADPN}" describe "${PACKET_SOURCE}" > "${AGJP_PACKETS}"
result="$?"
elif [[ -f "${PACKET_SOURCE}" && -r "${PACKET_SOURCE}" ]] ; then
cp "${PACKET_SOURCE}" "${AGJP_PACKETS}"
else
printf "[%s] WARNING: Cannot locate any AU data from: '%s'\n" "${CONTEXT}" "${PACKET_SOURCE}" 1>&2
result=1
fi
if [[ "${result}" -eq 0 ]] ; then
cat "${AGJP_PACKETS}"
fi
rm "${AGJP_PACKETS}"
return "${result}"
}
function adpn_write_json_packets_from () {
local web_url="${1}" ; shift
local gitlab_resource="${1}" ; shift
local -a json_defaults=()
local -a json=( "$@" )
local -a json_overlays=(
"$( adpn-json.py --key="Gitlab Issue" --value="${web_url}" --prolog )"
"$( adpn-json.py --key="Gitlab Resource" --value="${gitlab_resource}" --prolog )"
)
if [[ -n "${_PARAM[raw]}" ]] ; then
printf "%s\n" "${json[@]}"
else
for json_packet in "${json[@]}" ; do
local ingest_step="$( adpn_parameter_from ".Ingest Step<${json_packet}" )"
local -a _kv=()
[[ -n "${ingest_step}" ]] && _kv+=( "--key+=adpn:workflow" "--value+=${ingest_step}" )
json_defaults=(
"$( printf '%s\n' '{ "adpn:workflow": [] }' | adpn-json.py --output=application/json --prolog "${_kv[@]}" )"
)
printf "%s\n" "${json_defaults[@]}" "${json_packet}" "${json_overlays[@]}" | adpn-json.py --cascade --output="application/json" --prolog ; printf "\n"
done
fi
}
( which python3 2>/dev/null >/dev/null ) && export ADPN_JSON_PACKET_REGEX="$( adpn-json.py --regex )"
# adpn_get_json_packets: filter an input stream for JSON packets, based on regex
# for a JSON packet or prolog from adpn-json.py
# example: FOOBAR=$( adpnprop "${KEY}" )
#
# @param string file name of input to filter for JSON packets; "-" for stdin/pipeline
# @return int exit code from grep (0=success, 1=nothing found)
#
# @version 2021.0407
function adpn_get_json_packets() {
local file="${1}"
local packet=""
local result=0
if [[ -z "${file}" ]] ; then
file="-"
fi
packet=$( grep -P $ADPN_JSON_PACKET_REGEX "${file}" )
result="$?"
if [[ -n "${packet}" ]] ; then
printf "%s\n" "${packet}"
fi
return "${result}"
}
function adpn_select_json_packet() {
local piped_from="${1}" ; shift
local packets_from="${1}" ; shift
[[ -z "${packets_in}" ]] && packets_in="-"
local -a filter=()
IFS=" " read -ra filter <<< "${1}" ; shift
[[ "${#filter[@]}" -eq 0 ]] && filter=( "head" "-n" "1" )
local -a addon=( "adpn-json.py" "--output=application/json" "--prolog" )
local addon_step="${1}" ; shift
#[[ -z "${addon_step}" ]] &&
[[ -n "${addon_step}" ]] && addon+=( "--key+=adpn:workflow" "--value+=${addon_step}" )
if [[ "-" == "${packets_from}" || -r "${packets_from}" ]] ; then
local GREP=""
local packets_recvd="$( cat "${packets_from}" )"
# 1. Are we looking for a packet with a specific "Ingest Step" setting? If so, look for that one & prefer it.
if [[ -n "${piped_from}" ]] ; then
adpn_debug 01 "${__ALIASFILE__}:${LINENO}" 'cat "%s" | adpn-json.py --output=application/json --where="Ingest Step:%s"\n' "${packets_from}" "${piped_from}"
adpn_debug 50 "${__ALIASFILE__}:${LINENO}" 'adpn-json.py <<< ~~~\n%s\n~~~\n\n' "${packets_recvd}"
GREP="$( printf "%s" "${packets_recvd}" | adpn-json.py --parse --output=application/json --where="Ingest Step:${piped_from}" )"
result="$?"
adpn_debug 50 "${__ALIASFILE__}:${LINENO}" 'adpn-json.py >>> ~~~\n%s\n~~~\n\n' "${GREP}"
fi
# 2. If not, or if we haven't got that step, let's take one of the packets that seems to be available
# -- Use FIRST for early (verify); LAST for late (promote, etc.); parameter provides a filter command,
# e.g. head -n 1 or tail -n 1
if [[ -z "${GREP}" ]] ; then
adpn_debug 10 "${__ALIASFILE__}:${LINENO}" 'cat "%s" | adpn_get_json_packets | head -n 1\n' "${packets_from}"
GREP="$( printf "%s" "${packets_recvd}" | adpn_get_json_packets | "${filter[@]}" )"
result="$?"
[[ -n "${GREP}" ]] && adpn_debug 50 "${__ALIASFILE__}:${LINENO}" 'json by default >>> ~~~\n%s\n~~~\n\n' "${GREP}" || result=1
fi
[[ -n "${GREP}" ]] && printf "%s" "${GREP}" | "${filter[@]}" | "${addon[@]}"
adpn_debug 20 "${__ALIASFILE__}:${LINENO}" 'JSON source (%s) = <<<EOF\n%s\nEOF;\n\n' "${AIT_INPUT}" "${GREP}"
else
result=2
fi
return "${result}"
}
function adpn_opened_get_labels() {
local tag=""
local -a labels=()
for tag in "$@" ; do
if [[ "${tag}" =~ ^[/](.*)$ ]] ; then
label_prefix="DONE:"
label_content="${BASH_REMATCH[1]//_/ }"
elif [[ "${tag}" =~ crawl ]] ; then
label_prefix="WAITING:"
label_content="${tag//_/ }"
else
label_prefix="TODO:"
label_content="${tag//_/ }"
fi
labels+=( "$(join_by " " "${label_prefix}" "${label_content}" )" )
done
join_by "," "${labels[@]}"
}
function adpn_plugin_parameter_from_json() {
local parameter="${1}"
local input_file="${2}"
[[ -z "${input_file}" ]] && input_file="-"
local delimiter="${3}"
local JSON_PACKET="$( cat "${input_file}" )"
local result=1
local -A FOUND=()
push_input_loop
while read -r LINE ; do
local KEY=$( printf "%s\n" "${LINE}" | cut --field=1 )
local VALUE=$( printf "%s\n" "${LINE}" | cut --field=2- )
[[ "${KEY}" =~ ^${parameter}$ ]] && FOUND["${KEY}"]="${VALUE}" && result=0
done < <(printf "%s" "${JSON_PACKET}" | adpn-json.py --key="parameters")
pop_input_loop
join_by "${delimiter}" "${FOUND[@]}"
return "${result}"
}
function adpn_check_au_status() {
local LINE=""
local -a switches=()
local -A params=(
[test:Status]="100.00% Agreement"
[test:LastCrawlResult]="Successful"
[test:LastPollResult]="Complete"
[test:AvailableFromPublisher]="Yes"
)
local key
local value
for switch in "$@" ; do
if [[ "${switch}" =~ ^--([A-Za-z_0-9][^=]*)(=(.*))?$ ]] ; then
key="${BASH_REMATCH[1]}"
[[ -z "${BASH_REMATCH[2]}" ]] && value="${key}" || value="${BASH_REMATCH[3]}"
switches+=( "${switch}" )
params["${key}"]="${value}"
fi
done
local result=0
local piped_input="$( cat - )"
push_input_loop
while read -r LINE ; do # < <(printf "%s\n" "${piped_input}")
key="$( printf "%s" "${LINE}" | cut --field=1 )"
value="$( printf "%s" "${LINE}" | cut -s --fields=2- )"
if [[ -n "${key}" ]] ; then
local status=""
local user_value="${value}"
local -a ok_tests=()
local safe_key="$( printf '%s' "${key}" | sed -E 's/[^A-Za-z_0-9]+//g' )"
if [[ "${key}" =~ ^(Created|Last[^A-Za-z0-9]) ]] ; then
if [[ "${value}" =~ ^[-]?[0-9]+$ ]] ; then
if [[ "${value}" -gt 0 ]] ; then
local TS=""
local TS_EXIT=0
TS=$(( "${value}" / 1000 )) ; TS="$( date --date="@${TS}" )" ; TS_EXIT="$?"
ok_tests+=( "'${TS_EXIT}' -eq 0" )
[[ "${TS_EXIT}" -eq 0 ]] && user_value="${TS}"
else
user_value="$( printf "(never yet; ts=%d)" "${value}" )"
fi
fi
fi
if [[ -n "${params[test:$safe_key]}" ]] ; then
local -a cmp=( "=" "${params[test:$safe_key]}" )