-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.sh
More file actions
executable file
·1424 lines (1196 loc) · 37.3 KB
/
boot.sh
File metadata and controls
executable file
·1424 lines (1196 loc) · 37.3 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
#!/bin/bash
set -euo pipefail
# bootstrap a macOS machine by delegating to the hosted bootbox entrypoint.
#
# examples:
#
# $ ./boot.sh --op-token "$OP_TOKEN"
# $ ./boot.sh --op-token "$OP_TOKEN" --ssh-key vmruk4ny353aly6tbom7z3v2hy/id_botbox1
# $ ./boot.sh --op-token "$OP_TOKEN" --me v0.3.1
# $ ./boot.sh --op-token "$OP_TOKEN" --tanaab v0.2.0
# $ DEBUG=1 ./boot.sh --op-token "$OP_TOKEN" --yes
#
# option precedence: cli options override environment variables, which override defaults.
#
# run `./boot.sh --help` for more advanced usage.
MACOS_OLDEST_SUPPORTED="26.0"
REQUIRED_CURL_VERSION="7.41.0"
BOOTBOX_URL="https://bootbox.tanaab.sh/bootbox.sh"
DEFAULT_SSH_KEY="vmruk4ny353aly6tbom7z3v2hy/id_pirog,vmruk4ny353aly6tbom7z3v2hy/id_botbox1"
DEFAULT_ME_SOURCE="ssh"
DEFAULT_TANAAB_SOURCE="ssh"
ME_REPO_SSH_URL="git@github.com:pirog/me.git"
ME_REPO_RELEASE_BASE_URL="https://github.com/pirog/me/releases/download"
ME_REPO_RELEASE_ARCHIVE_PREFIX="piroplugin"
TANAAB_REPO_SSH_URL="git@github.com:tanaabased/canon.git"
TANAAB_REPO_RELEASE_BASE_URL="https://github.com/tanaabased/canon/releases/download"
TANAAB_REPO_RELEASE_ARCHIVE_PREFIX="tanaab"
TANAAB_PLUGIN_RELATIVE_TARGET="../../../../../canon"
abort() {
printf "%serror%s: %s\n" "${tty_red-}" "${tty_reset-}" "$@" >&2
exit 1
}
abort_multi() {
while read -r line; do
printf "%serror%s: %s\n" "${tty_red-}" "${tty_reset-}" "${line}" >&2
done <<< "$@"
exit 1
}
value_enabled() {
case "${1:-}" in
'' | 0 | false | FALSE | False | no | NO | No | off | OFF | Off)
return 1
;;
*)
return 0
;;
esac
}
source_value_disabled() {
case "${1:-}" in
0 | false | FALSE | False | no | NO | No | off | OFF | Off | null | NULL | Null)
return 0
;;
*)
return 1
;;
esac
}
mask_secret_for_display() {
local value="$1"
local length="${#value}"
local prefix_length="4"
local suffix_length="4"
local suffix_start
if [[ -z "${value}" ]]; then
printf "none"
return 0
fi
if [[ "${length}" -le 4 ]]; then
printf "****"
return 0
fi
if [[ "${length}" -le 8 ]]; then
prefix_length="2"
suffix_length="2"
fi
suffix_start=$((length - suffix_length))
printf "%s...%s" "${value:0:${prefix_length}}" "${value:${suffix_start}:${suffix_length}}"
}
trim_whitespace() {
local value="$1"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
printf "%s" "${value}"
}
append_array_value() {
local array_name="$1"
local value
local quoted
value="$(trim_whitespace "$2")"
if [[ -n "${value}" ]]; then
printf -v quoted '%q' "${value}"
eval "${array_name}+=(${quoted})"
fi
}
append_csv_to_array() {
local array_name="$1"
local old_ifs="${IFS}"
local entry
local -a values=()
if [[ -z "${2}" ]]; then
return 0
fi
IFS=','
read -r -a values <<< "${2}"
IFS="${old_ifs}"
if [[ "${#values[@]}" -eq 0 ]]; then
return 0
fi
for entry in "${values[@]}"; do
append_array_value "${array_name}" "${entry}"
done
}
array_join() {
local delimiter="$1"
local array_name="$2"
local item
local first="1"
local value_count="0"
local -a values=()
eval "value_count=\${#${array_name}[@]}"
if [[ "${value_count}" -eq 0 ]]; then
return 0
fi
eval "values=(\"\${${array_name}[@]}\")"
for item in "${values[@]}"; do
if [[ "${first}" == "1" ]]; then
printf "%s" "${item}"
first="0"
else
printf "%s%s" "${delimiter}" "${item}"
fi
done
}
chomp() {
printf "%s" "${1/"$'\n'"/}"
}
shell_join() {
local arg
printf "%s" "${1:-}"
if [[ $# -eq 0 ]]; then
return 0
fi
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
# shellcheck disable=SC2292
if [ -z "${BASH_VERSION:-}" ]; then
abort "bash is required to interpret this script."
fi
if [[ -n "${CI-}" && -n "${INTERACTIVE-}" ]]; then
abort "cannot run force-interactive mode in CI."
fi
# shellcheck disable=SC2016
if [[ -n "${INTERACTIVE-}" && -n "${NONINTERACTIVE-}" ]]; then
abort 'both `$INTERACTIVE` and `$NONINTERACTIVE` are set. please unset at least one variable and try again.'
fi
if [[ -n "${POSIXLY_CORRECT+1}" ]]; then
abort 'bash must not run in POSIX mode. please unset POSIXLY_CORRECT and try again.'
fi
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_mkdim() { tty_escape "2;$1"; }
tty_bold="$(tty_mkbold 39)"
tty_dim="$(tty_mkdim 39)"
# shellcheck disable=SC2034 # keep the shared palette available even when a given change doesn't use green directly
tty_green="$(tty_escape 32)"
tty_magenta="$(tty_escape 35)"
tty_red="$(tty_mkbold 31)"
tty_reset="$(tty_escape 0)"
tty_underline="$(tty_escape "4;39")"
tty_yellow="$(tty_escape 33)"
tty_tp="$(tty_escape '38;2;0;200;138')" # #00c88a
# shellcheck disable=SC2034 # reserved for future plan/action styling
tty_ts="$(tty_escape '38;2;219;39;119')" # #db2777
SCRIPT_NAME="${0##*/}"
# Keep a single top-level assignment so release automation can stamp the entrypoint in place.
SCRIPT_VERSION="${SCRIPT_VERSION:-$(git describe --tags --always --abbrev=1 2>/dev/null || printf '%s' '0.0.0-unreleased')}"
DEBUG="${PIROME_DEBUG:-${DEBUG:-${RUNNER_DEBUG:-}}}"
FORCE="${PIROME_FORCE:-}"
OP_TOKEN="${PIROME_OP_TOKEN:-${OP_SERVICE_ACCOUNT_TOKEN:-}}"
SSH_KEYS_CSV="${PIROME_SSH_KEY:-${DEFAULT_SSH_KEY}}"
ME_SOURCE="${PIROME_ME:-${DEFAULT_ME_SOURCE}}"
TANAAB_SOURCE="${PIROME_TANAAB:-${DEFAULT_TANAAB_SOURCE}}"
declare -a ORIGINAL_ARGS=("$@")
declare -a SSH_KEYS=()
declare -a SSH_KEYS_TO_INSTALL=()
declare -a SSH_KEYS_TO_OVERWRITE=()
declare -a SSH_KEYS_TO_SKIP=()
declare -a ME_APPLY_DOTPKGS=()
declare -a PLANNED_ACTIONS=()
BOOT_TMPDIR=""
BOOTBOX_SCRIPT_PATH=""
CORE_NEEDS_REMEDIATION="0"
CURL=""
DETECTED_ARCH=""
DETECTED_OS=""
ARCH=""
OS=""
ME_SOURCE_KIND=""
ME_SOURCE_LOCAL_PATH=""
ME_SOURCE_VERSION_TAG=""
ME_TARGET_PATH=""
TANAAB_SOURCE_KIND=""
TANAAB_SOURCE_LOCAL_PATH=""
TANAAB_SOURCE_VERSION_TAG=""
TANAAB_TARGET_PATH=""
ME_APPLY_BREWFILE=""
if [[ -n "${PIROME_SSH_KEYS:-}" ]]; then
SSH_KEYS_CSV="${SSH_KEYS_CSV}${SSH_KEYS_CSV:+,}${PIROME_SSH_KEYS}"
fi
append_csv_to_array SSH_KEYS "${SSH_KEYS_CSV}"
if [[ "${#ORIGINAL_ARGS[@]}" -gt 0 ]]; then
for arg in "${ORIGINAL_ARGS[@]}"; do
case "${arg}" in
--ssh-key | --ssh-key=* | --ssh-keys | --ssh-keys=*)
SSH_KEYS=()
break
;;
esac
done
fi
debug_enabled() {
value_enabled "${DEBUG:-}"
}
force_enabled() {
value_enabled "${FORCE:-}"
}
debug() {
if debug_enabled; then
printf "${tty_dim}debug${tty_reset} %s\n" "$(shell_join "$@")" >&2
fi
}
log() {
printf "%s\n" "$(shell_join "$@")"
}
warn() {
printf "${tty_yellow}warn${tty_reset}: %s\n" "$(chomp "$@")" >&2
}
show_version() {
printf "%s\n" "${SCRIPT_VERSION}"
exit 0
}
is_semver_value() {
[[ "${1:-}" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$ ]]
}
normalize_release_tag() {
if [[ "${1}" == v* ]]; then
printf "%s" "${1}"
else
printf "v%s" "${1}"
fi
}
normalize_repo_source_value() {
if is_semver_value "${1}"; then
normalize_release_tag "${1}"
else
printf "%s" "${1}"
fi
}
usage() {
local debug_display="off"
local force_display="off"
local ssh_keys_display="none"
local op_token_display="none"
local me_display="none"
local tanaab_display="none"
if debug_enabled; then
debug_display="on"
fi
if force_enabled; then
force_display="on"
fi
ssh_keys_display="$(array_join "," SSH_KEYS)"
ssh_keys_display="${ssh_keys_display:-none}"
if [[ -n "${OP_TOKEN:-}" ]]; then
op_token_display="$(mask_secret_for_display "${OP_TOKEN}")"
fi
me_display="$(normalize_repo_source_value "${ME_SOURCE}")"
tanaab_display="$(normalize_repo_source_value "${TANAAB_SOURCE}")"
cat <<EOS
Usage: ${tty_dim}[NONINTERACTIVE=1] [CI=1]${tty_reset} ${tty_bold}${SCRIPT_NAME}${tty_reset} ${tty_dim}[options]${tty_reset}
${tty_tp}Options:${tty_reset}
--ssh-key installs 1password ssh keys as vault/item[:filename] ${tty_dim}[default: ${ssh_keys_display}]${tty_reset}
--op-token auths with 1password service account token ${tty_dim}[default: ${op_token_display}]${tty_reset}
--me fetches me from ssh, a local git repo path, or a release version ${tty_dim}[default: ${me_display}]${tty_reset}
--tanaab fetches tanaab from ssh, a local git repo path, a release version, or a falsey disable value ${tty_dim}[default: ${tanaab_display}]${tty_reset}
--version shows version of this script
--debug shows debug messages ${tty_dim}[default: ${debug_display}]${tty_reset}
--force forces supported bootbox operations ${tty_dim}[default: ${force_display}]${tty_reset}
-h, --help displays this help message
-y, --yes runs with all defaults and no prompts, sets NONINTERACTIVE=1
${tty_tp}Environment Variables:${tty_reset}
PIROME_SSH_KEY comma-separated list of 1password ssh keys as vault/item[:filename]
PIROME_OP_TOKEN 1password service account token; falls back to OP_SERVICE_ACCOUNT_TOKEN
PIROME_ME source for ~/tanaab/me; supports ssh, local repo paths, or release versions
PIROME_TANAAB source for ~/tanaab/canon; supports ssh, local repo paths, release versions, or falsey disable values
PIROME_FORCE set to a truthy value to force supported operations
PIROME_DEBUG set to a truthy value to show debug messages
NONINTERACTIVE installs without prompting for user input
CI installs in CI mode (e.g. does not prompt for user input)
EOS
if [[ "${1:-0}" != "noexit" ]]; then
exit "${1:-0}"
fi
}
abort_option_usage() {
usage "noexit"
abort "$1"
}
require_next_option_value() {
local option="$1"
local argc="$2"
if [[ "${argc}" -lt 2 ]]; then
abort_option_usage "option ${tty_bold}${option}${tty_reset} requires a value."
fi
}
require_inline_option_value() {
local option="$1"
local value="$2"
if [[ -z "${value}" ]]; then
abort_option_usage "option ${tty_bold}${option}${tty_reset} must not be empty."
fi
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--ssh-key)
require_next_option_value "--ssh-key" "$#"
append_array_value SSH_KEYS "$2"
shift 2
;;
--ssh-key=*)
require_inline_option_value "--ssh-key" "${1#*=}"
append_array_value SSH_KEYS "${1#*=}"
shift
;;
--ssh-keys)
require_next_option_value "--ssh-keys" "$#"
append_csv_to_array SSH_KEYS "$2"
shift 2
;;
--ssh-keys=*)
require_inline_option_value "--ssh-keys" "${1#*=}"
append_csv_to_array SSH_KEYS "${1#*=}"
shift
;;
--op-token)
require_next_option_value "--op-token" "$#"
OP_TOKEN="$2"
shift 2
;;
--op-token=*)
require_inline_option_value "--op-token" "${1#*=}"
OP_TOKEN="${1#*=}"
shift
;;
--me)
require_next_option_value "--me" "$#"
ME_SOURCE="$2"
shift 2
;;
--me=*)
require_inline_option_value "--me" "${1#*=}"
ME_SOURCE="${1#*=}"
shift
;;
--tanaab)
require_next_option_value "--tanaab" "$#"
TANAAB_SOURCE="$2"
shift 2
;;
--tanaab=*)
require_inline_option_value "--tanaab" "${1#*=}"
TANAAB_SOURCE="${1#*=}"
shift
;;
--debug)
DEBUG="1"
shift
;;
--force)
FORCE="1"
shift
;;
-h | --help)
usage
;;
--version)
show_version
;;
-y | --yes)
NONINTERACTIVE="1"
shift
;;
*)
usage "noexit"
abort "unrecognized option ${tty_bold}$1${tty_reset}; see usage above."
;;
esac
done
}
detect_arch() {
local arch
arch="$(/usr/bin/uname -m || /usr/bin/arch || uname -m || arch)"
if [[ "${arch}" == "arm64" ]] || [[ "${arch}" == "aarch64" ]]; then
DETECTED_ARCH="arm64"
elif [[ "${arch}" == "x86_64" ]] || [[ "${arch}" == "x64" ]]; then
DETECTED_ARCH="x64"
else
DETECTED_ARCH="${arch}"
fi
}
detect_os() {
local os
os="$(uname)"
if [[ "${os}" == "Darwin" ]]; then
DETECTED_OS="macos"
else
DETECTED_OS="${os}"
fi
}
major_minor() {
echo "${1%%.*}.$(
x="${1#*.}"
echo "${x%%.*}"
)"
}
version_compare() (
yy_a="$(echo "$1" | cut -d'.' -f1)"
yy_b="$(echo "$2" | cut -d'.' -f1)"
if [ "$yy_a" -lt "$yy_b" ]; then
return 1
fi
if [ "$yy_a" -gt "$yy_b" ]; then
return 0
fi
mm_a="$(echo "$1" | cut -d'.' -f2)"
mm_b="$(echo "$2" | cut -d'.' -f2)"
mm_a="${mm_a#0}"
mm_b="${mm_b#0}"
if [ "${mm_a:-0}" -lt "${mm_b:-0}" ]; then
return 1
fi
return 0
)
test_curl() {
if [[ ! -x "$1" ]]; then
return 1
fi
local curl_version_output curl_name_and_version
curl_version_output="$("$1" --version 2>/dev/null)"
curl_name_and_version="${curl_version_output%% (*}"
version_compare "$(major_minor "${curl_name_and_version##* }")" "$(major_minor "${REQUIRED_CURL_VERSION}")"
}
display_home_path() {
local path="$1"
if [[ "${path}" == "${HOME}" ]]; then
printf "~"
return 0
fi
if [[ "${path}" == "${HOME}/"* ]]; then
printf "%s/%s" "~" "${path#"${HOME}"/}"
return 0
fi
printf "%s" "${path}"
}
find_git_repo_root() {
local path="$1"
local parent
while :; do
if [[ -d "${path}/.git" ]]; then
printf "%s" "${path}"
return 0
fi
if [[ -f "${path}/HEAD" && -d "${path}/objects" && -d "${path}/refs" ]]; then
printf "%s" "${path}"
return 0
fi
parent="$(dirname "${path}")"
if [[ "${parent}" == "${path}" ]]; then
return 1
fi
path="${parent}"
done
}
resolve_local_repo_source_path() {
local source_path="$1"
local absolute_path
local repo_root
if ! absolute_path="$(cd "${source_path}" 2>/dev/null && pwd -P)"; then
return 1
fi
if ! repo_root="$(find_git_repo_root "${absolute_path}")"; then
return 1
fi
printf "%s" "${repo_root}"
}
resolve_repo_source_kind() {
local source_value="$1"
local allow_disable="${2:-0}"
if [[ "${allow_disable}" == "1" ]] && source_value_disabled "${source_value}"; then
printf "disabled"
elif [[ "${source_value}" == "ssh" ]]; then
printf "ssh"
elif is_semver_value "${source_value}"; then
printf "version"
else
printf "local"
fi
}
me_target_display() {
display_home_path "${ME_TARGET_PATH}"
}
tanaab_target_display() {
display_home_path "${TANAAB_TARGET_PATH}"
}
me_apply_brewfile_display() {
display_home_path "${ME_APPLY_BREWFILE}"
}
prepare_repo_source() {
local source_value="$1"
local target_path="$2"
local repo_label="$3"
local source_kind_var="$4"
local source_local_path_var="$5"
local source_version_tag_var="$6"
local allow_disable="${7:-0}"
local source_kind=""
local source_local_path=""
local source_version_tag=""
source_kind="$(resolve_repo_source_kind "${source_value}" "${allow_disable}")"
case "${source_kind}" in
disabled)
;;
ssh)
;;
version)
source_version_tag="$(normalize_release_tag "${source_value}")"
;;
local)
if ! source_local_path="$(resolve_local_repo_source_path "${source_value}")"; then
abort "local ${repo_label} source ${tty_ts}${source_value}${tty_reset} must resolve to a local git repo."
fi
if [[ "${source_local_path}" == "${target_path}" ]]; then
abort "local ${repo_label} source ${tty_ts}${source_local_path}${tty_reset} cannot be the same as target ${tty_ts}$(display_home_path "${target_path}")${tty_reset}."
fi
;;
*)
abort "unsupported internal ${repo_label} source kind ${tty_bold}${source_kind}${tty_reset}."
;;
esac
printf -v "${source_kind_var}" "%s" "${source_kind}"
printf -v "${source_local_path_var}" "%s" "${source_local_path}"
printf -v "${source_version_tag_var}" "%s" "${source_version_tag}"
}
prepare_me_source() {
ME_TARGET_PATH="${HOME}/tanaab/me"
prepare_repo_source \
"${ME_SOURCE}" \
"${ME_TARGET_PATH}" \
"me" \
"ME_SOURCE_KIND" \
"ME_SOURCE_LOCAL_PATH" \
"ME_SOURCE_VERSION_TAG"
}
prepare_tanaab_source() {
TANAAB_TARGET_PATH="${HOME}/tanaab/canon"
prepare_repo_source \
"${TANAAB_SOURCE}" \
"${TANAAB_TARGET_PATH}" \
"tanaab" \
"TANAAB_SOURCE_KIND" \
"TANAAB_SOURCE_LOCAL_PATH" \
"TANAAB_SOURCE_VERSION_TAG" \
"1"
}
tanaab_enabled() {
[[ "${TANAAB_SOURCE_KIND}" != "disabled" ]]
}
tanaab_plugin_checkout_path() {
printf "%s/dotfiles/ai/.codex/plugins/tanaab" "${ME_TARGET_PATH}"
}
tanaab_plugin_checkout_display() {
display_home_path "$(tanaab_plugin_checkout_path)"
}
prepare_tanaab_plugin_link() {
local ai_dotpkg_path="${ME_TARGET_PATH}/dotfiles/ai"
local plugin_checkout_path
local plugin_parent_path
plugin_checkout_path="$(tanaab_plugin_checkout_path)"
if ! tanaab_enabled; then
if [[ -L "${plugin_checkout_path}" || -e "${plugin_checkout_path}" ]]; then
execute rm -rf "${plugin_checkout_path}"
fi
return 0
fi
if [[ ! -d "${ai_dotpkg_path}" ]]; then
abort "me checkout at ${tty_ts}$(me_target_display)${tty_reset} is missing required ${tty_ts}ai${tty_reset} dotpkg needed for the tanaab plugin link."
fi
plugin_parent_path="$(dirname "${plugin_checkout_path}")"
execute mkdir -p "${plugin_parent_path}"
if [[ -L "${plugin_checkout_path}" || -e "${plugin_checkout_path}" ]]; then
execute rm -rf "${plugin_checkout_path}"
fi
execute ln -s "${TANAAB_PLUGIN_RELATIVE_TARGET}" "${plugin_checkout_path}"
}
discover_me_apply_payload() {
local dotfiles_root="${ME_TARGET_PATH}/dotfiles"
local dotpkg
ME_APPLY_BREWFILE="${ME_TARGET_PATH}/Brewfile"
ME_APPLY_DOTPKGS=()
if [[ ! -f "${ME_APPLY_BREWFILE}" ]]; then
abort "me checkout at ${tty_ts}$(me_target_display)${tty_reset} is missing required brewfile ${tty_ts}$(me_apply_brewfile_display)${tty_reset}."
fi
if [[ ! -d "${dotfiles_root}" ]]; then
abort "me checkout at ${tty_ts}$(me_target_display)${tty_reset} is missing required dotfiles directory ${tty_ts}$(display_home_path "${dotfiles_root}")${tty_reset}."
fi
while IFS= read -r dotpkg; do
append_array_value ME_APPLY_DOTPKGS "${dotpkg}"
done < <(find "${dotfiles_root}" -mindepth 1 -maxdepth 1 -type d | LC_ALL=C sort)
if [[ "${#ME_APPLY_DOTPKGS[@]}" -eq 0 ]]; then
abort "me checkout at ${tty_ts}$(me_target_display)${tty_reset} must contain at least one top-level dotpkg under ${tty_ts}$(display_home_path "${dotfiles_root}")${tty_reset}."
fi
}
build_git_ssh_command_from_ssh_keys() {
local repo_name="${1:-repo}"
local ssh_key
local key_path
local arg
local command_string=""
local -a ssh_command=(ssh)
local -a existing_key_paths=()
for ssh_key in "${SSH_KEYS[@]}"; do
key_path="$(ssh_key_destination_path "${ssh_key}")"
if [[ -f "${key_path}" ]]; then
existing_key_paths+=("${key_path}")
fi
done
if [[ "${#existing_key_paths[@]}" -eq 0 ]]; then
abort "cannot clone ${tty_ts}${repo_name}${tty_reset} via ssh because no installed ssh key paths were found."
fi
for key_path in "${existing_key_paths[@]}"; do
ssh_command+=(-i "${key_path}")
done
ssh_command+=(-o IdentitiesOnly=yes)
for arg in "${ssh_command[@]}"; do
printf -v command_string '%s%q ' "${command_string}" "${arg}"
done
printf "%s" "${command_string% }"
}
repo_release_archive_url() {
local release_base_url="$1"
local archive_prefix="$2"
local tag="$3"
printf "%s/%s/%s-%s.tar.gz" "${release_base_url}" "${tag}" "${archive_prefix}" "${tag}"
}
repo_prepare_target() {
local target="$1"
if [[ -e "${target}" ]]; then
if ! force_enabled; then
return 1
fi
execute rm -rf "${target}"
fi
execute mkdir -p "$(dirname "${target}")"
return 0
}
fetch_repo_source_to_target() {
local repo_name="$1"
local source_value="$2"
local target="$3"
local ssh_url="$4"
local release_base_url="$5"
local archive_prefix="$6"
local source_kind="$7"
local git_ssh_command
local archive_tag
local archive_url
local archive_path
if ! repo_prepare_target "${target}"; then
warn "${tty_tp}skipping${tty_reset} ${tty_ts}${repo_name}${tty_reset} because ${tty_ts}$(display_home_path "${target}")${tty_reset} already exists and ${tty_bold}--force${tty_reset} is not set."
return 0
fi
case "${source_kind}" in
ssh)
git_ssh_command="$(build_git_ssh_command_from_ssh_keys "${repo_name}")"
log "${tty_tp}cloning${tty_reset} ${tty_ts}${repo_name}${tty_reset} via ssh to ${tty_ts}$(display_home_path "${target}")${tty_reset}"
execute env GIT_SSH_COMMAND="${git_ssh_command}" git clone "${ssh_url}" "${target}"
;;
local)
log "${tty_tp}cloning${tty_reset} ${tty_ts}${repo_name}${tty_reset} from local repo ${tty_ts}${source_value}${tty_reset} to ${tty_ts}$(display_home_path "${target}")${tty_reset}"
execute git clone "${source_value}" "${target}"
;;
version)
archive_tag="$(normalize_release_tag "${source_value}")"
archive_url="$(repo_release_archive_url "${release_base_url}" "${archive_prefix}" "${archive_tag}")"
archive_path="${BOOT_TMPDIR}/${repo_name}-${archive_tag}.tar.gz"
log "${tty_tp}extracting${tty_reset} ${tty_ts}${repo_name}${tty_reset} release ${tty_ts}${archive_tag}${tty_reset} to ${tty_ts}$(display_home_path "${target}")${tty_reset}"
execute mkdir -p "${target}"
execute "${CURL}" -fsSL "${archive_url}" -o "${archive_path}"
execute tar -xzf "${archive_path}" -C "${target}"
;;
*)
abort "unsupported internal repo source kind ${tty_bold}${source_kind}${tty_reset}."
;;
esac
}
plan_repo_fetch() {
local repo_name="$1"
local target_path="$2"
local source_kind="$3"
local source_local_path="$4"
local source_version_tag="$5"
local target_display
target_display="$(display_home_path "${target_path}")"
if [[ -e "${target_path}" ]]; then
if force_enabled; then
plan_action "${tty_tp}replace${tty_reset} existing ${tty_ts}${repo_name}${tty_reset} checkout at ${tty_ts}${target_display}${tty_reset} because ${tty_bold}--force${tty_reset} is set"
else
plan_action "${tty_tp}skip${tty_reset} fetching ${tty_ts}${repo_name}${tty_reset} because ${tty_ts}${target_display}${tty_reset} already exists and ${tty_bold}--force${tty_reset} is not set"
return 0
fi
fi
case "${source_kind}" in
ssh)
plan_action "${tty_tp}clone${tty_reset} ${tty_ts}${repo_name}${tty_reset} via ssh to ${tty_ts}${target_display}${tty_reset}"
;;
local)
plan_action "${tty_tp}clone${tty_reset} ${tty_ts}${repo_name}${tty_reset} from local repo ${tty_ts}${source_local_path}${tty_reset} to ${tty_ts}${target_display}${tty_reset}"
;;
version)
plan_action "${tty_tp}extract${tty_reset} ${tty_ts}${repo_name}${tty_reset} release ${tty_ts}${source_version_tag}${tty_reset} to ${tty_ts}${target_display}${tty_reset}"
;;
esac
}
plan_me_fetch() {
plan_repo_fetch \
"me" \
"${ME_TARGET_PATH}" \
"${ME_SOURCE_KIND}" \
"${ME_SOURCE_LOCAL_PATH}" \
"${ME_SOURCE_VERSION_TAG}"
}
plan_tanaab_fetch() {
plan_repo_fetch \
"tanaab" \
"${TANAAB_TARGET_PATH}" \
"${TANAAB_SOURCE_KIND}" \
"${TANAAB_SOURCE_LOCAL_PATH}" \
"${TANAAB_SOURCE_VERSION_TAG}"
}
plan_tanaab_plugin_link() {
plan_action "${tty_tp}ensure${tty_reset} relative ${tty_ts}tanaab${tty_reset} plugin link at ${tty_ts}$(tanaab_plugin_checkout_display)${tty_reset} points to ${tty_ts}$(tanaab_target_display)${tty_reset}"
}
plan_me_apply() {
plan_action "${tty_tp}run${tty_reset} ${tty_ts}bootbox${tty_reset} against the ${tty_ts}me${tty_reset} checkout at ${tty_ts}$(me_target_display)${tty_reset} using its ${tty_ts}Brewfile${tty_reset} and dotpkgs on ${tty_ts}~${tty_reset}"
}
run_me_fetch() {
fetch_repo_source_to_target \
"me" \
"${ME_SOURCE_LOCAL_PATH:-${ME_SOURCE_VERSION_TAG:-${ME_SOURCE}}}" \
"${ME_TARGET_PATH}" \
"${ME_REPO_SSH_URL}" \
"${ME_REPO_RELEASE_BASE_URL}" \
"${ME_REPO_RELEASE_ARCHIVE_PREFIX}" \
"${ME_SOURCE_KIND}"
}
run_tanaab_fetch() {
fetch_repo_source_to_target \
"tanaab" \
"${TANAAB_SOURCE_LOCAL_PATH:-${TANAAB_SOURCE_VERSION_TAG:-${TANAAB_SOURCE}}}" \
"${TANAAB_TARGET_PATH}" \
"${TANAAB_REPO_SSH_URL}" \
"${TANAAB_REPO_RELEASE_BASE_URL}" \
"${TANAAB_REPO_RELEASE_ARCHIVE_PREFIX}" \
"${TANAAB_SOURCE_KIND}"
}
run_bootbox_for_me_apply() {
local dotpkg
local -a bootbox_args=(--brewfile "${ME_APPLY_BREWFILE}")
for dotpkg in "${ME_APPLY_DOTPKGS[@]}"; do
bootbox_args+=(--dotpkg "${dotpkg}")
done
bootbox_run_or_abort me "bootbox failed while applying me checkout ${tty_ts}$(me_target_display)${tty_reset}." "${bootbox_args[@]}"
}
plan_action() {
PLANNED_ACTIONS+=("$1")
}
ssh_key_spec_base() {
printf "%s" "${1%%:*}"
}
ssh_key_spec_filename_override() {
if [[ "$1" == *:* ]]; then
printf "%s" "${1#*:}"
fi
}
ssh_key_spec_item() {
local spec_base
spec_base="$(ssh_key_spec_base "$1")"
printf "%s" "${spec_base##*/}"
}
ssh_key_filename() {
local filename_override
filename_override="$(ssh_key_spec_filename_override "$1")"
if [[ -n "${filename_override}" ]]; then
printf "%s" "${filename_override}"
return 0
fi
ssh_key_spec_item "$1"
}
ssh_key_target_root() {
printf "%s" "${PIROME_TARGET:-${HOME}}"
}
ssh_key_destination_path() {
printf "%s/.ssh/%s" "$(ssh_key_target_root)" "$(ssh_key_filename "$1")"
}
describe_ssh_key_specs() {
local first="1"
local ssh_key
local destination_path
if [[ $# -eq 0 ]]; then
return 0
fi
for ssh_key in "$@"; do
if [[ "${first}" == "1" ]]; then
first="0"
else
printf ", "
fi
destination_path="$(ssh_key_destination_path "${ssh_key}")"
printf "%s ${tty_dim}->${tty_reset} ${tty_ts}%s${tty_reset}" "${ssh_key}" "${destination_path}"
done
}
collect_ssh_key_actions() {