forked from terrastruct/d2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·1101 lines (966 loc) · 26.9 KB
/
install.sh
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/sh
set -eu
# *************
# DO NOT EDIT
#
# install.sh was bundled together from
#
# - ./ci/sub/lib/rand.sh
# - ./ci/sub/lib/temp.sh
# - ./ci/sub/lib/log.sh
# - ./ci/sub/lib/flag.sh
# - ./ci/sub/lib/release.sh
# - ./ci/release/_install.sh
#
# The last of which implements the installation logic.
#
# Generated by ./ci/release/gen_install.sh.
# *************
#!/bin/sh
if [ "${LIB_RAND-}" ]; then
return 0
fi
LIB_RAND=1
pick() {
seed="$1"
shift
seed_file="$(mktempd)/pickseed"
# We add 32 more bytes to the seed file for sufficient entropy. Otherwise both Cygwin's
# and MinGW's sort for example complains about the lack of entropy on stderr and writes
# nothing to stdout. I'm sure there are more platforms that would too.
#
# We also limit to a max of 32 bytes as otherwise macOS's sort complains that the random
# seed is too large. Probably more platforms too.
(echo "$seed" && echo "================================") | head -c32 >"$seed_file"
while [ $# -gt 0 ]; do
echo "$1"
shift
done \
| sort --sort=random --random-source="$seed_file" \
| head -n1
}
#!/bin/sh
if [ "${LIB_TEMP-}" ]; then
return 0
fi
LIB_TEMP=1
ensure_tmpdir() {
if [ -n "${_TMPDIR-}" ]; then
return
fi
_TMPDIR=$(mktemp -d)
export _TMPDIR
}
if [ -z "${_TMPDIR-}" ]; then
trap 'rm -Rf "$_TMPDIR"' EXIT
fi
ensure_tmpdir
temppath() {
while true; do
temppath=$_TMPDIR/$(</dev/urandom od -N8 -tx -An -v | tr -d '[:space:]')
if [ ! -e "$temppath" ]; then
echo "$temppath"
return
fi
done
}
mktempd() {
tp=$(temppath)
mkdir -p "$tp"
echo "$tp"
}
#!/bin/sh
if [ "${LIB_LOG-}" ]; then
return 0
fi
LIB_LOG=1
if [ -n "${TRACE-}" ]; then
set -x
fi
tput() {
if should_color; then
TERM=${TERM:-xterm-256color} command tput "$@"
fi
}
should_color() {
if [ -n "${COLOR-}" ]; then
if [ "$COLOR" = 1 -o "$COLOR" = true ]; then
_COLOR=1
__COLOR=1
return 0
elif [ "$COLOR" = 0 -o "$COLOR" = false ]; then
_COLOR=
__COLOR=0
return 1
else
printf '$COLOR must be 0, 1, false or true but got %s\n' "$COLOR" >&2
fi
fi
if [ -t 1 -a "${TERM-}" != dumb ]; then
_COLOR=1
__COLOR=1
return 0
else
_COLOR=
__COLOR=0
return 1
fi
}
setaf() {
fg=$1
shift
printf '%s\n' "$*" | while IFS= read -r line; do
tput setaf "$fg"
printf '%s' "$line"
tput sgr0
printf '\n'
done
}
_echo() {
printf '%s\n' "$*"
}
get_rand_color() {
if [ "${TERM_COLORS+x}" != x ]; then
TERM_COLORS=""
export TERM_COLORS
ncolors=$(TERM=${TERM:-xterm-256color} command tput colors)
if [ "$ncolors" -ge 8 ]; then
# 1-6 are regular
TERM_COLORS="$TERM_COLORS 1 2 3 4 5 6"
elif [ "$ncolors" -ge 16 ]; then
# 9-14 are bright.
TERM_COLORS="$TERM_COLORS 9 10 11 12 13 14"
fi
fi
pick "$*" $TERM_COLORS
}
echop() {
prefix="$1"
shift
if [ "$#" -gt 0 ]; then
printfp "$prefix" "%s\n" "$*"
else
printfp "$prefix"
printf '\n'
fi
}
printfp() {(
prefix="$1"
shift
_FGCOLOR=${FGCOLOR:-$(get_rand_color "$prefix")}
should_color || true
if [ $# -eq 0 ]; then
printf '%s' "$(COLOR=$__COLOR setaf "$_FGCOLOR" "$prefix")"
else
printf '%s: %s\n' "$(COLOR=$__COLOR setaf "$_FGCOLOR" "$prefix")" "$(printf "$@")"
fi
)}
catp() {
prefix="$1"
shift
should_color || true
sed "s/^/$(COLOR=$__COLOR printfp "$prefix" '')/"
}
repeat() {
char="$1"
times="$2"
seq -s "$char" "$times" | tr -d '[:digit:]'
}
strlen() {
printf %s "$1" | wc -c
}
echoerr() {
FGCOLOR=1 logp err "$*"
}
caterr() {
FGCOLOR=1 logpcat err "$@"
}
printferr() {
FGCOLOR=1 logfp err "$@"
}
logp() {
should_color >&2 || true
COLOR=$__COLOR echop "$@" | humanpath >&2
}
logfp() {
should_color >&2 || true
COLOR=$__COLOR printfp "$@" | humanpath >&2
}
logpcat() {
should_color >&2 || true
COLOR=$__COLOR catp "$@" | humanpath >&2
}
log() {
FGCOLOR=5 logp log "$@"
}
logf() {
FGCOLOR=5 logfp log "$@"
}
logcat() {
FGCOLOR=5 logpcat log "$@"
}
warn() {
FGCOLOR=3 logp warn "$@"
}
warnf() {
FGCOLOR=3 logfp warn "$@"
}
warncat() {
FGCOLOR=3 logpcat warn "$@"
}
sh_c() {
FGCOLOR=3 logp exec "$*"
if [ -z "${DRY_RUN-}" ]; then
eval "$@"
fi
}
sudo_sh_c() {
if [ "$(id -u)" -eq 0 ]; then
sh_c "$@"
elif command -v doas >/dev/null; then
sh_c "doas $*"
elif command -v sudo >/dev/null; then
sh_c "sudo $*"
elif command -v su >/dev/null; then
sh_c "su root -c '$*'"
else
caterr <<EOF
Unable to run the following command as root:
$*
Please install doas, sudo, or su.
EOF
return 1
fi
}
header() {
FGCOLOR=${FGCOLOR:-4} logp "/* $1 */"
}
bigheader() {
set -- "$(echo "$*" | sed "s/^/ * /")"
FGCOLOR=${FGCOLOR:-6} logp "/****************************************************************
$*
****************************************************************/"
}
# humanpath replaces all occurrences of " $HOME" with " ~"
# and all occurrences of '$HOME' with the literal '$HOME'.
humanpath() {
if [ -z "${HOME-}" ]; then
cat
else
sed -e "s# $HOME# ~#g" -e "s#$HOME#\$HOME#g"
fi
}
hide() {
out="$(mktempd)/hideout"
capcode "$@" >"$out" 2>&1
if [ "$code" -eq 0 ]; then
return
fi
cat "$out" >&2
return "$code"
}
hide_stderr() {
out="$(mktempd)/hideout"
capcode "$@" 2>"$out"
if [ "$code" -eq 0 ]; then
return
fi
cat "$out" >&2
return "$code"
}
echo_dur() {
local dur=$1
local h=$((dur/60/60))
local m=$((dur/60%60))
local s=$((dur%60))
printf '%dh%dm%ds' "$h" "$m" "$s"
}
sponge() {
dst="$1"
tmp="$(mktempd)/sponge"
cat > "$tmp"
cat "$tmp" > "$dst"
}
stripansi() {
# First regex gets rid of standard xterm escape sequences for controlling
# visual attributes.
# The second regex I'm not 100% sure, the reference says it selects the US
# encoding but I'm not sure why that's necessary or why it always occurs
# in tput sgr0 before the standard escape sequence.
# See tput sgr0 | xxd
sed -e $'s/\x1b\[[0-9;]*m//g' -e $'s/\x1b(.//g'
}
runtty() {
case "$(uname)" in
Darwin)
script -q /dev/null "$@"
;;
Linux)
script -eqc "$*"
;;
*)
echoerr "runtty: unsupported OS $(uname)"
return 1
esac
}
capcode() {
set +e
"$@"
code=$?
set -e
}
strjoin() {
(IFS="$1"; shift; echo "$*")
}
#!/bin/sh
if [ "${LIB_FLAG-}" ]; then
return 0
fi
LIB_FLAG=1
# flag_parse implements a robust flag parser.
#
# For a full fledge example see ../examples/date.sh
#
# It differs from getopts(1) in that long form options are supported. Currently the only
# deficiency is that short combined options are not supported like -xyzq. That would be
# interpreted as a single -xyzq flag. The other deficiency is lack of support for short
# flag syntax like -carg where the arg is not separated from the flag. This one is
# unfixable I believe unfortunately but for combined short flags I have opened
# https://github.com/terrastruct/ci/issues/6
#
# flag_parse stores state in $FLAG, $FLAGRAW, $FLAGARG and $FLAGSHIFT.
# FLAG contains the name of the flag without hyphens.
# FLAGRAW contains the name of the flag as passed in with hyphens.
# FLAGARG contains the argument for the flag if there was any.
# If there was none, it will not be set.
# FLAGSHIFT contains the number by which the arguments should be shifted to
# start at the next flag/argument
#
# flag_parse exits with a non zero code when there are no more flags
# to be parsed. Still, call shift "$FLAGSHIFT" in case there was a --
#
# If the argument for the flag is optional, then use ${FLAGARG-} to access
# the argument if one was passed. Use ${FLAGARG+x} = x to check if it was set.
# You only need to explicitly check if the flag was set if you care whether the user
# explicitly passed the empty string as the argument.
#
# Otherwise, call one of the flag_*arg functions:
#
# If a flag requires an argument, call flag_reqarg
# - $FLAGARG is guaranteed to be set after.
# If a flag requires a non empty argument, call flag_nonemptyarg
# - $FLAGARG is guaranteed to be set to a non empty string after.
# If a flag should not be passed an argument, call flag_noarg
# - $FLAGARG is guaranteed to be unset after.
#
# And then shift "$FLAGSHIFT"
flag_parse() {
case "${1-}" in
-*=*)
# Remove everything after first equal sign.
FLAG="${1%%=*}"
# Remove leading hyphens.
FLAG="${FLAG#-}"; FLAG="${FLAG#-}"
FLAGRAW="$(flag_fmt)"
# Remove everything before first equal sign.
FLAGARG="${1#*=}"
FLAGSHIFT=1
return 0
;;
-)
FLAGSHIFT=0
return 1
;;
--)
FLAGSHIFT=1
return 1
;;
-*)
# Remove leading hyphens.
FLAG="${1#-}"; FLAG="${FLAG#-}"
FLAGRAW=$(flag_fmt)
unset FLAGARG
FLAGSHIFT=1
if [ $# -gt 1 ]; then
case "$2" in
-)
FLAGARG="$2"
FLAGSHIFT=2
;;
-*)
;;
*)
FLAGARG="$2"
FLAGSHIFT=2
;;
esac
fi
return 0
;;
*)
FLAGSHIFT=0
return 1
;;
esac
}
flag_reqarg() {
if [ "${FLAGARG+x}" != x ]; then
flag_errusage "flag $FLAGRAW requires an argument"
fi
}
flag_nonemptyarg() {
flag_reqarg
if [ -z "$FLAGARG" ]; then
flag_errusage "flag $FLAGRAW requires a non-empty argument"
fi
}
flag_noarg() {
if [ "$FLAGSHIFT" -eq 2 ]; then
unset FLAGARG
FLAGSHIFT=1
elif [ "${FLAGARG+x}" = x ]; then
# Means an argument was passed via equal sign as in -$FLAG=$FLAGARG
flag_errusage "flag $FLAGRAW does not accept an argument"
fi
}
flag_errusage() {
caterr <<EOF
$1
Run with --help for usage.
EOF
return 1
}
flag_fmt() {
if [ "$(printf %s "$FLAG" | wc -c)" -eq 1 ]; then
echo "-$FLAG"
else
echo "--$FLAG"
fi
}
#!/bin/sh
if [ "${LIB_RELEASE-}" ]; then
return 0
fi
LIB_RELEASE=1
ensure_os() {
if [ -n "${OS-}" ]; then
# Windows defines OS=Windows_NT.
if [ "$OS" = Windows_NT ]; then
OS=windows
fi
return
fi
uname=$(uname)
case $uname in
Linux) OS=linux;;
Darwin) OS=macos;;
FreeBSD) OS=freebsd;;
*) OS=$uname;;
esac
}
ensure_arch() {
if [ -n "${ARCH-}" ]; then
return
fi
uname_m=$(uname -m)
case $uname_m in
aarch64) ARCH=arm64;;
x86_64) ARCH=amd64;;
*) ARCH=$uname_m;;
esac
}
ensure_goos() {
if [ -n "${GOOS-}" ]; then
return
fi
ensure_os
case "$OS" in
macos) export GOOS=darwin;;
*) export GOOS=$OS;;
esac
}
ensure_goarch() {
if [ -n "${GOARCH-}" ]; then
return
fi
ensure_arch
case "$ARCH" in
*) export GOARCH=$ARCH;;
esac
}
gh_repo() {
gh repo view --json nameWithOwner --template '{{ .nameWithOwner }}'
}
manpath() {
if command -v manpath >/dev/null; then
command manpath
elif man -w 2>/dev/null; then
man -w
else
echo "${MANPATH-}"
fi
}
is_writable_dir() {
mkdir -p "$1" 2>/dev/null
# directory must exist otherwise -w returns 1 even for paths that should be writable.
[ -w "$1" ]
}
ensure_prefix() {
if [ -n "${PREFIX-}" ]; then
return
fi
# The reason for checking whether lib is writable is that on macOS you have /usr/local
# owned by root but you don't need root to write to its subdirectories which is all we
# need to do.
if ! is_writable_dir "/usr/local/lib"; then
# This also handles M1 Mac's which do not allow modifications to /usr/local even
# with sudo.
PREFIX=$HOME/.local
else
PREFIX=/usr/local
fi
}
ensure_prefix_sh_c() {
ensure_prefix
sh_c="sh_c"
# The reason for checking whether lib is writable is that on macOS you have /usr/local
# owned by root but you don't need root to write to its subdirectories which is all we
# need to do.
if ! is_writable_dir "$PREFIX/lib"; then
sh_c="sudo_sh_c"
fi
}
#!/bin/sh
set -eu
help() {
arg0="$0"
if [ "$0" = sh ]; then
arg0="curl -fsSL https://d2lang.com/install.sh | sh -s --"
fi
cat <<EOF
usage: $arg0 [-d|--dry-run] [--version vX.X.X] [--edge] [--method detect] [--prefix path]
[--tala latest] [--force] [--uninstall] [-x|--trace]
install.sh automates the installation of D2 onto your system. It currently only supports
the installation of standalone releases from GitHub and via Homebrew on macOS. See the
docs for --detect below for more information
If you pass --edge, it will clone the source, build a release and install from it.
--edge is incompatible with --tala and currently unimplemented.
\$PREFIX in the docs below refers to the path set by --prefix. See docs on the --prefix
flag below for the default.
Flags:
-d, --dry-run
Pass to have install.sh show the install method and flags that will be used to install
without executing them. Very useful to understand what changes it will make to your system.
--version vX.X.X
Pass to have install.sh install the given version instead of the latest version.
warn: The version may not be obeyed with package manager installations. Use
--method=standalone to enforce the version.
--edge
Pass to build and install D2 from source. This will still use --method if set to detect
to install the release archive for your OS, whether it's apt, yum, brew or standalone
if an unsupported package manager is used.
To install from source like a dev would, use go install oss.terrastruct.com/d2. There's
also ./ci/release/build.sh --install to build and install a proper standalone release
including manpages. The proper release will also ensure d2 --version shows the correct
version by embedding the commit hash into the binary.
note: currently unimplemented.
warn: incompatible with --tala as TALA is closed source.
--method [detect | standalone | homebrew ]
Pass to control the method by which to install. Right now we only support standalone
releases from GitHub but later we'll add support for brew, rpm, deb and more.
- detect will use your OS's package manager automatically.
So far it only detects macOS and automatically uses homebrew.
- homebrew uses https://brew.sh/ which is a macOS and Linux package manager.
- standalone installs a standalone release archive into the unix hierarchy path
specified by --prefix
--prefix path
Controls the unix hierarchy path into which standalone releases are installed.
Defaults to /usr/local or ~/.local if /usr/local is not writable by the current user.
Remember that whatever you use, you must have the bin directory of your prefix path in
\$PATH to execute the d2 binary. For example, if my prefix directory is /usr/local then
my \$PATH must contain /usr/local/bin.
You may also need to include \$PREFIX/share/man into \$MANPATH.
install.sh will tell you whether \$PATH or \$MANPATH need to be updated after successful
installation.
--tala [latest]
Install Terrastruct's closed source TALA for improved layouts.
See https://github.com/terrastruct/tala
It optionally takes an argument of the TALA version to install.
Installation obeys all other flags, just like the installation of d2. For example,
the d2plugin-tala binary will be installed into \$PREFIX/bin/d2plugin-tala
warn: The version may not be obeyed with package manager installations. Use
--method=standalone to enforce the version.
--force:
Force installation over the existing version even if they match. It will attempt a
uninstall first before installing the new version. The installed release tree
will be deleted from \$PREFIX/lib/d2/d2-<VERSION> but the release archive in
~/.cache/d2/release will remain.
--uninstall:
Uninstall the installed version of d2. The --method and --prefix flags must be the same
as for installation. i.e if you used --method standalone you must again use --method
standalone for uninstallation. With detect, the install script will try to use the OS
package manager to uninstall instead.
note: tala will also be uninstalled if installed.
-x, --trace:
Run script with set -x.
All downloaded archives are cached into ~/.cache/d2/release. use \$XDG_CACHE_HOME to change
path of the cached assets. Release archives are unarchived into \$PREFIX/lib/d2/d2-<VERSION>
note: Deleting the unarchived releases will cause --uninstall to stop working.
You can rerun install.sh to update your version of D2. install.sh will avoid reinstalling
if the installed version is the latest unless --force is passed.
See https://github.com/terrastruct/d2/blob/master/docs/INSTALL.md#security for
documentation on its security.
EOF
}
main() {
while flag_parse "$@"; do
case "$FLAG" in
h|help)
help
return 0
;;
d|dry-run)
flag_noarg && shift "$FLAGSHIFT"
DRY_RUN=1
;;
version)
flag_nonemptyarg && shift "$FLAGSHIFT"
VERSION=$FLAGARG
;;
tala)
shift "$FLAGSHIFT"
TALA=${FLAGARG:-latest}
;;
edge)
flag_noarg && shift "$FLAGSHIFT"
EDGE=1
echoerr "$FLAGRAW is currently unimplemented"
return 1
;;
method)
flag_nonemptyarg && shift "$FLAGSHIFT"
METHOD=$FLAGARG
;;
prefix)
flag_nonemptyarg && shift "$FLAGSHIFT"
export PREFIX=$FLAGARG
;;
force)
flag_noarg && shift "$FLAGSHIFT"
FORCE=1
;;
uninstall)
flag_noarg && shift "$FLAGSHIFT"
UNINSTALL=1
;;
x|trace)
flag_noarg && shift "$FLAGSHIFT"
set -x
export TRACE=1
;;
*)
flag_errusage "unrecognized flag $FLAGRAW"
;;
esac
done
shift "$FLAGSHIFT"
if [ $# -gt 0 ]; then
flag_errusage "no arguments are accepted"
fi
REPO=${REPO:-terrastruct/d2}
ensure_os
ensure_arch
ensure_prefix
CACHE_DIR=$(cache_dir)
mkdir -p "$CACHE_DIR"
METHOD=${METHOD:-detect}
INSTALL_DIR=$PREFIX/lib/d2
case $METHOD in
detect)
case "$OS" in
macos)
if command -v brew >/dev/null; then
log "detected macOS with homebrew, using homebrew for installation"
METHOD=homebrew
else
warn "detected macOS without homebrew, falling back to --method=standalone"
METHOD=standalone
fi
;;
linux|windows)
METHOD=standalone
;;
*)
warn "unrecognized OS $OS, falling back to --method=standalone"
METHOD=standalone
;;
esac
;;
standalone) ;;
homebrew) ;;
*)
echoerr "unknown installation method $METHOD"
return 1
;;
esac
if [ -n "${UNINSTALL-}" ]; then
uninstall
if [ -n "${DRY_RUN-}" ]; then
bigheader "Rerun without --dry-run to execute printed commands and perform install."
fi
else
install
if [ -n "${DRY_RUN-}" ]; then
bigheader "Rerun without --dry-run to execute printed commands and perform install."
fi
fi
}
install() {
case $METHOD in
standalone)
install_d2_standalone
if [ -n "${TALA-}" ]; then
# Run in subshell to avoid overwriting VERSION.
TALA_VERSION="$( RELEASE_INFO= install_tala_standalone && echo "$VERSION" )"
fi
;;
homebrew)
install_d2_brew
if [ -n "${TALA-}" ]; then install_tala_brew; fi
;;
esac
FGCOLOR=2 bigheader 'next steps'
case $METHOD in
standalone) install_post_standalone ;;
homebrew) install_post_brew ;;
esac
install_post_warn
}
install_post_standalone() {
log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
if [ -n "${TALA-}" ]; then
log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
fi
log "Rerun this install script with --uninstall to uninstall."
log
if ! echo "$PATH" | grep -qF "$PREFIX/bin"; then
logcat >&2 <<EOF
Extend your \$PATH to use d2:
export PATH=$PREFIX/bin:\$PATH
Then run:
${TALA:+D2_LAYOUT=tala }d2 --help
EOF
else
log "Run ${TALA:+D2_LAYOUT=tala }d2 --help for usage."
fi
if ! manpath 2>/dev/null | grep -qF "$PREFIX/share/man"; then
logcat >&2 <<EOF
Extend your \$MANPATH to view d2's manpages:
export MANPATH=$PREFIX/share/man:\$MANPATH
Then run:
man d2
EOF
if [ -n "${TALA-}" ]; then
log " man d2plugin-tala"
fi
else
log "Run man d2 for detailed docs."
if [ -n "${TALA-}" ]; then
log "Run man d2plugin-tala for detailed TALA docs."
fi
fi
}
install_post_brew() {
log "d2 has been successfully installed with homebrew."
if [ -n "${TALA-}" ]; then
log "tala has been successfully installed with homebrew."
fi
log "Rerun this install script with --uninstall to uninstall."
log
log "Run ${TALA:+D2_LAYOUT=tala }d2 --help for usage."
log "Run man d2 for detailed docs."
if [ -n "${TALA-}" ]; then
log "Run man d2plugin-tala for detailed TALA docs."
fi
VERSION=$(brew info d2 | head -n1 | cut -d' ' -f4)
VERSION=${VERSION%,}
if [ -n "${TALA-}" ]; then
TALA_VERSION=$(brew info tala | head -n1 | cut -d' ' -f4)
TALA_VERSION=${TALA_VERSION%,}
fi
}
install_post_warn() {
if command -v d2 >/dev/null; then
INSTALLED_VERSION=$(d2 --version)
if [ "$INSTALLED_VERSION" != "$VERSION" ]; then
warn "newly installed d2 $VERSION is shadowed by d2 $INSTALLED_VERSION in \$PATH"
fi
fi
if [ -n "${TALA-}" ] && command -v d2plugin-tala >/dev/null; then
INSTALLED_TALA_VERSION=$(d2plugin-tala --version)
if [ "$INSTALLED_TALA_VERSION" != "$TALA_VERSION" ]; then
warn "newly installed d2plugin-tala $TALA_VERSION is shadowed by d2plugin-tala $INSTALLED_TALA_VERSION in \$PATH"
fi
fi
}
install_d2_standalone() {
VERSION=${VERSION:-latest}
header "installing d2-$VERSION"
if [ "$VERSION" = latest ]; then
fetch_release_info
fi
if command -v d2 >/dev/null; then
INSTALLED_VERSION="$(d2 --version)"
if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then
log "skipping installation as d2 $VERSION is already installed."
return 0
fi
log "uninstalling d2 $INSTALLED_VERSION to install $VERSION"
if ! uninstall_d2_standalone; then
warn "failed to uninstall d2 $INSTALLED_VERSION"
fi
fi
ARCHIVE="d2-$VERSION-$OS-$ARCH.tar.gz"
log "installing standalone release $ARCHIVE from github"
fetch_release_info
asset_line=$(sh_c 'cat "$RELEASE_INFO" | grep -n "$ARCHIVE" | cut -d: -f1 | head -n1')
asset_url=$(sh_c 'sed -n $((asset_line-3))p "$RELEASE_INFO" | sed "s/^.*: \"\(.*\)\",$/\1/g"')
fetch_gh "$asset_url" "$CACHE_DIR/$ARCHIVE" 'application/octet-stream'
ensure_prefix_sh_c
"$sh_c" mkdir -p "'$INSTALL_DIR'"
"$sh_c" tar -C "$INSTALL_DIR" -xzf "$CACHE_DIR/$ARCHIVE"
"$sh_c" sh -c "'cd \"$INSTALL_DIR/d2-$VERSION\" && make install PREFIX=\"$PREFIX\"'"
}
install_d2_brew() {
header "installing d2 with homebrew"
sh_c brew update
sh_c brew install d2
}
install_tala_standalone() {
REPO="${REPO_TALA:-terrastruct/tala}"
VERSION=$TALA
header "installing tala-$VERSION"
if [ "$VERSION" = latest ]; then
fetch_release_info
fi
if command -v d2plugin-tala >/dev/null; then
INSTALLED_VERSION="$(d2plugin-tala --version)"
if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then
log "skipping installation as tala $VERSION is already installed."
return 0
fi
log "uninstalling tala $INSTALLED_VERSION to install $VERSION"
if ! uninstall_tala_standalone; then
warn "failed to uninstall tala $INSTALLED_VERSION"
fi
fi
ARCHIVE="tala-$VERSION-$OS-$ARCH.tar.gz"
log "installing standalone release $ARCHIVE from github"
fetch_release_info
asset_line=$(sh_c 'cat "$RELEASE_INFO" | grep -n "$ARCHIVE" | cut -d: -f1 | head -n1')
asset_url=$(sh_c 'sed -n $((asset_line-3))p "$RELEASE_INFO" | sed "s/^.*: \"\(.*\)\",$/\1/g"')
fetch_gh "$asset_url" "$CACHE_DIR/$ARCHIVE" 'application/octet-stream'
ensure_prefix_sh_c
"$sh_c" mkdir -p "'$INSTALL_DIR'"
"$sh_c" tar -C "$INSTALL_DIR" -xzf "$CACHE_DIR/$ARCHIVE"
"$sh_c" sh -c "'cd \"$INSTALL_DIR/tala-$VERSION\" && make install PREFIX=\"$PREFIX\"'"
}
install_tala_brew() {
header "installing tala with homebrew"
sh_c brew update
sh_c brew install terrastruct/tap/tala
}
uninstall() {
# We uninstall tala first as package managers require that it be uninstalled before
# uninstalling d2 as TALA depends on d2.
if command -v d2plugin-tala >/dev/null; then
INSTALLED_VERSION="$(d2plugin-tala --version)"
header "uninstalling tala-$INSTALLED_VERSION"
case $METHOD in
standalone) uninstall_tala_standalone ;;
homebrew) uninstall_tala_brew ;;
esac
elif [ "${TALA-}" ]; then