-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_std.sh
More file actions
4048 lines (3665 loc) · 180 KB
/
Copy pathlib_std.sh
File metadata and controls
4048 lines (3665 loc) · 180 KB
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
# shellcheck shell=bash
#
# lib_std.sh - Foundation library for Bash scripts
# Requires Bash 4.2 or higher.
#
# This library provides a standardized set of functions for common tasks,
# ensuring consistency and robustness across multiple scripts.
#
# Areas covered:
# - PATH manipulation
# - Logging (with levels and colors)
# - Error handling and stack tracing
# - Bash version check helpers
# - Library importing
# - Miscellaneous helpers
#
# Quick Reference
# --------------------------------------------------------------------------------------------------------------------
# Sourcing:
# source "<repo>/lib/bash/std/lib_std.sh"
#
# Caller-visible metadata:
# BASE_BASH_LIBS_VERSION
# Package version read from VERSION or the embedded release
# metadata carried with installed/copy-only artifacts.
# BASE_BASH_LIBS_COMMIT
# Full source commit when the package came from a checkout;
# embedded release commit or unknown for copied artifacts.
# BASE_BASH_LIBS_DIRTY_STATE
# clean, dirty, or unknown source-tree state.
# BASE_BASH_LIBS_PROVENANCE
# checkout, release-artifact, source-archive, copy, or unknown.
# BASE_BASH_LIBS_STDLIB_LOADED
# Set to 1 after lib_std.sh has been loaded successfully.
#
# Runtime globals such as BASE_BASH_LIBS_SCRIPT_ARGS and BASE_BASH_LIBS_SCRIPT_DIR are published only
# by base_init. Sourcing this file never consumes or rewrites "$@".
#
# Core helpers:
# base_std_run [opts] cmd ...
# # Safe command runner with dry-run, timeout, retry & failure handling.
# base_std_exit_if_error rc msg... # Log + exit when rc != 0 (preserves original status).
# base_std_fatal_error msg... # Convenience wrapper: exit with last status or 1.
# base_std_register_cleanup_hook fn # Run a cleanup function from the shared EXIT trap.
# base_std_register_cleanup_path p # Remove owned files/directories from EXIT cleanup.
# base_std_register_cleanup_path --unsafe p
# # Explicitly opt out of path-identity proof.
# base_std_unregister_cleanup_path p
# # Drop files/directories from the shared EXIT cleanup list.
# base_std_make_temp_file var [pfx] # Create a temp file and store its path in var.
# base_std_make_temp_dir var [pfx] # Create a temp directory and store its path in var.
# base_std_command_path var cmd # Resolve an external command path without exiting.
# base_std_function_exists fn # Predicate for defined Bash functions.
# base_std_assert_variable_name name # Validate Bash variable-name arguments.
# base_std_assert_associative_array map # Validate caller-declared associative arrays.
# base_require_version min_version
# # Exit clearly if the loaded library is too old.
# base_std_add_to_path [-n] [-p] dir # Append/prepend unique PATH entries.
# base_std_set_log_level [LEVEL] # Adjust terminal verbosity (FATAL..VERBOSE).
# base_std_set_log_category_level -l category LEVEL
# # Gate a category independently of its sinks.
# base_std_log_is_enabled [-l category] LEVEL
# # Test whether any configured sink accepts a level.
# base_std_log_info/debug/... msgs # Structured logging (color in interactive shells).
# base_std_safe_touch file [...] # touch wrapper that returns on failure (same for base_std_safe_truncate).
# assert_* utilities # Validation helpers (base_std_assert_not_null / base_std_assert_integer / ...).
#
# Patterns:
# base_std_run some_cmd # exits on failure; BASE_BASH_LIBS_DRY_RUN=true/1/yes/on prints instead.
# base_std_run --timeout 30 some_cmd
# # bounds the command attempt to 30 seconds.
# base_std_run --max-attempts 3 --retry-delay 2 some_cmd
# # retries transient failures.
# some_cmd || base_std_fatal_error ... # preserves failing exit code before terminating.
# base_std_add_to_path -p "/opt/tools" # inject directories without duplicates.
#
# Notes:
# - Call base_init <result_array> [--source <script>] [--] [argv...]
# before using stateful helpers. It strips --debug-wrapper,
# --verbose-wrapper, --utc-wrapper, and --color into the result array.
# - --verbose-wrapper is deprecated compatibility surface; prefer --debug-wrapper.
# - BASE_BASH_LIBS_BOOTSTRAP_SOURCE is accepted as a source-path fallback by the
# explicit initializer, not consumed while this file is sourced.
#
################################################# INITIALIZATION #######################################################
__base_bash_libs_std_require_supported_bash__() {
if [[ -z "${BASH_VERSION:-}" ]]; then
printf '%s\n' "Error: This script requires Bash 4.2 or higher." >&2
printf '%s\n' "Your shell is not Bash." >&2
return 1
fi
if ((BASH_VERSINFO[0] < 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 2))); then
printf '%s\n' "Error: This script requires Bash 4.2 or higher." >&2
printf '%s\n' "Your version ($BASH_VERSION) is not compatible." >&2
return 1
fi
}
# Runtime state remains passive, but loading an unsupported interpreter is a
# deterministic contract error rather than a partially-defined library.
__base_bash_libs_std_require_supported_bash__ || return 1 2>/dev/null || exit 1
unset -f __base_bash_libs_std_require_supported_bash__
__base_bash_libs_std_resolve_file_path__() {
local source_path="${1-}" link_dir target link_depth=0
[[ -n "$source_path" ]] || return 1
[[ -e "$source_path" || -L "$source_path" ]] || return 1
while [[ -L "$source_path" ]]; do
((link_depth += 1))
((link_depth <= 40)) || return 1
link_dir="$(cd -P -- "$(dirname -- "$source_path")" 2>/dev/null && pwd -P)" || return 1
target="$(readlink "$source_path")" || return 1
if [[ "$target" == /* ]]; then
source_path="$target"
else
source_path="$link_dir/$target"
fi
done
link_dir="$(cd -P -- "$(dirname -- "$source_path")" 2>/dev/null && pwd -P)" || return 1
printf '%s/%s\n' "$link_dir" "$(basename -- "$source_path")"
}
__base_bash_libs_std_read_metadata_value__() {
local metadata_file="${1-}" requested_key="${2-}" metadata_key metadata_value
[[ -r "$metadata_file" && -n "$requested_key" ]] || return 1
while IFS='=' read -r metadata_key metadata_value; do
[[ "$metadata_key" == "$requested_key" ]] || continue
printf '%s' "$metadata_value"
return 0
done < "$metadata_file"
return 1
}
__base_bash_libs_std_read_package_version__() {
local source_path="${1-}" package_root version_file metadata_file version
[[ -n "$source_path" ]] || return 1
package_root="$(cd -- "$(dirname -- "$source_path")/../../.." &>/dev/null && pwd -P)" || return 1
version_file="$package_root/VERSION"
metadata_file="$package_root/lib/bash/base-bash-libs.release"
if [[ -r "$version_file" ]]; then
IFS= read -r version < "$version_file" || true
else
version="$(
__base_bash_libs_std_read_metadata_value__ "$metadata_file" version ||
printf '%s' ""
)"
fi
[[ -n "$version" ]] || {
printf '%s\n' "Error: base-bash-libs has no readable VERSION or embedded release metadata at '$package_root'." >&2
return 1
}
printf '%s' "$version"
}
# A source guard is deliberately package-prefixed and readonly. It is the only
# source-time state other than immutable package metadata. Re-sourcing the same
# version is a no-op; attempting to mix versions fails before definitions are
# replaced.
if [[ -n "${BASE_BASH_LIBS_STD_SOURCE_GUARD+x}" ]]; then
__base_bash_libs_std_requested_source_path__="$(__base_bash_libs_std_resolve_file_path__ "${BASH_SOURCE[0]}" 2>/dev/null || printf '%s' "${BASH_SOURCE[0]}")"
__base_bash_libs_std_requested_source_version__="$(__base_bash_libs_std_read_package_version__ "$__base_bash_libs_std_requested_source_path__" 2>/dev/null || printf '%s' unknown)"
if [[ "${BASE_BASH_LIBS_STD_SOURCE_VERSION-}" != "$__base_bash_libs_std_requested_source_version__" ||
"${BASE_BASH_LIBS_STD_SOURCE_PATH-}" != "$__base_bash_libs_std_requested_source_path__" ]]; then
if [[ -n "${BASE_BASH_LIBS_STD_SOURCE_PATH-}" &&
"${BASE_BASH_LIBS_STD_SOURCE_VERSION%%.*}" != "${__base_bash_libs_std_requested_source_version__%%.*}" ]]; then
printf '%s\n' "Error: mixed-major base-bash-libs module graph refused (loaded v${BASE_BASH_LIBS_STD_SOURCE_VERSION%%.*}, requested v${__base_bash_libs_std_requested_source_version__%%.*}). v1 inputs are not fallback-loaded by v2; migrate imports to the package-relative base_std_import contract and use one v2 package root." >&2
else
printf '%s\n' "Error: incompatible base-bash-libs stdlib versions or sources are already loaded (loaded ${BASE_BASH_LIBS_STD_SOURCE_VERSION:-unknown} from ${BASE_BASH_LIBS_STD_SOURCE_PATH:-unknown}, requested $__base_bash_libs_std_requested_source_version__ from $__base_bash_libs_std_requested_source_path__)." >&2
fi
unset __base_bash_libs_std_requested_source_path__ __base_bash_libs_std_requested_source_version__
unset -f __base_bash_libs_std_read_package_version__
unset -f __base_bash_libs_std_read_metadata_value__
return 1 2>/dev/null || exit 1
fi
unset __base_bash_libs_std_requested_source_path__ __base_bash_libs_std_requested_source_version__
unset -f __base_bash_libs_std_read_package_version__ __base_bash_libs_std_read_metadata_value__
return 0
fi
if [[ -n "${BASE_BASH_LIBS_VERSION+x}" || -n "${BASE_BASH_LIBS_STDLIB_LOADED+x}" ||
-n "${BASE_BASH_LIBS_COMMIT+x}" || -n "${BASE_BASH_LIBS_DIRTY_STATE+x}" ||
-n "${BASE_BASH_LIBS_PROVENANCE+x}" ]]; then
printf '%s\n' "Error: base-bash-libs metadata names are already owned by the caller; refusing to overwrite them." >&2
return 1 2>/dev/null || exit 1
fi
BASE_BASH_LIBS_STD_SOURCE_PATH="$(__base_bash_libs_std_resolve_file_path__ "${BASH_SOURCE[0]}")" || {
printf '%s\n' "Error: Unable to resolve base-bash-libs stdlib source path from '${BASH_SOURCE[0]}'." >&2
return 1 2>/dev/null || exit 1
}
readonly BASE_BASH_LIBS_STD_SOURCE_PATH
BASE_BASH_LIBS_STD_ROOT="$(cd -- "$(dirname -- "$BASE_BASH_LIBS_STD_SOURCE_PATH")/../../.." &>/dev/null && pwd -P)" || {
printf '%s\n' "Error: Unable to resolve base-bash-libs root from '$BASE_BASH_LIBS_STD_SOURCE_PATH'." >&2
return 1 2>/dev/null || exit 1
}
readonly BASE_BASH_LIBS_STD_ROOT
readonly BASE_BASH_LIBS_MODULE_ROOT="$BASE_BASH_LIBS_STD_ROOT/lib/bash"
BASE_BASH_LIBS_VERSION="$(__base_bash_libs_std_read_package_version__ "$BASE_BASH_LIBS_STD_SOURCE_PATH")" || {
return 1 2>/dev/null || exit 1
}
readonly BASE_BASH_LIBS_VERSION
readonly BASE_BASH_LIBS_STD_SOURCE_VERSION="$BASE_BASH_LIBS_VERSION"
readonly BASE_BASH_LIBS_STD_SOURCE_GUARD=1
__base_bash_libs_std_metadata_file__="$BASE_BASH_LIBS_MODULE_ROOT/base-bash-libs.release"
__base_bash_libs_std_embedded_commit__="$(__base_bash_libs_std_read_metadata_value__ "$__base_bash_libs_std_metadata_file__" commit 2>/dev/null || printf '%s' unknown)"
__base_bash_libs_std_embedded_dirty_state__="$(__base_bash_libs_std_read_metadata_value__ "$__base_bash_libs_std_metadata_file__" dirty_state 2>/dev/null || printf '%s' unknown)"
__base_bash_libs_std_embedded_provenance__="$(__base_bash_libs_std_read_metadata_value__ "$__base_bash_libs_std_metadata_file__" provenance 2>/dev/null || printf '%s' source-archive)"
BASE_BASH_LIBS_COMMIT="$__base_bash_libs_std_embedded_commit__"
BASE_BASH_LIBS_DIRTY_STATE="$__base_bash_libs_std_embedded_dirty_state__"
BASE_BASH_LIBS_PROVENANCE="$__base_bash_libs_std_embedded_provenance__"
if [[ -e "$BASE_BASH_LIBS_STD_ROOT/.git" ]] && command -v git >/dev/null 2>&1; then
__base_bash_libs_std_git_commit__="$(git -C "$BASE_BASH_LIBS_STD_ROOT" rev-parse --verify HEAD 2>/dev/null || true)"
if [[ "$__base_bash_libs_std_git_commit__" =~ ^[[:xdigit:]]{40}$ ]]; then
BASE_BASH_LIBS_COMMIT="$__base_bash_libs_std_git_commit__"
if git -C "$BASE_BASH_LIBS_STD_ROOT" diff --quiet -- . 2>/dev/null &&
git -C "$BASE_BASH_LIBS_STD_ROOT" diff --cached --quiet -- . 2>/dev/null &&
[[ -z "$(git -C "$BASE_BASH_LIBS_STD_ROOT" status --porcelain --untracked-files=all 2>/dev/null)" ]]; then
BASE_BASH_LIBS_DIRTY_STATE=clean
else
BASE_BASH_LIBS_DIRTY_STATE=dirty
fi
BASE_BASH_LIBS_PROVENANCE=checkout
fi
fi
readonly BASE_BASH_LIBS_COMMIT BASE_BASH_LIBS_DIRTY_STATE BASE_BASH_LIBS_PROVENANCE
# Used by companion libraries as a source-order guard. This marker means the
# definitions and immutable metadata are loaded; it does not imply runtime init.
# shellcheck disable=SC2034
readonly BASE_BASH_LIBS_STDLIB_LOADED=1
declare -gA __base_bash_libs_std_import_state=()
declare -ga __base_bash_libs_std_import_stack=()
__base_bash_libs_std_import_state["$BASE_BASH_LIBS_STD_SOURCE_PATH"]=loaded
unset __base_bash_libs_std_metadata_file__ __base_bash_libs_std_embedded_commit__ __base_bash_libs_std_embedded_dirty_state__ __base_bash_libs_std_embedded_provenance__ __base_bash_libs_std_git_commit__
unset -f __base_bash_libs_std_read_package_version__ __base_bash_libs_std_read_metadata_value__
__base_bash_libs_std_is_dotted_numeric_version__() {
local version="${1-}" version_re='^[0-9]+([.][0-9]+)*$'
[[ "$version" =~ $version_re ]]
}
__base_bash_libs_std_version_at_least__() {
local actual_version="$1" minimum_version="$2"
local -a actual_parts=() minimum_parts=()
local index max_parts actual_part minimum_part actual_number minimum_number
IFS=. read -r -a actual_parts <<<"$actual_version"
IFS=. read -r -a minimum_parts <<<"$minimum_version"
max_parts="${#actual_parts[@]}"
if ((${#minimum_parts[@]} > max_parts)); then
max_parts="${#minimum_parts[@]}"
fi
for ((index = 0; index < max_parts; index++)); do
actual_part="${actual_parts[$index]:-0}"
minimum_part="${minimum_parts[$index]:-0}"
actual_number=$((10#$actual_part))
minimum_number=$((10#$minimum_part))
if ((actual_number > minimum_number)); then
return 0
fi
if ((actual_number < minimum_number)); then
return 1
fi
done
return 0
}
#
# base_require_version - Requires a minimum base-bash-libs version.
#
# Usage:
# base_require_version 1.1.0
#
base_require_version() {
local minimum_version="${1-}"
if (($# != 1)); then
base_std_log_error -l base_bash_libs.std \
"base_require_version: expected exactly one minimum version."
return 2
fi
if ! __base_bash_libs_std_is_dotted_numeric_version__ "$minimum_version" ||
! __base_bash_libs_std_is_dotted_numeric_version__ "$BASE_BASH_LIBS_VERSION"; then
base_std_log_error -l base_bash_libs.std "base_require_version expects dotted numeric versions."
return 2
fi
if ! __base_bash_libs_std_version_at_least__ "$BASE_BASH_LIBS_VERSION" "$minimum_version"; then
base_std_log_error -l base_bash_libs.std \
"base-bash-libs $minimum_version or newer is required; loaded version is $BASE_BASH_LIBS_VERSION."
return 1
fi
return 0
}
############################################ BASH VERSION CHECKER #######################################################
#
# base_std_is_interactive - Checks if the current shell is interactive.
#
# An interactive shell is one where the user is typing commands directly.
# This is used to determine if we can safely prompt the user for input.
#
# Returns:
# 0 (true) if the shell is interactive.
# 1 (false) if the shell is not interactive (e.g., running in a cron job).
#
base_std_is_interactive() {
[[ -t 0 ]]
}
#
# base_std_check_bash_version - Verifies the Bash version without prompting or installing anything.
#
# This function checks if the running Bash interpreter is version 4.2 or higher and returns
# non-zero when it is not. Base entrypoints should enforce the supported runtime before
# sourcing this library; this helper is intentionally passive so sourcing lib_std.sh never
# prompts, installs packages, or re-execs the caller.
#
# Note: This function is called before logging is initialized, so it uses `echo` to stderr.
#
base_std_check_bash_version() {
local bash_major bash_minor test_version
if [[ -n "${BASE_TEST_BASH_VERSION:-}" ]]; then
test_version="$BASE_TEST_BASH_VERSION"
if [[ "$test_version" == *.* ]]; then
bash_major="${test_version%%.*}"
bash_minor="${test_version#*.}"
else
bash_major="${test_version:0:1}"
bash_minor="${test_version:1}"
fi
else
bash_major="${BASH_VERSINFO[0]}"
bash_minor="${BASH_VERSINFO[1]}"
fi
bash_minor="${bash_minor:-0}"
if ((bash_major < 4 || (bash_major == 4 && bash_minor < 2))); then
echo "Error: This script requires Bash 4.2 or higher." >&2
echo "Your version ($BASH_VERSION) is not compatible." >&2
return 1
fi
}
###################################################### INIT ############################################################
__base_bash_libs_std_init_validate_result_array__() {
local result_name="${1-}" declaration attributes nocasematch_enabled=0 attributes_ok=0
[[ "$result_name" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || {
printf '%s\n' "base_init: result name must be a valid Bash variable name." >&2
return 1
}
[[ "$result_name" != __* ]] || {
printf '%s\n' "base_init: result name '$result_name' uses the reserved internal namespace." >&2
return 1
}
declaration="$(declare -p "$result_name" 2>/dev/null || true)"
[[ -n "$declaration" ]] || {
printf '%s\n' "base_init: result '$result_name' must be a caller-declared indexed array." >&2
return 1
}
attributes="${declaration#declare -}"
attributes="${attributes%% *}"
if shopt -q nocasematch; then
nocasematch_enabled=1
shopt -u nocasematch
fi
if [[ "$attributes" == *a* &&
"$attributes" != *A* &&
"$attributes" != *r* ]]; then
attributes_ok=1
fi
if ((nocasematch_enabled)); then
shopt -s nocasematch
fi
((attributes_ok)) || {
printf '%s\n' "base_init: result '$result_name' must be a caller-declared indexed array." >&2
return 1
}
}
__base_bash_libs_std_init_publish_array__() {
local result_name="$1" value
shift
eval "$result_name=()"
# shellcheck disable=SC2034 # eval publishes each value into a caller array.
for value; do
eval "$result_name+=(\"\$value\")"
done
}
__base_bash_libs_std_init_args_match__() {
local index=0
(($# == ${#BASE_BASH_LIBS_SCRIPT_ARGS[@]})) || return 1
for index in "${!BASE_BASH_LIBS_SCRIPT_ARGS[@]}"; do
[[ "${BASE_BASH_LIBS_SCRIPT_ARGS[$index]}" == "$1" ]] || return 1
shift
done
}
__base_bash_libs_std_initialize_runtime_state__() {
local script_dir="$1"
shift
if [[ -n "${BASE_BASH_LIBS_STD_INITIALIZED+x}" ]]; then
[[ "${BASE_BASH_LIBS_STD_INITIALIZED}" == 1 ]] || {
printf '%s\n' "base_init: runtime state marker is owned by the caller." >&2
return 1
}
return 0
fi
if [[ -n "${BASE_BASH_LIBS_STD_INIT_SOURCE+x}" || -n "${BASE_BASH_LIBS_SCRIPT_ARGS+x}" || -n "${BASE_BASH_LIBS_SCRIPT_DIR+x}" ]]; then
printf '%s\n' "base_init: initialization names are already owned by the caller." >&2
return 1
fi
__base_bash_libs_std_log_init__
declare -g BASE_BASH_LIBS_STD_COLOR_ENABLED=0
declare -ga __base_bash_libs_std_cleanup_hooks=()
declare -ga __base_bash_libs_std_cleanup_paths=()
declare -ga __base_bash_libs_std_cleanup_entries=()
declare -gA __base_bash_libs_std_cleanup_path_fingerprints=()
declare -g __base_bash_libs_std_cleanup_dispatcher_installed=0
declare -g __base_bash_libs_std_cleanup_dispatcher_running=0
declare -g __base_bash_libs_std_cleanup_dispatcher_finished=0
declare -g __base_bash_libs_std_cleanup_pending_signal_status=0
declare -g __base_bash_libs_std_cleanup_debug_guard_running=0
declare -g __base_bash_libs_std_original_exit_trap=""
declare -g __base_bash_libs_std_original_exit_trap_spec=""
declare -g __base_bash_libs_std_cleanup_dispatcher_trap_spec=""
declare -g __base_bash_libs_std_original_int_trap=""
declare -g __base_bash_libs_std_original_int_trap_spec=""
declare -g __base_bash_libs_std_cleanup_int_trap_spec="__not-installed__"
declare -g __base_bash_libs_std_original_term_trap=""
declare -g __base_bash_libs_std_original_term_trap_spec=""
declare -g __base_bash_libs_std_cleanup_term_trap_spec="__not-installed__"
declare -g __base_bash_libs_std_original_debug_trap=""
declare -g __base_bash_libs_std_original_debug_trap_spec=""
declare -g __base_bash_libs_std_cleanup_debug_trap_spec="__not-installed__"
readonly BASE_BASH_LIBS_STD_INIT_SOURCE="$script_dir"
declare -ga BASE_BASH_LIBS_SCRIPT_ARGS=("$@")
readonly -a BASE_BASH_LIBS_SCRIPT_ARGS
declare -g BASE_BASH_LIBS_SCRIPT_DIR="$script_dir"
readonly BASE_BASH_LIBS_SCRIPT_DIR
readonly BASE_BASH_LIBS_STD_INITIALIZED=1
}
#
# base_init - Explicitly initializes runtime state and filters wrapper
# flags into a caller-owned indexed array. Sourcing the library never invokes
# this function and never mutates positional parameters.
#
# Usage:
# declare -a app_args=()
# base_init app_args --source "$script" -- "$@"
#
base_init() {
local result_name="${1-}" source_path="" script_dir="" arg
local parse_config=1 color_requested=0 configure_runtime=0
local -a input_args=() filtered_args=()
(($# >= 1)) || {
printf '%s\n' "base_init: expected a result array name." >&2
return 1
}
__base_bash_libs_std_init_validate_result_array__ "$result_name" || return 1
shift
while (($#)); do
if ((parse_config)) && [[ "$1" == "--source" ]]; then
(($# >= 2)) || {
printf '%s\n' "base_init: --source requires a script path." >&2
return 1
}
source_path="$2"
shift 2
continue
fi
if ((parse_config)) && [[ "$1" == "--" ]]; then
parse_config=0
shift
input_args+=("$@")
break
fi
input_args+=("$1")
shift
done
source_path="${source_path:-${BASE_BASH_LIBS_BOOTSTRAP_SOURCE:-${BASH_SOURCE[1]-}}}"
if [[ -n "$source_path" ]]; then
script_dir="$(cd -- "$(dirname -- "$source_path")" &>/dev/null && pwd -P)" || {
printf '%s\n' "base_init: unable to resolve source directory from '$source_path'." >&2
return 1
}
else
script_dir="$(pwd -P)" || {
printf '%s\n' "base_init: unable to resolve the current caller directory." >&2
return 1
}
fi
if [[ -n "${BASE_BASH_LIBS_STD_INITIALIZED+x}" ]]; then
[[ "${BASE_BASH_LIBS_STD_INIT_SOURCE:-}" == "$script_dir" ]] || {
printf '%s\n' "base_init: already initialized for '$BASE_BASH_LIBS_STD_INIT_SOURCE'; requested '$script_dir'." >&2
return 1
}
__base_bash_libs_std_init_args_match__ "${input_args[@]+${input_args[@]}}" || {
printf '%s\n' "base_init: repeated initialization received different argv; refusing to hide the mismatch." >&2
return 1
}
else
configure_runtime=1
__base_bash_libs_std_initialize_runtime_state__ "$script_dir" "${input_args[@]+${input_args[@]}}" || return 1
fi
parse_config=1
for arg in "${input_args[@]+${input_args[@]}}"; do
if ((parse_config)) && [[ "$arg" == "--" ]]; then
filtered_args+=("$arg")
parse_config=0
continue
fi
if ((parse_config)); then
case "$arg" in
--debug-wrapper)
if ((configure_runtime)); then
base_std_set_log_level DEBUG
base_std_set_log_category_level -l base_bash_libs DEBUG
export BASE_BASH_LIBS_LOG_DEBUG=1
fi
;;
--verbose-wrapper)
if ((configure_runtime)); then
base_std_set_log_level VERBOSE
base_std_set_log_category_level -l base_bash_libs VERBOSE
export BASE_BASH_LIBS_LOG_DEBUG=1
fi
;;
--utc-wrapper)
if ((configure_runtime)); then
export BASE_BASH_LIBS_LOG_UTC=1
fi
;;
--color)
color_requested=1
;;
*)
filtered_args+=("$arg")
;;
esac
else
filtered_args+=("$arg")
fi
done
if ((configure_runtime)); then
BASE_BASH_LIBS_STD_COLOR_ENABLED="$color_requested"
__base_bash_libs_std_init_colors__
base_std_set_log_category_level -l base_bash_libs INFO
# Re-apply explicit debug levels after the default category gate.
for arg in "${input_args[@]+${input_args[@]}}"; do
if [[ "$arg" == "--debug-wrapper" ]]; then
base_std_set_log_category_level -l base_bash_libs DEBUG
elif [[ "$arg" == "--verbose-wrapper" ]]; then
base_std_set_log_category_level -l base_bash_libs VERBOSE
fi
done
fi
__base_bash_libs_std_init_publish_array__ "$result_name" "${filtered_args[@]+${filtered_args[@]}}"
return 0
}
################################################# LIBRARY IMPORTER #####################################################
#
# base_std_import - Sources package-relative library files.
#
# Every supported consumer uses this loader. Paths are relative to the loaded
# package's `lib/bash` root, never to the caller's cwd or script directory. The
# loader validates the path, resolves symlinks, rejects package-root escapes,
# tracks loading state, and sources each module at most once. During source,
# top-level `declare` statements are promoted to globals so module authors do
# not need function-scope implementation knowledge; declarations in functions
# retain their normal Bash behavior after the module is loaded.
#
# Usage:
# base_std_import str/lib_str.sh
# base_std_import file/lib_file.sh git/lib_git.sh
#
base_std_import() {
local module import_path canonical_path module_root source_status saved_declare component
local -a components=()
(($# > 0)) || {
base_std_log_error -l base_bash_libs.std \
"base_std_import: expected one or more package-relative module paths."
return 2
}
module_root="$(cd -P -- "$BASE_BASH_LIBS_MODULE_ROOT" 2>/dev/null && pwd -P)" || {
base_std_log_error -l base_bash_libs.std \
"base_std_import: package module root '$BASE_BASH_LIBS_MODULE_ROOT' is unavailable."
return 1
}
for module; do
[[ -n "$module" && "$module" != /* && "$module" != *$'\n'* && "$module" != *$'\r'* ]] || {
base_std_log_error -l base_bash_libs.std \
"base_std_import: '$module' is not a package-relative module path."
return 2
}
[[ "$module" == *.sh ]] || {
base_std_log_error -l base_bash_libs.std \
"base_std_import: module '$module' is not a shell library path."
return 2
}
case "$module" in
*'//'|/*|*/|./*|*/./*|.|..|../*|*/..|*/../*|*[^A-Za-z0-9_./-]*|*.sh/*)
base_std_log_error -l base_bash_libs.std \
"base_std_import: refusing unsafe package-relative path '$module'."
return 2
;;
esac
IFS=/ read -r -a components <<<"$module"
((${#components[@]} > 0)) || return 2
for component in "${components[@]}"; do
[[ "$component" != "" && "$component" != "." && "$component" != ".." ]] || {
base_std_log_error -l base_bash_libs.std \
"base_std_import: refusing unsafe package-relative path '$module'."
return 2
}
done
import_path="$module_root/$module"
[[ -f "$import_path" ]] || {
base_std_log_error -l base_bash_libs.std \
"base_std_import: module '$module' does not exist under '$module_root'."
return 1
}
canonical_path="$(__base_bash_libs_std_resolve_file_path__ "$import_path" 2>/dev/null || true)"
[[ -n "$canonical_path" ]] || {
base_std_log_error -l base_bash_libs.std \
"base_std_import: unable to resolve module '$module'."
return 1
}
case "$canonical_path" in
"$module_root"/*) ;;
*)
base_std_log_error -l base_bash_libs.std \
"base_std_import: refusing module '$module' because its symlink resolves outside '$module_root'."
return 2
;;
esac
case "${__base_bash_libs_std_import_state[$canonical_path]-}" in
loaded)
continue
;;
loading)
base_std_log_error -l base_bash_libs.std \
"base_std_import: dependency cycle detected while loading '$module'."
return 2
;;
esac
[[ "$canonical_path" == "$BASE_BASH_LIBS_STD_SOURCE_PATH" ||
"${BASE_BASH_LIBS_STDLIB_LOADED:-}" == "1" ]] || {
base_std_log_error -l base_bash_libs.std \
"base_std_import: module '$module' requires the std library dependency."
return 2
}
__base_bash_libs_std_import_state["$canonical_path"]=loading
__base_bash_libs_std_import_stack+=("$canonical_path")
# Bash scopes `declare` variables to this function when a file is
# sourced from here. A short-lived builtin wrapper makes module-level
# declarations global while leaving function bodies and caller state
# untouched. Preserve an intentionally caller-defined declare function.
# shellcheck disable=SC2316
saved_declare="$(builtin declare -f declare 2>/dev/null || true)"
declare() {
case "${1-}" in
-p|-F|-f)
builtin declare "$@"
;;
*)
case "${1-}" in
-g|--*) builtin declare "$@" ;;
*) builtin declare -g "$@" ;;
esac
;;
esac
}
# shellcheck disable=SC1090
if source "$canonical_path"; then
source_status=0
else
source_status=$?
fi
unset -f declare
if [[ -n "$saved_declare" ]]; then
eval "$saved_declare"
fi
__base_bash_libs_std_import_stack=()
if ((source_status != 0)); then
unset '__base_bash_libs_std_import_state['"$canonical_path"']'
base_std_log_error -l base_bash_libs.std \
"base_std_import: module '$module' failed to load (status $source_status)."
return "$source_status"
fi
__base_bash_libs_std_import_state["$canonical_path"]=loaded
done
return 0
}
################################################# PATH MANIPULATION ####################################################
#
# base_std_add_to_path - Adds one or more directories to the system PATH.
#
# This function safely adds directories to the PATH, avoiding duplicates.
#
# Usage:
# base_std_add_to_path [options] /path/to/dir1 /path/to/dir2 ...
#
# Options:
# -p : Prepend the directory to the PATH instead of appending.
# -n : Do not check if the directory exists before adding it.
#
base_std_add_to_path() {
local dir path_dir prepend=0 opt strict=1 index in_path directory_count
local -a path_dirs directories=()
local OPTIND=1
while getopts np opt; do
case "$opt" in
n) strict=0 ;; # don't care if directory exists or not before adding it to PATH
p) prepend=1 ;; # prepend the directory to PATH instead of appending
*) base_std_log_error -l base_bash_libs.std "base_std_add_to_path: invalid option '$opt'"
return 1
;;
esac
done
shift $((OPTIND-1))
directories=("$@")
directory_count=$#
if ((prepend)); then
for ((index = directory_count - 1; index >= 0; index--)); do
dir="${directories[index]}"
((strict)) && [[ ! -d $dir ]] && continue
in_path=0
IFS=: read -ra path_dirs <<< "$PATH"
for path_dir in "${path_dirs[@]+"${path_dirs[@]}"}"; do
if [[ "$path_dir" == "$dir" ]]; then
in_path=1
break
fi
done
if ((! in_path)); then
PATH="$dir:$PATH"
fi
done
else
for dir in "${directories[@]+"${directories[@]}"}"; do
in_path=0
((strict)) && [[ ! -d $dir ]] && continue
IFS=: read -ra path_dirs <<< "$PATH"
for path_dir in "${path_dirs[@]+"${path_dirs[@]}"}"; do
if [[ "$path_dir" == "$dir" ]]; then
in_path=1
break
fi
done
if ((! in_path)); then
PATH="$PATH:$dir"
fi
done
fi
# It's good practice to de-duplicate the path after adding to it
base_std_dedupe_path
return 0
}
#
# base_std_dedupe_path - Removes duplicate entries from the PATH variable.
#
base_std_dedupe_path() {
local -A seen
local IFS=':' new_path dir
for dir in $PATH; do
if [[ -n "$dir" && -z "${seen[$dir]-}" ]]; then
new_path="${new_path:+$new_path:}$dir"
seen["$dir"]=1
fi
done
PATH="$new_path"
}
#
# base_std_print_path - Prints each directory in the PATH on a new line.
#
base_std_print_path() {
local IFS=':' dirs dir
IFS=: read -ra dirs <<< "$PATH"
for dir in "${dirs[@]+"${dirs[@]}"}"; do printf '%s\n' "$dir"; done
}
#################################################### LOGGING ###########################################################
#
# __base_bash_libs_std_log_init__ - Initializes the logging system.
#
# Sets up colors for interactive terminals and defines the log level hierarchy.
# This is called by base_init.
#
__base_bash_libs_std_log_init__() {
# Map log level strings (FATAL, ERROR, etc.) to numeric values.
# Note the '-g' option passed to declare is essential for global scope.
unset BASE_BASH_LIBS_STD_LOG_LEVELS BASE_BASH_LIBS_STD_LOGGER_LEVELS BASE_BASH_LIBS_STD_LOG_CATEGORY_LEVELS BASE_BASH_LIBS_STD_LOG_FAILED_SINKS
declare -gA BASE_BASH_LIBS_STD_LOG_LEVELS BASE_BASH_LIBS_STD_LOGGER_LEVELS BASE_BASH_LIBS_STD_LOG_CATEGORY_LEVELS BASE_BASH_LIBS_STD_LOG_FAILED_SINKS
BASE_BASH_LIBS_STD_LOG_FAILED_SINKS=()
# VERBOSE is deprecated compatibility surface; new callers should use DEBUG.
BASE_BASH_LIBS_STD_LOG_LEVELS=([FATAL]=0 [ERROR]=1 [WARN]=2 [INFO]=3 [DEBUG]=4 [VERBOSE]=5)
# Terminal output defaults to INFO. Category filtering is a separate,
# permissive gate so existing callers retain their current sink behavior.
BASE_BASH_LIBS_STD_LOGGER_LEVELS["default"]=3
BASE_BASH_LIBS_STD_LOG_CATEGORY_LEVELS["default"]=5
}
#
# __base_bash_libs_std_join_message__ - Join message fragments with a stable single-space separator.
#
__base_bash_libs_std_join_message__() {
local __base_bash_libs_std_join_message_result="" __base_bash_libs_std_join_message_fragment __base_bash_libs_std_join_message_separator=""
for __base_bash_libs_std_join_message_fragment in "$@"; do
__base_bash_libs_std_join_message_result+="${__base_bash_libs_std_join_message_separator}${__base_bash_libs_std_join_message_fragment}"
__base_bash_libs_std_join_message_separator=" "
done
printf '%s' "$__base_bash_libs_std_join_message_result"
}
#
# __base_bash_libs_std_log_timestamp__ - Store the current log timestamp in a named variable.
#
__base_bash_libs_std_log_timestamp__() {
local __base_bash_libs_std_log_timestamp_result_name="$1"
if [[ "${BASE_BASH_LIBS_LOG_UTC:-}" == 1 ]]; then
TZ=UTC0 printf -v "$__base_bash_libs_std_log_timestamp_result_name" '%(%Y-%m-%d %H:%M:%S)T UTC' -1
else
printf -v "$__base_bash_libs_std_log_timestamp_result_name" '%(%Y-%m-%d %H:%M:%S %z)T' -1
fi
}
#
# __base_bash_libs_std_log_source_location__ - Store the first non-stdlib caller location.
#
__base_bash_libs_std_log_source_location__() {
local __base_bash_libs_std_log_source_result_name="$1"
local __base_bash_libs_std_log_source_fallback_path="${2:-}" __base_bash_libs_std_log_source_fallback_line="${3:-0}"
local __base_bash_libs_std_log_source_path="" __base_bash_libs_std_log_source_line=""
local __base_bash_libs_std_log_source_frame=1 __base_bash_libs_std_log_source_max_frames=20
local __base_bash_libs_std_log_source_caller_info __base_bash_libs_std_log_source_caller_rest
local __base_bash_libs_std_log_source_caller_line __base_bash_libs_std_log_source_caller_file
while ((__base_bash_libs_std_log_source_frame <= __base_bash_libs_std_log_source_max_frames)) &&
__base_bash_libs_std_log_source_caller_info=$(caller "$__base_bash_libs_std_log_source_frame"); do
__base_bash_libs_std_log_source_caller_line="${__base_bash_libs_std_log_source_caller_info%% *}"
__base_bash_libs_std_log_source_caller_rest="${__base_bash_libs_std_log_source_caller_info#* }"
__base_bash_libs_std_log_source_caller_file="${__base_bash_libs_std_log_source_caller_rest#* }"
if [[ -n "$__base_bash_libs_std_log_source_caller_file" &&
"$__base_bash_libs_std_log_source_caller_file" != "$BASE_BASH_LIBS_STD_SOURCE_PATH" ]]; then
__base_bash_libs_std_log_source_path="$__base_bash_libs_std_log_source_caller_file"
__base_bash_libs_std_log_source_line="$__base_bash_libs_std_log_source_caller_line"
break
fi
((__base_bash_libs_std_log_source_frame++))
done
if [[ -z "$__base_bash_libs_std_log_source_path" ]]; then
__base_bash_libs_std_log_source_path="${__base_bash_libs_std_log_source_fallback_path:-${BASH_SOURCE[2]:-${BASH_SOURCE[1]:-${BASH_SOURCE[0]:-unknown}}}}"
__base_bash_libs_std_log_source_line="${__base_bash_libs_std_log_source_fallback_line:-${BASH_LINENO[1]:-${BASH_LINENO[0]:-0}}}"
fi
__base_bash_libs_std_log_source_path="${__base_bash_libs_std_log_source_path#"$BASE_BASH_LIBS_SCRIPT_DIR"/}"
__base_bash_libs_std_log_source_path="${__base_bash_libs_std_log_source_path#./}"
printf -v "$__base_bash_libs_std_log_source_result_name" '%s:%s' "$__base_bash_libs_std_log_source_path" "$__base_bash_libs_std_log_source_line"
}
#
# __base_bash_libs_std_log_primary_sink_is_usable__ - Check the primary sink without modifying it.
#
__base_bash_libs_std_log_primary_sink_is_usable__() {
local __base_bash_libs_std_log_primary_usable_path="${1-}" __base_bash_libs_std_log_primary_usable_parent_dir
[[ -n "$__base_bash_libs_std_log_primary_usable_path" && "$__base_bash_libs_std_log_primary_usable_path" != */ ]] || return 1
[[ -z "${BASE_BASH_LIBS_STD_LOG_FAILED_SINKS[$__base_bash_libs_std_log_primary_usable_path]+set}" ]] || return 1
[[ ! -L "$__base_bash_libs_std_log_primary_usable_path" ]] || return 1
if [[ -e "$__base_bash_libs_std_log_primary_usable_path" ]]; then
if [[ -f "$__base_bash_libs_std_log_primary_usable_path" && -O "$__base_bash_libs_std_log_primary_usable_path" &&
-w "$__base_bash_libs_std_log_primary_usable_path" ]]; then
return 0
fi
return 1
fi
if [[ "$__base_bash_libs_std_log_primary_usable_path" == */* ]]; then
__base_bash_libs_std_log_primary_usable_parent_dir="${__base_bash_libs_std_log_primary_usable_path%/*}"
[[ -n "$__base_bash_libs_std_log_primary_usable_parent_dir" ]] || __base_bash_libs_std_log_primary_usable_parent_dir=/
else
__base_bash_libs_std_log_primary_usable_parent_dir=.
fi
[[ -d "$__base_bash_libs_std_log_primary_usable_parent_dir" && -w "$__base_bash_libs_std_log_primary_usable_parent_dir" &&
-x "$__base_bash_libs_std_log_primary_usable_parent_dir" ]]
}
#
# __base_bash_libs_std_log_primary_sink_prepare__ - Create or privately harden a usable sink.
#
__base_bash_libs_std_log_primary_sink_prepare__() {
local __base_bash_libs_std_log_primary_prepare_path="$1" __base_bash_libs_std_log_primary_prepare_chmod_path
__base_bash_libs_std_log_primary_sink_is_usable__ "$__base_bash_libs_std_log_primary_prepare_path" || return 1
if [[ ! -e "$__base_bash_libs_std_log_primary_prepare_path" ]]; then
# noclobber avoids truncating a target that appears after the
# non-mutating eligibility check.
if ! (umask 077; set -o noclobber; : >"$__base_bash_libs_std_log_primary_prepare_path") 2>/dev/null; then
[[ -e "$__base_bash_libs_std_log_primary_prepare_path" && ! -L "$__base_bash_libs_std_log_primary_prepare_path" ]] || return 1
fi
fi
[[ -f "$__base_bash_libs_std_log_primary_prepare_path" && ! -L "$__base_bash_libs_std_log_primary_prepare_path" &&
-O "$__base_bash_libs_std_log_primary_prepare_path" && -w "$__base_bash_libs_std_log_primary_prepare_path" ]] || return 1
# macOS chmod does not accept "--"; prefix a bare option-like path instead.
__base_bash_libs_std_log_primary_prepare_chmod_path="$__base_bash_libs_std_log_primary_prepare_path"
[[ "$__base_bash_libs_std_log_primary_prepare_chmod_path" == -* ]] &&
__base_bash_libs_std_log_primary_prepare_chmod_path="./$__base_bash_libs_std_log_primary_prepare_chmod_path"
command chmod 600 "$__base_bash_libs_std_log_primary_prepare_chmod_path" 2>/dev/null || return 1
[[ -f "$__base_bash_libs_std_log_primary_prepare_path" && ! -L "$__base_bash_libs_std_log_primary_prepare_path" &&
-O "$__base_bash_libs_std_log_primary_prepare_path" && -w "$__base_bash_libs_std_log_primary_prepare_path" ]]
}
#
# __base_bash_libs_std_log_primary_sink_append__ - Append one record or file payload.
#
__base_bash_libs_std_log_primary_sink_append__() {
local __base_bash_libs_std_log_primary_append_kind="${1-}" __base_bash_libs_std_log_primary_append_payload="${2-}"
local __base_bash_libs_std_log_primary_append_path="${BASE_BASH_LIBS_PRIMARY_LOG:-}"
__base_bash_libs_std_log_primary_sink_is_usable__ "$__base_bash_libs_std_log_primary_append_path" || return 1
(
umask 077
__base_bash_libs_std_log_primary_sink_prepare__ "$__base_bash_libs_std_log_primary_append_path" || exit 1
case "$__base_bash_libs_std_log_primary_append_kind" in
record)
printf '%s\n' "$__base_bash_libs_std_log_primary_append_payload"
;;
file)
command cat -- "$__base_bash_libs_std_log_primary_append_payload" || exit 1
printf '\n'
;;
*)
exit 1
;;