-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcntbuild
executable file
·11348 lines (10241 loc) · 326 KB
/
cntbuild
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
#!/usr/bin/env bash
#######################################
# CntBuild
#
# Container images build tool
#
# Author: serdigital64 (https://github.com/serdigital64)
# Repository: https://github.com/automation64/container64
# Version: 3.0.0
#
#######################################
# Copyright [X_COPYRIGHT_YEAR_X] [X_COPYRIGHT_OWNER_X]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#######################################
#
###[ embedded-bashlib64-start ]#####################
#
#!/usr/bin/env bash
#######################################
# BashLib64 / Bash automation library
#
# Author: serdigital64 (https://github.com/serdigital64)
# Repository: https://github.com/automation64/bashlib64
#
# Copyright 2022 SerDigital64@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#######################################
#######################################
# BashLib64 / Module / Globals / Manipulate CSV like text files
#######################################
# shellcheck disable=SC2034
{
declare BL64_XSV_VERSION='2.1.0'
declare BL64_XSV_MODULE='0'
declare BL64_XSV_CMD_YQ="$BL64_VAR_UNAVAILABLE"
declare BL64_XSV_CMD_JQ="$BL64_VAR_UNAVAILABLE"
#
# Field separators
#
# shellcheck disable=SC2034
declare BL64_XSV_FS='_@64@_'
declare BL64_XSV_FS_SPACE=' '
declare BL64_XSV_FS_NEWLINE=$'\n'
declare BL64_XSV_FS_TAB=$'\t'
declare BL64_XSV_FS_COLON=':'
declare BL64_XSV_FS_SEMICOLON=';'
declare BL64_XSV_FS_COMMA=','
declare BL64_XSV_FS_PIPE='|'
declare BL64_XSV_FS_AT='@'
declare BL64_XSV_FS_DOLLAR='$'
declare BL64_XSV_FS_SLASH='/'
}
#######################################
# BashLib64 / Module / Setup / Manipulate CSV like text files
#######################################
#######################################
# Setup the bashlib64 module
#
# Arguments:
# $@: (optional) search full paths for tools
# Outputs:
# STDOUT: None
# STDERR: None
# Returns:
# 0: setup ok
# >0: setup failed
#######################################
function bl64_xsv_setup() {
[[ -z "$BL64_VERSION" ]] &&
echo 'Error: bashlib64-module-core.bash should the last sourced module' &&
return 21
local search_paths=("${@:-}")
# shellcheck disable=SC2034
_bl64_lib_module_is_imported 'BL64_DBG_MODULE' &&
bl64_dbg_lib_show_function &&
_bl64_lib_module_is_imported 'BL64_CHECK_MODULE' &&
_bl64_lib_module_is_imported 'BL64_TXT_MODULE' &&
_bl64_lib_module_is_imported 'BL64_BSH_MODULE' &&
_bl64_xsv_set_command "${search_paths[@]}" &&
BL64_XSV_MODULE="$BL64_VAR_ON"
bl64_check_alert_module_setup 'xsv'
}
#######################################
# Identify and normalize commands
#
# * If no values are provided, try to detect commands looking for common paths
# * Commands are exported as variables with full path
# * All commands are optional, no error if not found
#
# Arguments:
# None
# Outputs:
# STDOUT: None
# STDERR: None
# Returns:
# 0: always ok
#######################################
function _bl64_xsv_set_command() {
bl64_dbg_lib_show_function "$@"
BL64_XSV_CMD_JQ="$(bl64_bsh_command_locate 'jq' "$@")" &&
BL64_XSV_CMD_YQ="$(bl64_bsh_command_locate 'yq' "$@")"
}
#######################################
# BashLib64 / Module / Functions / Manipulate CSV like text files
#######################################
#######################################
# Dump file to STDOUT without comments and spaces
#
# Arguments:
# $1: Full path to the file
# Outputs:
# STDOUT: file content
# STDERR: Error messages
# Returns:
# 0: successfull execution
# BL64_LIB_ERROR_FILE_*
#######################################
function bl64_xsv_dump() {
bl64_dbg_lib_show_function "$@"
local source="$1"
bl64_check_parameter 'source' &&
bl64_check_file "$source" 'source file not found' || return $?
bl64_txt_run_egrep "$BL64_TXT_SET_GREP_INVERT" '^#.*$|^$' "$source"
}
#######################################
# Search for records based on key filters and return matching rows
#
# * Column numbers are AWK fields. First column: 1
#
# Arguments:
# $1: Single string with one ore more search values separated by $BL64_XSV_FS
# $2: source file path. Default: STDIN
# $3: one ore more column numbers (keys) where values will be searched. Format: single string using $BL64_XSV_COLON as field separator
# $4: one or more fields to show on record match. Format: single string using $BL64_XSV_COLON as field separator
# $5: field separator for the source file. Default: $BL64_XSV_COLON
# $6: field separator for the output record. Default: $BL64_XSV_COLON
# Outputs:
# STDOUT: matching records
# STDERR: Error messages
# Returns:
# 0: successfull execution
# >0: awk command exit status
#######################################
function bl64_xsv_search_records() {
bl64_dbg_lib_show_function "$@"
local values="$1"
local source="${2:--}"
local keys="${3:-1}"
local fields="${4:-0}"
local fs_src="${5:-$BL64_XSV_FS_COLON}"
local fs_out="${6:-$BL64_XSV_FS_COLON}"
# shellcheck disable=SC2086
bl64_check_parameter 'values' 'search value' || return $?
bl64_dbg_lib_show_comments 'run in a subshell to avoid leaving exported vars'
{
export BL64_XSV_FS_COLON
export BL64_XSV_FS
export BL64_LIB_ERROR_PARAMETER_INVALID
# shellcheck disable=SC2016
bl64_txt_run_awk \
-F "$fs_src" \
-v VALUES="${values}" \
-v KEYS="$keys" \
-v FIELDS="$fields" \
-v FS_OUT="$fs_out" \
'
BEGIN {
show_total = split( FIELDS, show_fields, ENVIRON["BL64_XSV_FS_COLON"] )
keys_total = split( KEYS, keys_fields, ENVIRON["BL64_XSV_FS_COLON"] )
values_total = split( VALUES, values_fields, ENVIRON["BL64_XSV_FS"] )
if( keys_total != values_total ) {
exit ENVIRON["BL64_LIB_ERROR_PARAMETER_INVALID"]
}
row_match = ""
count = 0
found = 0
}
/^#/ || /^$/ { next }
{
found = 0
for( count = 1; count <= keys_total; count++ ) {
if ( $keys_fields[count] == values_fields[count] ) {
found = 1
} else {
found = 0
break
}
}
if( found == 1 ) {
row_match = $show_fields[1]
for( count = 2; count <= show_total; count++ ) {
row_match = row_match FS_OUT $show_fields[count]
}
print row_match
}
}
END {}
' \
"$source"
}
}
#######################################
# Command wrapper with verbose, debug and common options
#
# * Trust no one. Ignore inherited config and use explicit
#
# Arguments:
# $@: arguments are passed as-is to the command
# Outputs:
# STDOUT: command output
# STDERR: command stderr
# Returns:
# 0: operation completed ok
# >0: operation failed
#######################################
# shellcheck disable=SC2120
function bl64_xsv_run_jq() {
bl64_dbg_lib_show_function "$@"
bl64_check_parameters_none "$#" &&
bl64_check_module 'BL64_XSV_MODULE' &&
bl64_check_command "$BL64_XSV_CMD_JQ" ||
return $?
bl64_dbg_lib_trace_start
"$BL64_XSV_CMD_JQ" "$@"
bl64_dbg_lib_trace_stop
}
#######################################
# Command wrapper with verbose, debug and common options
#
# * Trust no one. Ignore inherited config and use explicit
#
# Arguments:
# $@: arguments are passed as-is to the command
# Outputs:
# STDOUT: command output
# STDERR: command stderr
# Returns:
# 0: operation completed ok
# >0: operation failed
#######################################
# shellcheck disable=SC2120
function bl64_xsv_run_yq() {
bl64_dbg_lib_show_function "$@"
bl64_check_parameters_none "$#" &&
bl64_check_module 'BL64_XSV_MODULE' &&
bl64_check_command "$BL64_XSV_CMD_YQ" "$BL64_VAR_DEFAULT" 'yq' ||
return $?
bl64_dbg_lib_trace_start
"$BL64_XSV_CMD_YQ" "$@"
bl64_dbg_lib_trace_stop
}
#!/usr/bin/env bash
#######################################
# BashLib64 / Bash automation library
#
# Author: serdigital64 (https://github.com/serdigital64)
# Repository: https://github.com/automation64/bashlib64
#
# Copyright 2022 SerDigital64@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#######################################
#######################################
# BashLib64 / Module / Globals / Interact with Bash shell
#######################################
# shellcheck disable=SC2034
{
declare BL64_BSH_VERSION='3.4.0'
declare BL64_BSH_MODULE='0'
declare BL64_BSH_VERSION_BASH=''
declare BL64_BSH_ENV_STORE='.env.d'
}
#######################################
# BashLib64 / Module / Setup / Interact with Bash shell
#######################################
#######################################
# Setup the bashlib64 module
#
# * Warning: bootstrap function
#
# Arguments:
# None
# Outputs:
# STDOUT: None
# STDERR: None
# Returns:
# 0: setup ok
# >0: setup failed
#######################################
function bl64_bsh_setup() {
[[ -z "$BL64_VERSION" ]] &&
echo 'Error: bashlib64-module-core.bash should the last sourced module' &&
return 21
# shellcheck disable=SC2034
_bl64_lib_module_is_imported 'BL64_DBG_MODULE' &&
bl64_dbg_lib_show_function &&
_bl64_lib_module_is_imported 'BL64_CHECK_MODULE' &&
_bl64_lib_module_is_imported 'BL64_FMT_MODULE' &&
_bl64_lib_module_is_imported 'BL64_XSV_MODULE' &&
_bl64_lib_module_is_imported 'BL64_TXT_MODULE' &&
_bl64_lib_module_is_imported 'BL64_FS_MODULE' &&
_bl64_bsh_set_version &&
BL64_BSH_MODULE="$BL64_VAR_ON"
bl64_check_alert_module_setup 'bsh'
}
#######################################
# Identify and set module components versions
#
# * Version information is stored in module global variables
#
# Arguments:
# None
# Outputs:
# STDOUT: None
# STDERR: command errors
# Returns:
# 0: version set ok
# $BL64_LIB_ERROR_OS_BASH_VERSION
#######################################
function _bl64_bsh_set_version() {
bl64_dbg_lib_show_function
# shellcheck disable=SC2034
case "${BASH_VERSINFO[0]}" in
4*) BL64_BSH_VERSION_BASH='4.0' ;;
5*) BL64_BSH_VERSION_BASH='5.0' ;;
*)
bl64_check_alert_unsupported "Bash: ${BASH_VERSINFO[0]}"
return $BL64_LIB_ERROR_OS_BASH_VERSION
;;
esac
bl64_dbg_lib_show_vars 'BL64_BSH_VERSION_BASH'
return 0
}
#######################################
# BashLib64 / Module / Functions / Interact with Bash shell
#######################################
#
# Deprecation aliases
#
# * Aliases to deprecated functions
# * Needed to maintain compatibility up to N-2 versions
#
function bl64_bsh_script_set_id() { bl64_msg_show_deprecated 'bl64_bsh_script_set_id' 'bl64_lib_script_set_id'; bl64_lib_script_set_id "$@"; }
function bl64_bsh_script_set_identity() { bl64_msg_show_deprecated 'bl64_bsh_script_set_identity' 'bl64_lib_script_set_identity'; bl64_lib_script_set_identity "$@"; }
#
# Public functions
#
#######################################
# Get current script location
#
# Arguments:
# None
# Outputs:
# STDOUT: full path
# STDERR: Error messages
# Returns:
# 0: full path
# >0: command error
#######################################
function bl64_bsh_script_get_path() {
bl64_dbg_lib_show_function
local -i main=${#BASH_SOURCE[*]}
local caller=''
((main > 0)) && main=$((main - 1))
caller="${BASH_SOURCE[${main}]}"
unset CDPATH &&
[[ -n "$caller" ]] &&
cd -- "${caller%/*}" >/dev/null &&
pwd -P ||
return $?
}
#######################################
# Get current script name
#
# Arguments:
# None
# Outputs:
# STDOUT: script name
# STDERR: Error messages
# Returns:
# 0: name
# >0: command error
#######################################
function bl64_bsh_script_get_name() {
bl64_dbg_lib_show_function
local -i main=${#BASH_SOURCE[*]}
((main > 0)) && main=$((main - 1))
bl64_fmt_basename "${BASH_SOURCE[${main}]}"
}
#######################################
# Generate a string that can be used to populate shell.env files
#
# * Export format is bash compatible
#
# Arguments:
# $1: variable name
# $2: value
# Outputs:
# STDOUT: export string
# STDERR: Error messages
# Returns:
# 0: string created
# >0: creation error
#######################################
function bl64_bsh_env_export_variable() {
bl64_dbg_lib_show_function "$@"
local variable="${1:-${BL64_VAR_NULL}}"
local value="${2:-}"
bl64_check_parameter 'variable' ||
return $?
printf "export %s='%s'\n" "$variable" "$value"
}
#######################################
# Import shell environment variables from YAML file
#
# * Conversion is done using YQ and Awk
# * YAML nested variables are flatten and converted to single shell variables:
# * first.second: value -> FIRST_SECOND="value"
# * first.second[2]: value -> FIRST_SECOND_2="value"
# * Shell variable names are created using uppercase and exported
# * Resulting variables are saved to a temporary file which is then sourced into the current script
#
# Arguments:
# $1: path to the YAML file
# Outputs:
# STDOUT: none
# STDERR: conversion errors
# Returns:
# 0: converted and lodaded ok
# >0: failed to convert
#######################################
function bl64_bsh_env_import_yaml() {
bl64_dbg_lib_show_function "$@"
local source="$1"
local dynamic_env=''
bl64_check_parameter 'source' &&
bl64_check_file "$source" ||
return $?
# shellcheck disable=SC1090
bl64_msg_show_subtask "convert and import shell variables from YAML file (${source})"
# shellcheck disable=SC1090
dynamic_env="$(bl64_fs_create_tmpfile)" &&
bl64_xsv_run_yq \
-o p \
'.' \
"$source" |
bl64_txt_run_awk \
-F ' = ' '
{
gsub( "[.]", "_", $1 )
print "export " toupper( $1 ) "='"'"'" $2 "'"'"'"
}' >"$dynamic_env" &&
source "$dynamic_env" ||
return $?
bl64_fs_rm_tmpfile "$dynamic_env"
return 0
}
#######################################
# Determine the full path of a command
#
# * valid for command type only (type -p)
# * if the command is already a path, nothing else is done
#
# Arguments:
# $1: command name with/without path
# Outputs:
# STDOUT: full path
# STDERR: Error messages
# Returns:
# 0: full path detected
# >0: unable to detect or error
#######################################
function bl64_bsh_command_get_path() {
bl64_dbg_lib_show_function "$@"
local command="${1:-}"
local full_path=''
bl64_check_parameter 'command' ||
return $?
full_path="$(type -p "${command}")"
if [[ -n "$full_path" ]]; then
echo "$full_path"
return 0
fi
return $BL64_LIB_ERROR_TASK_FAILED
}
#######################################
# Check if the command is executable
#
# * command is first converted to full path
#
# Arguments:
# $1: command name with/without path
# Outputs:
# STDOUT: none
# STDERR: Error messages
# Returns:
# 0: command is executable
# >0: command is not present or not executable
#######################################
function bl64_bsh_command_is_executable() {
bl64_dbg_lib_show_function "$@"
local command="${1:-}"
local full_path=''
bl64_check_parameter 'command' ||
return $?
full_path="$(bl64_bsh_command_get_path "${command}")" ||
return $?
[[ ! -e "$full_path" ]] &&
return $BL64_LIB_ERROR_FILE_NOT_FOUND
[[ ! -x "$full_path" ]] &&
return $BL64_LIB_ERROR_FILE_NOT_EXECUTE
[[ -x "$full_path" ]] ||
return $BL64_LIB_ERROR_TASK_FAILED
}
#######################################
# Create env file store
#
# * Use to store .env files that can later be automatically loaded by the shell profile
#
# Arguments:
# $1: User home path. Default: HOME
# $2: permissions. Default: 0750
# $3: user name. Default: current
# $4: group name. Default: current
# Outputs:
# STDOUT: progress
# STDERR: Error messages
# Returns:
# 0: task executed ok
# >0: failed to execute task
#######################################
function bl64_bsh_env_store_create() {
bl64_dbg_lib_show_function "$@"
local home="${1:-$HOME}"
local mode="${2:-$BL64_VAR_DEFAULT}"
local user="${3:-$BL64_VAR_DEFAULT}"
local group="${4:-$BL64_VAR_DEFAULT}"
local mode='0750'
[[ "$mode" == "$BL64_VAR_DEFAULT" ]] && mode='0750'
bl64_fs_dir_create "$mode" "$user" "$group" \
"${home}/${BL64_BSH_ENV_STORE}"
}
#######################################
# Determines if the env store is present
#
# * Check that the store is presend only (directory path)
# * No check is done to detect if the shell properly configured to auto-load on login from the store
#
# Arguments:
# $1: User home path. Default: HOME
# Outputs:
# STDOUT: None
# STDERR: Error messages
# Returns:
# 0: store is present
# >0: store is not present or error
#######################################
function bl64_bsh_env_store_is_present() {
bl64_dbg_lib_show_function
local home="${1:-$HOME}"
[[ -d "${home}/${BL64_BSH_ENV_STORE}" ]]
}
#######################################
# Publish existing .env files to the store
#
# * The source file is sym-linked to the store
# * The source file must have permissions for the user to use it
#
# Arguments:
# $1: Full path to the source .env file
# $2: Load priority. Default: 64
# $3: User home path. Default: HOME
# Outputs:
# STDOUT: progress
# STDERR: Error messages
# Returns:
# 0: task executed ok
# >0: failed to execute task
#######################################
function bl64_bsh_env_store_publish() {
bl64_dbg_lib_show_function "$@"
local source_env="${1:-}"
local priority="${2:-64}"
local home="${3:-$HOME}"
local target=''
bl64_check_parameter 'source_env' &&
bl64_check_file "$source_env" &&
bl64_check_directory "${home}/${BL64_BSH_ENV_STORE}" ||
return $?
target="${home}/${BL64_BSH_ENV_STORE}/${priority}_$(bl64_fmt_basename "$source_env")" &&
bl64_fs_create_symlink \
"$source_env" \
"$target" \
"$BL64_VAR_ON"
}
#######################################
# Generate env file loader snippet
#
# * Use to generate bash snippet that can be added to user's profile
#
# Arguments:
# None
# Outputs:
# STDOUT: snippet
# STDERR: none
# Returns:
# 0: task executed ok
# >0: failed to execute task
#######################################
function bl64_bsh_env_store_generate() {
bl64_dbg_lib_show_function
# shellcheck disable=SC2016
printf '
# Load .env files from user store
if [[ -d "${HOME}/%s" ]]; then
_module=""
for _module in "${HOME}/%s"/*.env; do
[[ -r "$_module" ]] &&
source "$_module"
done
unset _module
fi\n
' "$BL64_BSH_ENV_STORE" "$BL64_BSH_ENV_STORE"
}
#######################################
# Generate bash rc snippet
#
# * Generic bashrc content to allow modular content
# * System PATH setting only
#
# Arguments:
# None
# Outputs:
# STDOUT: snippet
# STDERR: none
# Returns:
# 0: task executed ok
# >0: failed to execute task
#######################################
function bl64_bsh_profile_rc_generate() {
bl64_dbg_lib_show_function
# shellcheck disable=SC2016
printf '
# Set initial system path
export PATH="/bin:/usr/bin"
# Load global RC
[[ -f '/etc/bashrc' ]] &&
source '/etc/bashrc'
# Load user RC
if [[ -d "${HOME}/.bashrc.d" ]]; then
_module=""
for _module in "${HOME}/.bashrc.d"/*.sh; do
[[ -r "$_module" ]] &&
source "$_module"
done
unset _module
fi\n
'
}
#######################################
# Generate bash profile snippet
#
# * Generic bash_profile content to allow modular content
#
# Arguments:
# None
# Outputs:
# STDOUT: snippet
# STDERR: none
# Returns:
# 0: task executed ok
# >0: failed to execute task
#######################################
function bl64_bsh_profile_bash_generate() {
bl64_dbg_lib_show_function
# shellcheck disable=SC2016
printf '
# Import BashRC content
if [[ -f "${HOME}/.bashrc" ]]; then
source "${HOME}/.bashrc"
fi\n
'
}
#######################################
# Generate bash PATH snippet
#
# * Includes common search paths for:
# * XDG
# * NPM
#
# Arguments:
# $1: insecure setting?: ON: user paths first. OFF: user paths last. Default: OFF
# $2: include system paths?. Default: OFF
# $3: extra paths, separated by :
# Outputs:
# STDOUT: snippet
# STDERR: none
# Returns:
# 0: task executed ok
# >0: failed to execute task
#######################################
function bl64_bsh_profile_path_generate() {
bl64_dbg_lib_show_function "$@"
local insecure="${1:-$BL64_VAR_OFF}"
local system="${2:-$BL64_VAR_OFF}"
local paths_extra="${3:-}"
local paths_base=''
local paths_system=''
local paths_user=''
paths_base+='/bin:'
paths_base+='/usr/bin:'
paths_base+='/usr/local/bin'
paths_system+='/sbin:'
paths_system+='/usr/sbin:'
paths_system+='/usr/local/sbin'
# shellcheck disable=SC2016
paths_user+='$HOME/bin:'
# shellcheck disable=SC2016
paths_user+='$HOME/.local/bin:'
# shellcheck disable=SC2016
paths_user+='$HOME/node_modules/.bin'
bl64_lib_flag_is_enabled "$system" &&
paths_base+=":${paths_system}"
if bl64_lib_flag_is_enabled "$insecure"; then
printf '\nPATH="%s"\n' "${paths_extra}${paths_extra:+:}${paths_user}:${paths_base}"
else
printf '\nPATH="%s"\n' "${paths_base}:${paths_user}${paths_extra:+:}${paths_extra}"
fi
}
#######################################
# Simplified command wrapper
#
# Arguments:
# $1: target path
# Outputs:
# STDOUT: None
# STDERR: Command error
# Returns:
# 0: operation completed ok
# >0: operation failed
#######################################
function bl64_bsh_run_pushd() {
bl64_dbg_lib_show_function "$@"
local path="${1:-}"
# shellcheck disable=SC2164
pushd "$path" >/dev/null
}
#######################################
# Simplified command wrapper
#
# Arguments:
# None
# Outputs:
# STDOUT: None
# STDERR: Command error
# Returns:
# 0: operation completed ok
# >0: operation failed
#######################################
function bl64_bsh_run_popd() {
bl64_dbg_lib_show_function
# shellcheck disable=SC2164
popd >/dev/null
}
#######################################
# Search for the command in well known locations
#
# Arguments:
# $1: command name
# $@: (optional) list of additional paths where to look on
# Outputs:
# STDOUT: full path
# STDERR: Error messages
# Returns:
# 0: full path detected
# >0: unable to detect or error
#######################################
function bl64_bsh_command_locate() {
bl64_dbg_lib_show_function "$@"
local command="${1:-}"
local search_list=''
local full_path=''
local current_path=''
shift
bl64_check_parameter 'command' ||
return $?
search_list+=' /home/linuxbrew/.linuxbrew/bin'
search_list+=' /opt/homebrew/bin'
search_list+=' /usr/local/bin'
search_list+=' /usr/bin'
search_list+=' /bin'
search_list+=' /usr/sbin'
search_list+=' /sbin'
for current_path in $search_list "${@:-}"; do
bl64_dbg_lib_show_info "search in: ${current_path}/${command}"
[[ ! -d "$current_path" ]] && continue
if [[ -x "${current_path}/${command}" ]]; then
echo "${current_path}/${command}"
return 0
fi
done
bl64_check_alert_resource_not_found "$command"
}
#!/usr/bin/env bash
#######################################
# BashLib64 / Bash automation library
#
# Author: serdigital64 (https://github.com/serdigital64)
# Repository: https://github.com/automation64/bashlib64
#
# Copyright 2022 SerDigital64@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#######################################
#######################################
# BashLib64 / Module / Globals / Transfer and Receive data over the network
#######################################
# shellcheck disable=SC2034
{
declare BL64_RXTX_VERSION='2.4.3'
declare BL64_RXTX_MODULE='0'
declare BL64_RXTX_CMD_CURL=''
declare BL64_RXTX_CMD_WGET=''
declare BL64_RXTX_ALIAS_CURL=''
declare BL64_RXTX_ALIAS_WGET=''
declare BL64_RXTX_SET_CURL_FAIL=''
declare BL64_RXTX_SET_CURL_HEADER=''
declare BL64_RXTX_SET_CURL_INCLUDE=''
declare BL64_RXTX_SET_CURL_OUTPUT=''
declare BL64_RXTX_SET_CURL_REDIRECT=''
declare BL64_RXTX_SET_CURL_REQUEST=''
declare BL64_RXTX_SET_CURL_SECURE=''
declare BL64_RXTX_SET_CURL_SILENT=''
declare BL64_RXTX_SET_CURL_VERBOSE=''
declare BL64_RXTX_SET_WGET_OUTPUT=''
declare BL64_RXTX_SET_WGET_SECURE=''
declare BL64_RXTX_SET_WGET_VERBOSE=''
#
# GitHub specific parameters
#
# Public server
declare BL64_RXTX_GITHUB_URL='https://github.com'
}
#######################################
# BashLib64 / Module / Setup / Transfer and Receive data over the network
#######################################
#######################################
# Setup the bashlib64 module
#
# * Warning: bootstrap function
#
# Arguments:
# None
# Outputs:
# STDOUT: None
# STDERR: None
# Returns:
# 0: setup ok
# >0: setup failed
#######################################
function bl64_rxtx_setup() {