-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall
More file actions
executable file
·4049 lines (3525 loc) · 135 KB
/
Copy pathinstall
File metadata and controls
executable file
·4049 lines (3525 loc) · 135 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
#!/usr/bin/env bash
###############################################################################
# GENERATED FROM https://github.com/fchastanet/bash-dev-env/tree/master/src/_tools/install-binary.yaml
# DO NOT EDIT IT
# @generated
###############################################################################
# shellcheck disable=SC2288,SC2034
# ensure that no user aliases could interfere with
# commands used in this script
unalias -a || true
shopt -u expand_aliases
# shellcheck disable=SC2034
((failures = 0)) || true
# Bash will remember & return the highest exit code in a chain of pipes.
# This way you can catch the error inside pipes, e.g. mysqldump | gzip
set -o pipefail
set -o errexit
# Command Substitution can inherit errexit option since bash v4.4
shopt -s inherit_errexit || true
# if set, and job control is not active, the shell runs the last command
# of a pipeline not executed in the background in the current shell
# environment.
shopt -s lastpipe
# a log is generated when a command fails
set -o errtrace
# use nullglob so that (file*.php) will return an empty array if no file
# matches the wildcard
shopt -s nullglob
# ensure regexp are interpreted without accentuated characters
export LC_ALL=POSIX
export TERM=xterm-256color
# avoid interactive install
export DEBIAN_FRONTEND=noninteractive
export DEBCONF_NONINTERACTIVE_SEEN=true
# store command arguments for later usage
# shellcheck disable=SC2034
declare -a BASH_FRAMEWORK_ARGV=("$@")
# shellcheck disable=SC2034
declare -a ORIGINAL_BASH_FRAMEWORK_ARGV=("$@")
# @see https://unix.stackexchange.com/a/386856
# shellcheck disable=SC2317
interruptManagement() {
# restore SIGINT handler
trap - INT
# ensure that Ctrl-C is trapped by this script and not by sub process
# report to the parent that we have indeed been interrupted
kill -s INT "$$"
}
trap interruptManagement INT
################################################
# Temp dir management
################################################
KEEP_TEMP_FILES="${KEEP_TEMP_FILES:-0}"
export KEEP_TEMP_FILES
# PERSISTENT_TMPDIR is not deleted by traps
PERSISTENT_TMPDIR="${TMPDIR:-/tmp}/bash-framework"
export PERSISTENT_TMPDIR
if [[ ! -d "${PERSISTENT_TMPDIR}" ]]; then
mkdir -p "${PERSISTENT_TMPDIR}"
fi
# shellcheck disable=SC2034
TMPDIR="$(mktemp -d -p "${PERSISTENT_TMPDIR:-/tmp}" -t bash-framework-$$-XXXXXX)"
export TMPDIR
# temp dir cleaning
# shellcheck disable=SC2317
cleanOnExit() {
local rc=$?
if [[ "${KEEP_TEMP_FILES:-0}" = "1" ]]; then
Log::displayInfo "KEEP_TEMP_FILES=1 temp files kept here '${TMPDIR}'"
elif [[ -n "${TMPDIR+xxx}" ]]; then
Log::displayDebug "KEEP_TEMP_FILES=0 removing temp files '${TMPDIR}'"
rm -Rf "${TMPDIR:-/tmp/fake}" >/dev/null 2>&1
fi
exit "${rc}"
}
trap cleanOnExit EXIT HUP QUIT ABRT TERM
SCRIPT_NAME=${0##*/}
REAL_SCRIPT_FILE="$(readlink -e "$(realpath "${BASH_SOURCE[0]}")")"
if [[ -n "${EMBED_CURRENT_DIR}" ]]; then
CURRENT_DIR="${EMBED_CURRENT_DIR}"
else
CURRENT_DIR="${REAL_SCRIPT_FILE%/*}"
fi
FRAMEWORK_ROOT_DIR="$(cd "${CURRENT_DIR}/." && pwd -P)"
FRAMEWORK_SRC_DIR="${FRAMEWORK_ROOT_DIR}/src"
FRAMEWORK_BIN_DIR="${FRAMEWORK_ROOT_DIR}/bin"
FRAMEWORK_VENDOR_DIR="${FRAMEWORK_ROOT_DIR}/vendor"
FRAMEWORK_VENDOR_BIN_DIR="${FRAMEWORK_ROOT_DIR}/vendor/bin"
# @description Log namespace provides 2 kind of functions
# - Log::display* allows to display given message with
# given display level
# - Log::log* allows to log given message with
# given log level
# Log::display* functions automatically log the message too
# @see Env::requireLoad to load the display and log level from .env file
# @description log level off
export __LEVEL_OFF=0
# @description log level error
export __LEVEL_ERROR=1
# @description log level warning
export __LEVEL_WARNING=2
# @description log level info
export __LEVEL_INFO=3
# @description log level success
export __LEVEL_SUCCESS=3
# @description log level debug
export __LEVEL_DEBUG=4
# @description verbose level off
export __VERBOSE_LEVEL_OFF=0
# @description verbose level info
export __VERBOSE_LEVEL_INFO=1
# @description verbose level info
export __VERBOSE_LEVEL_DEBUG=2
# @description verbose level info
export __VERBOSE_LEVEL_TRACE=3
# @description check if an element is contained in an array
#
# @arg $1 needle:String
# @arg $@ array:String[]
# @exitcode 0 if found
# @exitcode 1 otherwise
# @example
# Array::contains "${libPath}" "${__BASH_FRAMEWORK_IMPORTED_FILES[@]}"
Array::contains() {
local element
for element in "${@:2}"; do
[[ "${element}" = "$1" ]] && return 0
done
return 1
}
# @description concatenate each element of an array with a separator
# but wrapping text when line length is more than provided argument
# The algorithm will try not to cut the array element if it can.
# - if an arg can be placed on current line it will be,
# otherwise current line is printed and arg is added to the new
# current line
# - Empty arg is interpreted as a new line.
# - Add \r to arg in order to force break line and avoid following
# arg to be concatenated with current arg.
#
# @arg $1 glue:String
# @arg $2 maxLineLength:int
# @arg $3 indentNextLine:int
# @arg $@ array:String[]
Array::wrap2() {
local glue="${1-}"
local -i glueLength="${#glue}"
shift || true
local -i maxLineLength=$1
shift || true
local -i indentNextLine=$1
shift || true
local indentStr=""
if ((indentNextLine > 0)); then
indentStr="$(head -c "${indentNextLine}" </dev/zero | tr '\0' " ")"
fi
if (($# == 0)); then
return 0
fi
printCurrentLine() {
if ((isNewline == 0)) || ((previousLineEmpty == 1)); then
echo
fi
((isNewline = 1))
echo -en "${indentStr}"
((currentLineLength = indentNextLine)) || true
}
appendToCurrentLine() {
local text="$1"
local -i length=$2
((currentLineLength += length)) || true
((isNewline = 0)) || true
if [[ "${text: -1}" = $'\r' ]]; then
text="${text:0:-1}"
echo -en "${text%%+([[:blank:]])}"
printCurrentLine
else
echo -en "${text%%+([[:blank:]])}"
fi
}
(
local currentLine
local -i currentLineLength=0 isNewline=1 argLength=0
local -a additionalLines
local -i previousLineEmpty=0
local arg=""
while (($# > 0)); do
arg="$1"
shift || true
# replace tab by 2 spaces
arg="${arg//$'\t'/ }"
# remove trailing spaces
arg="${arg%[[:blank:]]}"
if [[ "${arg}" = $'\n' || -z "${arg}" ]]; then
printCurrentLine
((previousLineEmpty = 1))
continue
else
if ((previousLineEmpty == 1)); then
printCurrentLine
fi
((previousLineEmpty = 0)) || true
fi
# convert eol to args
mapfile -t additionalLines <<<"${arg}"
if ((${#additionalLines[@]} > 1)); then
set -- "${additionalLines[@]}" "$@"
continue
fi
((argLength = ${#arg})) || true
# empty arg
if ((argLength == 0)); then
if ((isNewline == 0)); then
# isNewline = 0 means currentLine is not empty
printCurrentLine
fi
continue
fi
if ((isNewline == 0)); then
glueLength="${#glue}"
else
glueLength="0"
fi
if ((currentLineLength + argLength + glueLength > maxLineLength)); then
if ((argLength + glueLength > maxLineLength)); then
# arg is too long to even fit on one line
# we have to split the arg on current and next line
local -i remainingLineLength
((remainingLineLength = maxLineLength - currentLineLength - glueLength))
appendToCurrentLine "${glue:0:${glueLength}}${arg:0:${remainingLineLength}}" "$((glueLength + remainingLineLength))"
printCurrentLine
arg="${arg:${remainingLineLength}}"
# remove leading spaces
arg="${arg##[[:blank:]]}"
set -- "${arg}" "$@"
else
# the arg can fit on next line
printCurrentLine
appendToCurrentLine "${arg}" "${argLength}"
fi
else
appendToCurrentLine "${glue:0:${glueLength}}${arg}" "$((glueLength + argLength))"
fi
done
if [[ "${currentLine}" != "" ]] && [[ ! "${currentLine}" =~ ^[\ \t]+$ ]]; then
printCurrentLine
fi
) | sed -E -e 's/[[:blank:]]+$//'
}
# @description check if command specified exists or return 1
# with error and message if not
#
# @arg $1 commandName:String on which existence must be checked
# @arg $2 helpIfNotExists:String a help command to display if the command does not exist
#
# @exitcode 1 if the command specified does not exist
# @stderr diagnostic information + help if second argument is provided
Assert::commandExists() {
local commandName="$1"
local helpIfNotExists="$2"
"${BASH_FRAMEWORK_COMMAND:-command}" -v "${commandName}" >/dev/null 2>/dev/null || {
Log::displayError "${commandName} is not installed, please install it"
if [[ -n "${helpIfNotExists}" ]]; then
Log::displayInfo "${helpIfNotExists}"
fi
return 1
}
return 0
}
# @description check if param is valid email address
# @warning it is a very simple check, no RFC validation
# @arg $1 emailAddress:String the full email address
# @exitcode 1 if invalid email address
Assert::emailAddress() {
local expectedRegexp="^\S+@\S+$"
[[ "$1" =~ ${expectedRegexp} ]]
}
# @description check if argument respects 2 or more words separated by a space
# it supports accentuated characters and names with hyphen(-)
# @arg $1 firstNameLastName:String
# @exitcode 1 if regexp not matches
# @see https://regex101.com/r/JyyfOM/1
Assert::firstNameLastName() {
local regexp="^[^ ]+([ ][^ ]+)+$"
[[ $1 =~ ${regexp} ]]
}
# @description check if argument respects ldap login naming convention
# only using lowercase characters a-z
# @arg $1 ldapLogin:String
# @exitcode 1 if regexp not matches
Assert::ldapLogin() {
[[ $1 =~ ^[a-z]+$ ]]
}
# @description check if tty (interactive mode) is active
# @noargs
# @exitcode 1 if tty not active
# @env NON_INTERACTIVE if 1 consider as not interactive even if environment is interactive
# @env INTERACTIVE if 1 consider as interactive even if environment is not interactive
Assert::tty() {
if [[ "${NON_INTERACTIVE:-0}" = "1" ]]; then
return 1
fi
if [[ "${INTERACTIVE:-0}" = "1" ]]; then
return 0
fi
tty -s
}
# @description check if argument respects this framework variable naming convention
# - if variable begins with an uppercase or underscore, following letters have to be uppercase or underscore
# - variable name can includes ':' or '_' or digits but not as first letter
# here valid variable name examples
#
# @arg $1 variableName:String
# @exitcode 1 if regexp not matches
# @see https://regex101.com/r/BUlPXS/1
Assert::validVariableName() {
[[ "$1" =~ (^[a-z][A-Za-z_0-9:]+$)|(^[A-Z_][A-Z_0-9:]+$) ]]
}
# @description checks if variable name provided exists
# @arg $1 varName:String
# @exitcode 1 if variable doesn't exist
# @exitcode 2 if variable value empty
# @exitcode 3 if variable name invalid
# @see Assert::validVariableName
# @stderr diagnostics information is displayed
Assert::varExistsAndNotEmpty() {
local varName="$1"
if ! Assert::validVariableName "${varName}"; then
Log::displayError "${varName} - invalid variable name"
return 3
fi
if [[ -z "${!varName+unset}" ]]; then
Log::displayError "${varName} - not defined"
return 1
elif [[ -z "${!varName}" ]]; then
Log::displayError "${varName} - please provide a value"
return 2
fi
}
# @description determine if the script is executed under WSL
# cspell:disable
# @example text
# uname GitBash windows (with wsl) => MINGW64_NT-10.0 ZOXFL-6619QN2 2.10.0(0.325/5/3) 2018-06-13 23:34 x86_64 Msys
# uname GitBash windows (wo wsl) => MINGW64_NT-10.0 frsa02-j5cbkc2 2.9.0(0.318/5/3) 2018-01-12 23:37 x86_64 Msys
# uname wsl => Linux ZOXFL-6619QN2 4.4.0-17134-Microsoft #112-Microsoft Thu Jun 07 22:57:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux
# cspell:enable
#
# @exitcode 1 on error
Assert::wsl() {
[[ "$(uname -o)" = "GNU/Linux" ]]
}
# @description get property value from file
# if not present compute it using propertyNotFoundCallback (if provided) and store it in property file
# @arg $1 propertyFile:String the file in which the property will be searched
# @arg $2 key:String the property key to search in property file
# @arg $3 propertyNotFoundCallback:Function (optional) a callback to call if property key is not found in property file
# @arg $@ args:String[] (optional) the arguments to pass to the propertyNotFoundCallback
# @exitcode 1 if value is not found
# @exitcode * if propertyNotFoundCallback fails
# @stdout the property value given by property file or by the propertyNotFoundCallback
Cache::getPropertyValue2() {
local propertyFile="$1"
local -n propertiesMap=$2
local -n getPropertyValue2_val=$3
local key="$4"
local propertyNotFoundCallback=$5
shift 5 || true
local -a args=("$@")
if [[ "${#propertiesMap[@]}" = "0" && -s "${propertyFile}" ]]; then
local line
while IFS="" read -r line; do
if [[ "${line}" =~ ^([^=]+)=(.+)$ ]]; then
propertiesMap["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
fi
done <"${propertyFile}"
fi
if [[ -n "${propertiesMap[${key}]+abc}" ]]; then
getPropertyValue2_val="${propertiesMap[${key}]}"
return 0
elif [[ "$(type -t "${propertyNotFoundCallback}")" = "function" ]]; then
getPropertyValue2_val="$("${propertyNotFoundCallback}" "${args[@]}")" || return $?
propertiesMap["${key}"]="${getPropertyValue2_val}"
echo "${key}=${getPropertyValue2_val}" >>"${propertyFile}"
return 0
fi
return 1
}
# @description convert base64 encoded back to target file
# if target file is executable prepend dir of target
# file to PATH to make binary available everywhere
# it is advised to include in the path of the target file
# the md5sum of the binFile
#
# @arg $1 targetFile:String the file to write
# @arg $2 binFileBase64:String the base64 encoded file
# @arg $3 fileMode:String the chmod to set on the file
# @set PATH String prepend target embedded file binary directory to PATH variable if binary executable
Compiler::Embed::extractFileFromBase64() {
local targetFile="$1"
local binFileBase64="$2"
local fileMode="${3:-+x}"
local targetDir="${targetFile%/*}"
if [[ ! -f "${targetFile}" ]]; then
if [[ ! -d "${targetDir}" ]]; then
mkdir -p "${targetDir}"
fi
tr -d '\n ' <<<"${binFileBase64}" | base64 -d >"${targetFile}"
chmod "${fileMode}" "${targetFile}"
fi
if [[ -x "${targetFile}" ]]; then
Env::pathPrepend "${targetDir}"
fi
}
# @description list files of dir with given extension and display it as a list one by line
#
# @arg $1 dir:String the directory to list
# @arg $2 prefix:String the profile file prefix (default: "")
# @arg $3 ext:String the extension
# @arg $4 findOptions:String find options, eg: -type d (Default value: '-type f')
# @arg $5 indentStr:String the indentation can be any string compatible with sed not containing any / (Default value: ' - ')
# @stdout list of files without extension/directory
# @example text
# - default.local
# - default.remote
# - localhost-root
# @exitcode 1 if directory does not exists
Conf::list() {
local dir="$1"
local prefix="${2:-}"
local ext="${3}"
local findOptions="${4--type f}"
local indentStr="${5- - }"
if [[ ! -d "${dir}" ]]; then
Log::displayError "Directory ${dir} does not exist"
fi
if [[ -n "${ext}" && "${ext:0:1}" != "." ]]; then
ext=".${ext}"
fi
(
# shellcheck disable=SC2086
cd "${dir}" &&
find . -maxdepth 1 ${findOptions} -name "${prefix}*${ext}" |
sed -E "s#^\./${prefix}##g" |
sed -E "s#${ext}\$##g" | sort | sed -E "s#^#${indentStr}#"
)
}
# @description Load the nearest config file
# in next example will search first .framework-config file in "srcDir1"
# then if not found will go in up directories until /
# then will search in "srcDir2"
# then if not found will go in up directories until /
# source the file if found
# @example
# Conf::loadNearestFile ".framework-config" "srcDir1" "srcDir2"
#
# @arg $1 configFileName:String config file name to search
# @arg $2 loadedFile:String (passed by reference) will return the loaded config file name
# @arg $@ srcDirs:String[] source directories in which the config file will be searched
# @exitcode 0 if file found
# @exitcode 1 if file not found
Conf::loadNearestFile() {
local configFileName="$1"
local -n loadedFile="$2"
shift 2 || true
local -a srcDirs=("$@")
for srcDir in "${srcDirs[@]}"; do
configFile="$(File::upFind "${srcDir}" "${configFileName}" || true)"
if [[ -n "${configFile}" ]]; then
# shellcheck source=/.framework-config
source "${configFile}" || Log::fatal "error while loading config file '${configFile}'"
Log::displayDebug "Config file ${configFile} is loaded"
# shellcheck disable=SC2034
loadedFile="${configFile}"
return 0
fi
done
Log::displayDebug "Config file '${configFileName}' not found in any source directories provided"
return 1
}
# @description check validity of .env variables
# @env CHECK_ENV int 0 to avoid checking environment
# @noargs
Engine::Config::checkEnv() {
local envFile="$1"
if [[ "${CHECK_ENV:-1}" = "0" ]]; then
return 0
fi
# avoid checks if .env file didn't changed
local envFileMd5Cache="${PERSISTENT_TMPDIR:-/tmp}/bash-dev-env-enf-file-checksum"
if md5sum -c "${envFileMd5Cache}" &>/dev/null; then
return 0
else
md5sum "${envFile}" >"${envFileMd5Cache}"
fi
local errorCount=0 || true
checkNotEmpty() {
local var="$1"
if ! Assert::varExistsAndNotEmpty "${var}"; then
((++errorCount))
return 1
fi
}
checkVarAndDir() {
local var="$1"
local mode="${2:-}"
local status=0
if checkNotEmpty "${var}"; then
if [[ ! -d "${!var}" ]] && ! mkdir -p "${!var}"; then
Log::displayError "variable ${var} - impossible to create the directory '${!var}'"
((errorCount++))
return 1
fi
if [[ "${mode}" =~ w && ! -w "${!var}" ]]; then
Log::displayError "variable ${var} - directory '${!var}' is not writable"
((status++))
((errorCount++))
fi
if [[ "${mode}" =~ r && ! -r "${!var}" ]]; then
Log::displayError "variable ${var} - directory '${!var}' is not accessible"
((status++))
((errorCount++))
fi
fi
return "${status}"
}
checkValidValues() {
local var="$1"
shift || true
local -a validValues=("$@")
if ! Array::contains "${!var}" "${validValues[@]}"; then
Log::displayError "variable ${var} - value ${!var} is not part of the following values ${validValues[*]}"
((++errorCount))
fi
}
checkIsArray() {
local var="$1"
declare -p "${var}" 2>/dev/null | grep -q 'declare \-a'
}
if ! echo "${ID}" | grep -qEw 'debian|ubuntu'; then
Log::fatal "This script is built to support only Debian or Ubuntu distributions. You are using ${ID}."
fi
if checkNotEmpty USERNAME && ! getent passwd "${USERNAME}" 2>/dev/null >/dev/null; then
Log::displayError "USERNAME - user '${USERNAME}' does not exist"
((errorCount++))
fi
if [[ -n "${SSH_LOGIN:-}" ]] && ! Assert::ldapLogin "${SSH_LOGIN}"; then
Log::displayError "SSH_LOGIN - invalid ldap login (format expected firstNameLastName) in ${BASH_DEV_ENV_ROOT_DIR}/.env file"
((errorCount++))
fi
if checkNotEmpty "GIT_USERNAME" && ! Assert::firstNameLastName "${GIT_USERNAME}"; then
Log::displayError "GIT_USERNAME - invalid format, expected : firstName lastName"
((errorCount++))
fi
if checkNotEmpty "GIT_USER_MAIL" && ! Assert::emailAddress "${GIT_USER_MAIL}"; then
Log::displayError "GIT_USER_MAIL - invalid email address"
((errorCount++))
fi
if checkNotEmpty "AWS_USER_MAIL" && ! Assert::emailAddress "${AWS_USER_MAIL}"; then
Log::displayError "AWS_USER_MAIL - invalid email address"
((errorCount++))
fi
if ! checkIsArray "CONF_OVERRIDE_DIRS"; then
Log::displayError "CONF_OVERRIDE_DIRS - invalid format, expected : array of strings"
((errorCount++))
fi
((i = 0)) || true
local dir
for dir in "${CONF_OVERRIDE_DIRS[@]}"; do
if [[ ! -d "${dir}" ]]; then
Log::displayError "CONF_OVERRIDE_DIRS[${i}] - directory '${dir}' does not exist"
((errorCount++))
fi
if [[ ! -r "${dir}" ]]; then
Log::displayError "CONF_OVERRIDE_DIRS[${i}] - directory '${dir}' is not readable"
((errorCount++))
fi
((++i))
done
checkVarAndDir PROJECTS_DIR r || true
checkVarAndDir BACKUP_DIR rw || true
checkVarAndDir LOGS_DIR rw || true
checkVarAndDir INSTALL_SCRIPTS_ROOT_DIR r || true
checkVarAndDir HOME rw || true
checkValidValues UPGRADE_UBUNTU_VERSION 0 lts dev
checkValidValues PREFERRED_SHELL ShellBash ShellZsh
checkValidValues ZSH_PREFERRED_THEME ohmyposh powerlevel10k/powerlevel10k sindresorhus/pure starship/starship
checkValidValues SHOW_FORTUNES 0 1
checkValidValues SHOW_MOTD 0 1
checkValidValues OVERWRITE_CONFIG_FILES 0 1
checkValidValues CHANGE_WINDOWS_FILES 0 1
checkValidValues CAN_TALK_DURING_INSTALLATION 0 1
checkValidValues INSTALL_INTERACTIVE 0 1
checkNotEmpty WSLCONFIG_MAX_MEMORY
checkValidValues WSLCONFIG_SWAP 0 1
export CHECK_ENV=0
return "${errorCount}"
}
# @description if .env does not exist, initialize it with .env.template
Engine::Config::createEnvFileFromTemplate() {
local envFile="$1"
local envFileTemplate="$2"
if [[ ! -f "${envFile}" ]]; then
echo "${envFileTemplate}" >"${envFile}"
Log::displayError "a default env file has been created, please edit ${envFile}"
return 1
fi
}
# @description load .env file
# @arg $1 envFile:String the file to load
Engine::Config::loadConfig() {
if [[ "${BASH_DEV_ENV_CONFIG_LOADED:-0}" = "1" ]]; then
return 0
fi
Linux::requireTarCommand
Compiler::Embed::extractFileFromBase64 \
"${PERSISTENT_TMPDIR:-/tmp}/bac5e7091183d51fa83326113a7ff3ec8c81e56974d9b60002a1c5053a07e0d2/envFileTemplate" \
"IyEvYmluL2Jhc2gKIyBzaGVsbGNoZWNrIGRpc2FibGU9U0MyMDM0CgojIC0tLS0tLS0tLS0tLS0t \
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t \
LS0tLS0tCiMgR2VuZXJhbCBpbmZvcm1hdGlvbgojIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t \
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCgojIHdz \
bCB1c2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0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KIyBI \
YWNrcwojIG9uIHNvbWUgY29uZmlndXJhdGlvbnMgd2UgaGF2ZSB0byBmb3JjZSBwYXRoIG9mIHNv \
bWUgd2luZG93cyBjb21tYW5kcwojIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t \
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCgojUE9XRVJTSEVMTF9C \
SU49L21udC9jL1dpbmRvd3MvU3lzdGVtMzIvV2luZG93c1Bvd2VyU2hlbGwvdjEuMC9wb3dlcnNo \
ZWxsLmV4ZQojSVBDT05GSUdfQklOPS9tbnQvYy9XaW5kb3dzL3N5c3RlbTMyL2lwY29uZmlnLmV4 \
ZQojV1NMX0VYRV9CSU49L21udC9jL1dpbmRvd3Mvc3lzdGVtMzIvd3NsLmV4ZQoKIyAtLS0tLS0t \
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t \
LS0tLS0tLS0tLS0tLQojIEdlbmVyYWwgZGlzdHJpYnV0aW9uIGluZm9ybWF0aW9uCiMgLS0tLS0t \
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t \
LS0tLS0tLS0tLS0tLS0KCiMgaG9zdG5hbWUgb2YgdGhlIGRpc3RybwpESVNUUk9fSE9TVE5BTUU9 \
IndzbEhvc3QiCg==" \
"755"
declare -gx embed_file_envFileTemplate="${PERSISTENT_TMPDIR:-/tmp}/bac5e7091183d51fa83326113a7ff3ec8c81e56974d9b60002a1c5053a07e0d2/envFileTemplate"
local envFile="${BASH_DEV_ENV_ROOT_DIR}/.env"
# shellcheck disable=SC2154
Engine::Config::createEnvFileFromTemplate \
"${envFile}" "${embed_file_envFileTemplate}" || exit 1
set -o allexport
# shellcheck source=/.env.template
source <(echo "${embed_file_envFileTemplate}")
# shellcheck source=/.env
source "${BASH_DEV_ENV_ROOT_DIR}/.env"
set +o allexport
export STATS_DIR="${LOGS_DIR}/stats"
if [[ ! -d "${STATS_DIR}" ]]; then
mkdir -p "${STATS_DIR}" || true
fi
export LOGS_INSTALL_SCRIPTS_DIR="${LOGS_DIR}/installScripts"
if [[ ! -d "${LOGS_INSTALL_SCRIPTS_DIR}" ]]; then
mkdir -p "${LOGS_INSTALL_SCRIPTS_DIR}" || true
fi
# load environment variables ID, VERSION_CODENAME
Engine::Config::loadOsRelease
Engine::Config::loadUserVariables
if ! Engine::Config::checkEnv "${BASH_DEV_ENV_ROOT_DIR}/.env"; then
Log::displayError "one or more variables are invalid, check above logs and fix '${envFile}' file accordingly"
return 1
fi
Engine::Config::loadHostIp
Engine::Config::requireWslu
#Linux::Wsl::initEnv
Engine::Config::loadWslVariables
Log::requireLoad
Engine::Config::loadSshKey
Engine::Config::loadLocaleConfig
export BASH_DEV_ENV_CONFIG_LOADED=1
}
# @description deduce wsl host ip
# @set HOST_IP exported env containing the IP
Engine::Config::loadHostIp() {
HOST_IP="$(/sbin/ip route | awk '/default/ { print $3 }')"
export HOST_IP
}
# @description load locale configuration
Engine::Config::loadLocaleConfig() {
if [[ "${BASH_DEV_ENV_CONFIG_LOADED:-0}" = "1" ]]; then
return 0
fi
if [[ "${LOAD_LOCALE_CONFIG:-1}" = "1" && ! -f "${PERSISTENT_TMPDIR}/localeConfig.initialized" ]]; then
Log::displayInfo "Initializing locale en_US.UTF-8"
export PATH="${PATH}:${HOME}/.local/bin"
sudo sed -E -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
echo 'LANG="en_US.UTF-8"' | sudo tee /etc/default/locale >/dev/null
echo "LANG=en_US.UTF-8" | sudo tee /etc/locale.conf >/dev/null
sudo locale-gen en_US.UTF-8
sudo dpkg-reconfigure --frontend=noninteractive locales
export LC_ALL=C
export LANG=en_US.UTF-8
export LC_MESSAGES=en_US.UTF-8
touch "${PERSISTENT_TMPDIR}/localeConfig.initialized"
fi
}
# @description load /etc/os-release file
# @set NAME
# @set VERSION
# @set ID
# @set ID_LIKE
# @set PRETTY_NAME
# @set VERSION_ID
# @set HOME_URL
# @set SUPPORT_URL
# @set BUG_REPORT_URL
# @set PRIVACY_POLICY_URL
# @set VERSION_CODENAME
# @set UBUNTU_CODENAME
Engine::Config::loadOsRelease() {
if [[ ! -f /etc/os-release ]]; then
Log::displayError "file /etc/os-release does not exists"
return 1
fi
# This will load environment variables ID, VERSION_CODENAME, ...
set -o allexport
source /etc/os-release
set +o allexport
}
# @description load pageant and ssh key
# you can provide ssh key by env variable SSH_PRIVATE_KEY
# or if empty, file ~/.ssh/id_rsa will be used if present
# @env SSH_PRIVATE_KEY ssh key provided by env variable
# @env AUTHORIZE_SSH_KEY_USAGE if 0, no ssh key is loaded
# @env LOAD_SSH_KEY feature flag used in distro mode
# @env SKIP_REQUIRES ignore loading if set to 1
Engine::Config::loadSshKey() {
if [[ "${LOAD_SSH_KEY:-1}" = "0" || "${SKIP_REQUIRES:-0}" = "1" ]]; then
# ignore in distro mode
return 0
fi
if [[ "${AUTHORIZE_SSH_KEY_USAGE:-0}" = "0" ]]; then
Log::displaySkipped "Ssh key will not be loaded as you set AUTHORIZE_SSH_KEY_USAGE to 0"
return 0
fi
if [[ -n "${SSH_AUTH_SOCK}" && -n "${SSH_AGENT_PID}" ]]; then
Log::displaySkipped "Ssh agent skipped as variables SSH_AUTH_SOCK and SSH_AGENT_PID are set"
return 0
fi
if [[ -z "${SSH_PRIVATE_KEY}" && ! -f "${HOME}/.ssh/id_rsa" ]]; then
Log::displayError "File '${HOME}/.ssh/id_rsa' is missing and env variable SSH_PRIVATE_KEY is empty"
return 1
fi
local errorCode=0
ssh-add -l &>/dev/null || errorCode=$?
if [[ "${errorCode}" = "2" ]]; then
# ssh agent is not started
Log::displayInfo "Starting ssh agent"
eval "$(ssh-agent)" || return 2
export SSH_AUTH_SOCK
export SSH_AGENT_PID
fi
if [[ -n "${SSH_PRIVATE_KEY}" ]]; then
base64 -d <<<"${SSH_PRIVATE_KEY}" >"${HOME}/.ssh/id_rsa" || {
Log::displayError "Failed to decode SSH_PRIVATE_KEY"
return 2
}
chmod 600 "${HOME}/.ssh/id_rsa" || {
Log::displayError "Failed to set permissions on SSH key"
return 3
}
fi
ssh-keygen -f ~/.ssh/id_rsa -y >~/.ssh/id_rsa.pub || {
Log::displayError "Failed to generate public key from private key"
return 4
}
ssh-add "${HOME}/.ssh/id_rsa" || return 5
# Check key has been added to ssh agent
ssh-add -l &>/dev/null || {
Log::displayError "Your ssh key has not been loaded"
return 6
}
}
# @description deduce HOME, USER_ID, USERGROUP_ID and USERGROUP from USERNAME
# @env USERNAME String the name of the user
# @set USER_ID String
# @set USERGROUP String
# @set USERGROUP_ID String
# @set USER_SHELL String current user shell
# @set HOME String
# @env REMOTE String prefix command to run commands remotely
Engine::Config::loadUserVariables() {
# deduce user home and group
local -a split
local IFS=':'
# shellcheck disable=SC2207
split=($(${REMOTE:-} getent passwd "${USERNAME}"))
USER_ID="${split[2]}"
USERGROUP_ID="${split[3]}"
HOME="${split[5]}"
USER_SHELL="${split[6]}"
USERGROUP="$(${REMOTE:-} id -gn "${USERNAME}")"
if [[ -z "${USERGROUP}" || -z "${HOME}" ]]; then
Log::displayError "USERNAME - unable to deduce USERGROUP, HOME from USERNAME"
return 1
fi