-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·1614 lines (1355 loc) · 37.2 KB
/
deploy.sh
File metadata and controls
executable file
·1614 lines (1355 loc) · 37.2 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 sh
set -e
__BINNAME="$0"
__FILE="$(readlink -f -- "$0")"
__DIR="$(dirname -- "$__FILE")"
TARGET_FILE='target.sh'
G_DEPS="stow sops"
_G_DOC_PARAMS=''
# Current recipe and its variant on stacks.
_G_TARGET=''
_G_VARIANT=''
# XDG defaults
export XDG_DATA_HOME="${XDG_DATA_HOME:-"$HOME/.local/share"}"
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}"
export XDG_STATE_HOME="${XDG_STATE_HOME:-"$HOME/.local/state"}"
ESC=$(printf '\033')
BOLD="${ESC}[1m"
RESET="${ESC}[0m"
RED="${ESC}[31m"
CYAN="${ESC}[36m"
YELLOW="${ESC}[33m"
WHITE="${ESC}[37m"
GRAY="${ESC}[90m"
# HELPERS
# ------
pad_right() {
str=$1
width=$2
padchar=${3:-" "} # default to space if not provided
while [ "${#str}" -lt "$width" ]; do
str="$str$padchar"
done
printf "%s" "$str"
}
die() {
printf "%s%sError:%s %s%s%s\n" "$BOLD" "$RED" "$RESET" "$BOLD" "$WHITE" "$1" >&2
exit 1
}
notify_err() {
printf "%s%sError:%s %s%s%s%s\n" "$BOLD" "$RED" "$RESET" "$BOLD" "$WHITE" "$1" "$RESET" >&2
}
notify_step() {
printf "%s%s:: %s%s\n" "$BOLD" "$WHITE" "$*" "$RESET"
}
notify_info() {
printf "%s%sNOTE:%s %s\n" "$BOLD" "$CYAN" "$RESET" "$*"
}
notify_warn() {
printf "%s%sWARNING:%s %s%s%s%s\n" "$BOLD" "$YELLOW" "$RESET" "$BOLD" "$WHITE" "$*" "$RESET"
}
debug_log() {
if [ -n "$DEBUG" ]; then
printf "%s[dbg] %s%s\n" "$GRAY" "$*" "$RESET" >&2
fi
}
assert_in_target() {
if [ -z "$CURRENT_TARGET" ]; then
die 'should be inside a target'
fi
}
assert_command() {
if ! command -v "$1" >/dev/null 2>&1; then
die "$1 is not installed"
return
fi
}
assert_def() {
if [ -z "$1" ]; then
die "$2"
fi
}
command_exists() {
if ! command -v "$1" >/dev/null 2>&1; then
return 1
fi
return 0
}
__STACK_EMPTY_VAR='-'
# Push a value onto the stack
# @param $1 var_name
# @param $2 value
stack_push() {
stack_var="$1"
value="$2"
#HACK: shell skips empty entries. Use placeholder
if [ -z "$value" ]; then
value="$__STACK_EMPTY_VAR"
fi
eval "current_stack=\$$stack_var"
if [ -z "$current_stack" ]; then
eval "$stack_var=\"\$value\""
else
eval "$stack_var=\"\$current_stack,\$value\""
fi
}
# Peek at the last value without removing it
# @param $1 var_name
stack_peek() {
stack_var="$1"
eval "current_stack=\$$stack_var"
if [ -z "$current_stack" ] && [ "$(eval "echo \"\$$stack_var\"")" = "" ]; then
return
fi
# Save IFS and set to comma
old_ifs="$IFS"
IFS=','
# Get the last element
last=""
for item in $current_stack; do
last="$item"
done
IFS="$old_ifs"
if [ "$last" = "$__STACK_EMPTY_VAR" ]; then
last=''
fi
echo "$last"
}
# Pop the last value from the stack
# @param $1 var_name
# @param $2 is_ret
stack_pop() {
stack_var="$1"
is_ret="$2"
eval "current_stack=\$$stack_var"
if [ -z "$current_stack" ] && [ "$(eval "echo \"\$$stack_var\"")" = "" ]; then
return 1
fi
# Save IFS and set to comma
old_ifs="$IFS"
IFS=','
# Count elements and get last
count=0
last=""
for item in $current_stack; do
count=$((count + 1))
last="$item"
done
# Rebuild stack without last element
new_stack=""
i=0
for item in $current_stack; do
i=$((i + 1))
if [ $i -lt $count ]; then
if [ -z "$new_stack" ]; then
new_stack="$item"
else
new_stack="$new_stack,$item"
fi
fi
done
IFS="$old_ifs"
eval "$stack_var=\"\$new_stack\""
if [ -n "$is_ret" ] && [ "$last" != "$__STACK_EMPTY_VAR" ]; then
echo "$last"
fi
}
current_target() {
stack_peek '_G_TARGET'
}
current_variant() {
stack_peek '_G_VARIANT'
}
# Target Functions
# ----------------
#-- Stow wrappers --
link_dot() {
__private_stow 'link_dot' "$HOME" "$pkg" --dotfiles
}
link_home() {
dst="$HOME"
pkg="$1"
if [ -n "$2" ]; then
dst="$HOME/$1"
pkg="$2"
mkdir -p "$dst"
fi
__private_stow 'link_home' "$dst" "$pkg"
}
link_local_bin() {
bindir="$HOME/.local/bin"
mkdir -p "$bindir"
__private_stow 'link_local_bin' "$bindir" "$1"
}
link_xdg_config() {
__private_stow 'link_xdg_config' "$XDG_CONFIG_HOME" "$1"
}
# link_xdg_data - stow a directory to "$XDG_DATA_HOME" (default: ~/.local/share)
# Usage:
# link_xdg_data <src_dir> - Link files in a dir to $XDG_DATA_HOME
# link_xdg_data <dst_dir> <src_dir> - Link files in a dir to $XDG_DATA_HOME/dst_dir
link_xdg_data() {
src_dir="$1"
bind_dir="$XDG_DATA_HOME"
if [ -n "$2" ]; then
bind_dir="$bind_dir/$1"
src_dir="$2"
fi
__private_stow 'link_xdg_data' "$bind_dir" "$src_dir"
}
link_xdg_state() {
__private_stow 'link_xdg_state' "$XDG_STATE_HOME" "$1"
}
# @param $1 operation name
# @param $2 dest path
# @param $3 target package (target/package)
# @param $4 raw stow opts
__private_stow() {
assert_in_target
assert_def "$2" "$1: missing dir name"
assert_def "$3" "$1: missing package name"
dst="$2"
pkg="$3"
if [ -n "$G_DRY_RUN" ]; then
return
fi
if [ -n "$G_REVERT" ]; then
notify_step "Removing '$CURRENT_TARGET/$pkg' symlinks..."
__private_stow_unlink "$1" "$dst" "$pkg"
return
fi
notify_step "Symlink item from '$CURRENT_TARGET/$pkg' to '$dst' ..."
__private_stow_link "$1" "$dst" "$pkg"
}
# @param $1 operation name
# @param $2 dest path
# @param $3 target package (target/package)
# @param $4 raw stow opts
__private_stow_link() {
if [ -n "$G_DRY_RUN" ]; then
return
fi
assert_command stow
mkdir -p "$2"
recipes_dir="$__DIR/$CURRENT_TARGET"
cmd="stow -v -t '$2' -d '$recipes_dir' '$3' $4"
echo "$cmd"
sh -c "$cmd"
}
# @param $1 operation name
# @param $2 dest path
# @param $3 target package (target/package)
# @param $4 raw stow opts
__private_stow_unlink() {
if [ -n "$G_DRY_RUN" ]; then
return
fi
assert_command stow
if [ ! -d "$1" ]; then
return
fi
recipes_dir="$CURRENT_TARGET"
if [ ! -d "$recipes_dir/$3" ]; then
die "$1: directory '$recipes_dir/$3' doesn't exist"
fi
stow -v -D -t "$2" -d "$recipes_dir" "$3" "$4"
}
# -- Package managers --
__private_assert_archlinux() {
case "$G_DISTRO" in
'arch' | 'archarm') ;;
*)
die "'$1' action is supported only when running inside ArchLinux systems."
;;
esac
}
# brewfile - Install brew packages from file
brewfile() {
assert_in_target
assert_def "$1" "brewfile: missing package name"
fp="$__DIR/$CURRENT_TARGET/$1"
if [ ! -f "$fp" ]; then
die "brewfile: cannot read packages file '$fp'"
fi
if ! command_exists 'brew'; then
die 'Homebrew is not installed'
fi
if [ -n "$G_DRY_RUN" ]; then
return
fi
if [ -n "$G_REVERT" ]; then
notify_info "brewfile: revert not supported this action"
return
fi
brew bundle --file="$fp"
}
# __private_read_pkglist - Read packages list file
# Args:
# $1 - File path
__private_read_pkglist() {
grep -v '^[[:space:]]*#' "$1" | # remove comment lines
grep -v '^[[:space:]]*$' | # remove empty lines
tr '\n' ' ' | sed 's/[[:space:]]\+$//' # join into single line, trim trailing space
}
# aptfile - Install packages from a file using apt package manager.
#
# Note: on Termux, `pkg` tool is used.
aptfile() {
assert_in_target
assert_def "$1" "aptfile: missing package list file"
if [ -n "$G_DRY_RUN" ]; then
return
fi
# On Termux (Android): Run rootless 'pkg' wrapper
# On real Linux: Run classic 'sudo apt'.
pkgmgr='apt'
verb='sudo'
if [ "$G_DISTRO" = 'android' ]; then
pkgmgr='pkg'
verb=''
fi
if ! command_exists "$pkgmgr"; then
die 'aptfile: APT package manager not available. Are you running in Debian-based distro?'
fi
fp="$__DIR/$CURRENT_TARGET/$1"
if [ ! -f "$fp" ]; then
die "aptfile: cannot read packages file '$fp'"
fi
if [ -n "$G_REVERT" ]; then
notify_info 'aptfile: revert not supported this action'
return
fi
debug_log "aptfile: read $fp"
packages=$(__private_read_pkglist "$fp")
debug_log "aptfile: pkgs - $packages"
count=0
missing=""
for pkg in $packages; do
if ! dpkg -s "$pkg" >/dev/null 2>&1; then
missing="$missing $pkg"
count=$((count + 1))
fi
done
if [ -z "$missing" ]; then
notify_info 'aptfile: no packages to install, skip'
return
fi
notify_step "Installing $count package(s) using '$pkgmgr'..."
install_cmd="$pkgmgr install -y --no-upgrade $missing"
if [ -n "$verb" ]; then
install_cmd="$verb $install_cmd"
fi
debug_log "aptfile: cmd - $install_cmd"
if ! eval "$install_cmd"; then
die "Failed to install packages using '$pkgmgr'"
fi
}
# aurpkg - Clone and install Arch package from AUR
# Used to install aur helper, such as yay or paru.
#
# Note: use aurfile to install packages from AUR using a helper.
#
# Support: works only in Arch Linux, and other pacman-based distros.
AUR_URL_TEMPLATE="${AUR_URL_TEMPLATE:-https://aur.archlinux.org/%s.git}"
aurpkg() {
assert_in_target
assert_def "$1" "aurpkg: missing package name"
__private_assert_archlinux 'aurpkg'
if [ -n "$G_DRY_RUN" ]; then
return
fi
if [ -n "$G_REVERT" ]; then
notify_info "aurpkg: revert not supported this action"
return
fi
not_installed=$(pacman -T -- "$@" || :)
if [ -z "$not_installed" ]; then
notify_info 'aurpkg: packages already installed, skip.'
return
fi
if ! command_exists 'git'; then
notify_info 'git is not installed, installing...'
sudo pacman -S git
fi
oldwd="$PWD"
debug_log "aurpkg: not installed: '$(printf "$not_installed" | tr '\n' ', ')'"
for pkg in $not_installed; do
notify_step "Downloading package '$pkg' from AUR..."
clonedir="$(mktemp -d)"
aururl=$(printf "$AUR_URL_TEMPLATE" "$pkg")
debug_log "aurpkg: clone '$aururl' into '$clonedir'"
if ! git clone "$aururl" "$clonedir"; then
die "failed to clone '$aururl'"
fi
if [ ! -f "$clonedir/PKGBUILD" ]; then
cd "$oldwd"
rm -rf "$clonedir"
die "package '$pkg' doesn't exist on AUR (PKGBUILD not found)"
fi
notify_step "Building package '$pkg'..."
cd "$clonedir"
if ! makepkg -si; then
cd "$oldwd"
rm -rf "$clonedir"
die "failed to install AUR package '$pkg'"
fi
cd "$oldwd"
rm -rf "$clonedir"
unset clonedir
done
}
# pacmanfile - Install Arch packages from a text file
#
# Support: works only in Arch Linux, and other pacman-based distros.
pacmanfile() {
assert_in_target
assert_def "$1" "pacmanfile: missing package list file"
__private_assert_archlinux 'pacmanfile'
fp="$__DIR/$CURRENT_TARGET/$1"
if [ ! -f "$fp" ]; then
die "pacmanfile: cannot read Pacman packages file '$fp'"
fi
if [ -n "$G_DRY_RUN" ]; then
return
fi
if [ -n "$G_REVERT" ]; then
notify_info "pacmanfile: revert not supported this action"
return
fi
debug_log "pacmanfile: read $fp"
packages=$(__private_read_pkglist "$fp")
pkgcount=$(printf '%s\n' "$packages" | tr ' ' '\n' | grep -v '^$' | wc -l)
debug_log "pacmanfile: pkgs - $packages"
if [ "$pkgcount" -le 0 ]; then
notify_info "pacmanfile: empty packages list, skip."
return
fi
verb='package'
if [ "$pkgcount" -gt 1 ]; then
verb="${verb}s"
fi
notify_step "Installing $pkgcount pacman $verb from '$fp'..."
sudo pacman -S $packages --needed --noconfirm || {
die 'failed to install packages, command returned an error'
}
}
_G_AUR_HELPER=''
_G_AUR_HELPERS='yay paru yaourt'
__private_init_aurhelper() {
if [ -n "$_G_AUR_HELPER" ]; then
debug_log "aur: using AUR helper '$pkg'"
return
fi
for pkg in $_G_AUR_HELPERS; do
if command_exists "$pkg"; then
debug_log "aur: detected AUR helper '$pkg'"
_G_AUR_HELPER="$pkg"
return
fi
done
die "Failed to find AUR helper. Install one of following helpers using 'aurpkg' directive: $_G_AUR_HELPERS"
}
# aurfile - Install AUR packages from a file.
#
# @param $1 text file with list of packages.
#
# Note: this requires a working aur helper to be installed.
# Use `aurpkg` to install AUR helper (e.g.: `aurpkg yay`).
#
# Support: works only in Arch Linux, and other pacman-based distros.
aurfile() {
assert_in_target
assert_def "$1" "aurfile: missing package list file"
__private_assert_archlinux 'aurfile'
__private_init_aurhelper
fp="$__DIR/$CURRENT_TARGET/$1"
if [ ! -f "$fp" ]; then
die "aurfile: cannot read packages file '$fp'"
fi
if [ -n "$G_DRY_RUN" ]; then
return
fi
if [ -n "$G_REVERT" ]; then
notify_info "aurfile: revert not supported this action"
return
fi
debug_log "aurfile: read $fp"
packages=$(__private_read_pkglist "$fp")
pkgcount=$(printf '%s\n' "$packages" | tr ' ' '\n' | grep -v '^$' | wc -l)
debug_log "aurfile: pkgs - $packages"
if [ "$pkgcount" -le 0 ]; then
notify_info "aurfile: empty packages list, skip."
return
fi
verb='package'
if [ "$pkgcount" -gt 1 ]; then
verb="${verb}s"
fi
notify_step "Installing $pkgcount AUR $verb from '$fp'..."
"$_G_AUR_HELPER" -S $packages --needed --noconfirm || {
die 'failed to install packages, command returned an error'
}
}
# -- SOPS --
# Decrypts SOPS encrypted file.
#
# Second parameter is destination filename.
#
# Usage:
# sops_decrypt 'filename.enc' 'dest'
# sops_decrypt 'filename.enc' 'dest'
sops_decrypt() {
assert_def "$1" 'source file name is required'
assert_def "$2" 'destination file path is required'
if [ -n "$G_DRY_RUN" ]; then
return
fi
if [ -n "$G_REVERT" ]; then
notify_step "Removing encrypted file '$2'..."
rm -f "$2"
return
fi
src_file="$__DIR/$CURRENT_TARGET/$1"
notify_step "Decrypting '$1' -> '$2'..."
if [ ! -f "$src_file" ]; then
die "sops_decrypt: file '$src_file' doesn't exist"
fi
parentdir="$(dirname "$2")"
if [ ! -d "$parentdir" ]; then
mkdir -p -v "$parentdir"
fi
rm -f "$2"
GPG_TTY=$(tty) sops --decrypt "$src_file" >"$2"
}
# Appends a line to a file if line doesn't exist.
#
# Usage:
# file_append_once 'filename' 'some line'
file_append_once() {
assert_def "$1" 'file path is required'
assert_def "$2" 'missing contents'
if [ -n "$G_DRY_RUN" ]; then
return
fi
if [ -n "$G_REVERT" ]; then
# TODO: implement revert
notify_warn 'file_append_once: revert is not implemented yet'
return
fi
notify_step "Updating file '$1'..."
parentdir="$(dirname "$1")"
if [ ! -d "$parentdir" ]; then
mkdir -p -v "$parentdir"
fi
if grep -q "$ssh_include" ~/.ssh/config; then
notify_info 'File is already updated, skip'
return
fi
echo "$2" >>"$1"
}
# Adds an include into SSH config file ($HOME/.ssh/config).
#
# Usage:
# ssh_config_include path
ssh_config_include() {
assert_def "$1" 'file path is required'
if [ -n "$G_DRY_RUN" ]; then
return
fi
# Replace $HOME to have conflg flexible
rel_include_path="$(printf '%s\n' "$1" | sed "s|^$HOME|~|")"
ssh_include="Include \"$rel_include_path\""
if [ -n "$G_REVERT" ]; then
# TODO: implement revert
notify_warn 'ssh_config_include: revert is not implemented yet'
return
fi
file_append_once "$HOME/.ssh/config" "$ssh_include"
}
# -- Core --
# @param $1 target
require() {
assert_in_target
if [ "$#" -eq 0 ]; then
die 'require: missing target'
fi
while [ $# -gt 0 ]; do
target_name="$(__private_parse_target_name "$1")"
target_variant="$(__private_parse_target_variant "$1")"
target_file="$__DIR/$target_name/$TARGET_FILE"
shift
if [ ! -f "$target_file" ]; then
die "require: '$target_file' doesn't exist"
return
fi
if [ -n "$G_DRY_RUN" ]; then
_DOC_EXTENDS="$_DOC_EXTENDS $target_name"
return
fi
notify_step "Processing parent target '$target_name' ..."
__private_eval_target "$target_name" "$target_variant" 1
done
}
# param - declare a recipe input parameter
# @param $1 - Paramerer
# @param $* - Options in format 'key:value'
param() {
assert_in_target
assert_def "$1" 'param: missing name'
param_name="$1"
shift
descr="Set $param_name parameter"
doc_validate=''
param_required=''
param_defaults=''
debug_log "param: parse $param_name"
for param in "$@"; do
key="${param%%:*}"
val="${param#*:}"
debug_log "param: Name=$param_name; Opt='$key'; Val='$val';"
case "$key" in
description)
descr="$val"
;;
required)
param_required='1'
;;
validate)
doc_validate="$val"
;;
default)
param_defaults="$val"
;;
*)
die "param: unsupported option - $key"
;;
esac
done
if [ -n "$param_defaults" ] && [ -n "$param_required" ]; then
notify_warn "param '$param_name' - option 'required' has no effect when default value is defined."
fi
# TODO: dedup
if [ -n "$G_DRY_RUN" ]; then
# Just assembly doc
if [ -n "$doc_validate" ]; then
descr="$descr ($doc_validate)"
fi
if [ -n "$param_defaults" ]; then
descr="$descr (default: '$param_defaults')"
fi
if [ -n "$param_required" ]; then
descr="$descr (required)"
fi
if [ -n "$_G_DOC_PARAMS" ]; then
_G_DOC_PARAMS="$_G_DOC_PARAMS;"
fi
_G_DOC_PARAMS="${_G_DOC_PARAMS}${param_name}=${descr}"
_G_TARGET_EXPECT_FLAGS="$_G_TARGET_EXPECT_FLAGS $param_name"
return
fi
# If inside target - check if var is supplied
# Check if there are validation requirements
param_value=$(__get_flag_val "$param_name")
if [ -n "$param_required" ]; then
assert_def "$param_value" "flag '--$param_name' is required. See '$__BINNAME info $CURRENT_TARGET' for documentation."
fi
__private_validate_oneof "$param_name" "$param_value" "$doc_validate"
if [ -n "$doc_oneof" ]; then
eval "__G_PARAM_SPEC_${param_name}_ONEOF='$doc_oneof'"
fi
if [ -n "$param_required" ]; then
eval "__G_PARAM_SPEC_${param_name}_REQUIRED='$doc_oneof'"
fi
}
# get_param - return value of parameter declared using 'param' keyword.
# @param $1 name
get_param() {
assert_def "$1" 'get_param: missing param name'
__get_flag_val "$1"
}
# has_param - checks whetner parameter is defined.
# @param $1 name
has_param() {
assert_def "$1" 'has_param: missing param name'
echo "has_param: $1 V=$(__get_flag_val "$1")"
if [ -n "$(__get_flag_val "$1")" ]; then
return 0
fi
return 1
}
# @param $1 key
# @param $2 value
# @param $3 rule
__private_validate_oneof() {
if [ -z "$3" ]; then
debug_log "param.validate: no rules for param '$1'"
return
fi
debug_log "param.validate: TEST '$1' Val='$2' Rule='$3'"
if echo "$2" | grep -qE "^($3)$"; then
return
fi
debug_log "param.validate: OK '$1'"
die "invalid value of flag '--$1': value '$2' doesn't match rule '$3'"
}
# step - Run function as a part of target process.
# @param $1 func_name
# @param $2 optional, flag or var name
step() {
assert_in_target
assert_def "$1" 'step: missing function name'
func_name="$1"
binding="$2"
if [ -n "$binding" ]; then
__private_step_with_flag "$func_name" "$binding"
return
fi
if [ -n "$G_DRY_RUN" ]; then
return
fi
if [ -n "$G_REVERT" ]; then
rollback_func="$1_rollback"
if ! command -v "$rollback_func" >/dev/null 2>&1; then
notify_warn "missing rollback function for $func_name. Implement $rollback_func to support rollback."
return
fi
notify_step "Rollback step '$func_name'..."
func_name="$rollback_func"
else
notify_step "Running step '$func_name' ..."
fi
"$func_name" "$CURRENT_TARGET"
}
__private_step_with_flag() {
assert_in_target
func_name="$1"
bind_opt="$(printf '%s' "$2" | cut -d':' -f1)"
bind_val="$(printf '%s' "$2" | cut -d':' -f2)"
assert_def "$func_name" 'step: missing func name'
assert_def "$bind_opt" 'step: missing binding option'
assert_def "$bind_val" 'step: missing binding value'
if [ "$bind_opt" != 'flag' ]; then
die "step: unsupported step binding: $bind_opt (at '$2')"
fi
if [ -n "$G_DRY_RUN" ]; then
# Just assembly doc
_DOC_FLAGS="$_DOC_FLAGS $bind_val:$func_name"
_TARGET_EXPECT_FLAGS="$_TARGET_EXPECT_FLAGS $bind_val"
return
fi
debug_log "condition: $func_name - flag:'$bind_val'"
if ! __is_flag_defined "$bind_val" && ! __is_flag_defined 'all'; then
notify_info "step '$func_name' skipped as $bind_opt --$bind_val is missing"
return
fi
if [ -n "$G_REVERT" ]; then
rollback_func="${func_name}_rollback"
if ! command -v "$rollback_func" >/dev/null 2>&1; then
notify_warn "missing rollback function for $func_name. Implement $rollback_func to support rollback."
return
fi
notify_step "Rollback step '$func_name'..."
func_name="$rollback_func"
else
notify_step "Running step '$func_name' ..."
fi
"$func_name" "$flag_value"
unset flag_value
}
__EXPAND_TPL_AWK_SCRIPT=$(
cat <<EOF
BEGIN { %s }
{
for (k in v)
gsub("{{" k "}}", v[k])
print
}
EOF
)
# expand_template - Takes input template file and processes into output file.
# Accepts key-value pair of variables to expand in a template.
#
# Example:
# expand_template 'config.tpl' "$XDG_CONFIG_HOME/config.txt" 'foo=bar' 'bar=baz'
#
# @param $1 source_file
# @param $2 target_file
# @param $@ key-value pair of variables and values.
expand_template() {
assert_in_target
assert_def "$1" 'expand_template: missing template file name'
assert_def "$2" 'expand_template: missing destination file name'
tpl_file="$__DIR/$CURRENT_TARGET/$1"
dst_file="$2"
shift 2
if [ ! -f "$tpl_file" ]; then
die "expand_template: source template file doesn't exist - '$tpl_file'"
fi
if [ -n "$G_DRY_RUN" ]; then
return
fi
varlist=''
for kv in "$@"; do
case "$kv" in
*=*)
k="${kv%%=*}"
v="${kv#*=}"
v=$(printf '%s\n' "$v" | sed 's/\\/\\\\/g; s/"/\\"/g')
varlist="$(printf '%s\nv["%s"]="%s"' "$varlist" "$k" "$v")"
;;
*)
die "expand_template: invalid key-value pair '$kv'"
;;
esac
done
if [ -n "$G_REVERT" ]; then
notify_step "Removing generated file '$dst_file'..."
rm -f "$dst_file"
return
fi
notify_step "Generating file '$dst_file'..."
awk_script="$(printf "$__EXPAND_TPL_AWK_SCRIPT" "$varlist")"
debug_log "expand_template: Src='$tpl_file' Dst='$dst_file'"
debug_log "$awk_script"
awk "$awk_script" "$tpl_file" >"$dst_file"
}
# Private Functions
# -----------------
# Installs prerequisites
__private_install_deps() {
if [ -n "$G_DRY_RUN" ]; then
return
fi
install_deps=""