2022-11-14 05:58:55 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
|
|
# *************
|
|
|
|
|
# DO NOT EDIT
|
|
|
|
|
#
|
|
|
|
|
# install.sh was bundled together from
|
|
|
|
|
#
|
|
|
|
|
# - ./ci/sub/lib/rand.sh
|
2022-12-06 06:15:48 +00:00
|
|
|
# - ./ci/sub/lib/temp.sh
|
2022-11-14 05:58:55 +00:00
|
|
|
# - ./ci/sub/lib/log.sh
|
|
|
|
|
# - ./ci/sub/lib/flag.sh
|
2022-11-14 11:40:35 +00:00
|
|
|
# - ./ci/sub/lib/release.sh
|
2022-11-14 05:58:55 +00:00
|
|
|
# - ./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
|
|
|
|
|
|
2022-12-02 01:29:40 +00:00
|
|
|
pick() {
|
2022-11-14 05:58:55 +00:00
|
|
|
seed="$1"
|
2022-12-02 01:29:40 +00:00
|
|
|
shift
|
2022-11-14 05:58:55 +00:00
|
|
|
|
2022-12-06 06:15:48 +00:00
|
|
|
seed_file="$(mktempd)/pickseed"
|
2022-12-06 05:51:46 +00:00
|
|
|
|
|
|
|
|
# We add 32 more bytes to the seed file for sufficient entropy. Otherwise both Cygwin's
|
2022-12-04 22:56:50 +00:00
|
|
|
# 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.
|
2022-12-06 05:51:46 +00:00
|
|
|
#
|
|
|
|
|
# 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"
|
2022-11-14 20:42:07 +00:00
|
|
|
|
2022-12-02 01:29:40 +00:00
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
|
echo "$1"
|
|
|
|
|
shift
|
|
|
|
|
done \
|
|
|
|
|
| sort --sort=random --random-source="$seed_file" \
|
|
|
|
|
| head -n1
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
#!/bin/sh
|
2022-12-06 06:15:48 +00:00
|
|
|
if [ "${LIB_TEMP-}" ]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
LIB_TEMP=1
|
|
|
|
|
|
2022-12-13 11:29:30 +00:00
|
|
|
ensure_tmpdir() {
|
|
|
|
|
if [ -n "${_TMPDIR-}" ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
2022-12-06 06:15:48 +00:00
|
|
|
_TMPDIR=$(mktemp -d)
|
|
|
|
|
export _TMPDIR
|
2022-12-13 11:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [ -z "${_TMPDIR-}" ]; then
|
2022-12-08 12:01:24 +00:00
|
|
|
trap 'rm -Rf "$_TMPDIR"' EXIT
|
|
|
|
|
fi
|
2022-12-13 11:29:30 +00:00
|
|
|
ensure_tmpdir
|
2022-12-06 06:15:48 +00:00
|
|
|
|
|
|
|
|
temppath() {
|
|
|
|
|
while true; do
|
2022-12-06 13:17:17 +00:00
|
|
|
temppath=$_TMPDIR/$(</dev/urandom od -N8 -tx -An -v | tr -d '[:space:]')
|
2022-12-06 06:15:48 +00:00
|
|
|
if [ ! -e "$temppath" ]; then
|
|
|
|
|
echo "$temppath"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mktempd() {
|
|
|
|
|
tp=$(temppath)
|
|
|
|
|
mkdir -p "$tp"
|
|
|
|
|
echo "$tp"
|
|
|
|
|
}
|
|
|
|
|
#!/bin/sh
|
2022-11-14 05:58:55 +00:00
|
|
|
if [ "${LIB_LOG-}" ]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
LIB_LOG=1
|
|
|
|
|
|
2022-12-06 12:18:40 +00:00
|
|
|
if [ -n "${TRACE-}" ]; then
|
2022-11-14 21:51:24 +00:00
|
|
|
set -x
|
|
|
|
|
fi
|
|
|
|
|
|
2022-11-14 05:58:55 +00:00
|
|
|
tput() {
|
2022-11-20 10:09:32 +00:00
|
|
|
if should_color; then
|
|
|
|
|
TERM=${TERM:-xterm-256color} command tput "$@"
|
2022-11-16 18:48:39 +00:00
|
|
|
fi
|
2022-11-20 10:09:32 +00:00
|
|
|
}
|
2022-11-16 18:48:39 +00:00
|
|
|
|
2022-11-20 10:09:32 +00:00
|
|
|
should_color() {
|
|
|
|
|
if [ -n "${COLOR-}" ]; then
|
2022-12-01 14:49:48 +00:00
|
|
|
if [ "$COLOR" = 1 -o "$COLOR" = true ]; then
|
2022-11-20 10:09:32 +00:00
|
|
|
_COLOR=1
|
2022-12-01 14:49:48 +00:00
|
|
|
__COLOR=1
|
2022-11-20 10:09:32 +00:00
|
|
|
return 0
|
2022-12-01 14:49:48 +00:00
|
|
|
elif [ "$COLOR" = 0 -o "$COLOR" = false ]; then
|
|
|
|
|
_COLOR=
|
|
|
|
|
__COLOR=0
|
|
|
|
|
return 1
|
2022-11-20 10:09:32 +00:00
|
|
|
else
|
2022-11-29 22:18:52 +00:00
|
|
|
printf '$COLOR must be 0, 1, false or true but got %s\n' "$COLOR" >&2
|
2022-11-20 10:09:32 +00:00
|
|
|
fi
|
|
|
|
|
fi
|
2022-11-20 11:19:40 +00:00
|
|
|
|
2022-12-01 06:39:46 +00:00
|
|
|
if [ -t 1 -a "${TERM-}" != dumb ]; then
|
2022-11-20 10:09:32 +00:00
|
|
|
_COLOR=1
|
2022-12-01 14:49:48 +00:00
|
|
|
__COLOR=1
|
2022-11-20 10:09:32 +00:00
|
|
|
return 0
|
|
|
|
|
else
|
2022-11-20 11:19:40 +00:00
|
|
|
_COLOR=
|
2022-12-01 14:49:48 +00:00
|
|
|
__COLOR=0
|
2022-11-20 10:09:32 +00:00
|
|
|
return 1
|
2022-11-14 05:58:55 +00:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setaf() {
|
2022-12-13 11:29:30 +00:00
|
|
|
fg=$1
|
2022-11-14 05:58:55 +00:00
|
|
|
shift
|
2022-12-13 11:29:30 +00:00
|
|
|
printf '%s\n' "$*" | while IFS= read -r line; do
|
|
|
|
|
tput setaf "$fg"
|
|
|
|
|
printf '%s' "$line"
|
|
|
|
|
tput sgr0
|
|
|
|
|
printf '\n'
|
|
|
|
|
done
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_echo() {
|
|
|
|
|
printf '%s\n' "$*"
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 20:10:11 +00:00
|
|
|
get_rand_color() {
|
2022-12-06 05:51:46 +00:00
|
|
|
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
|
2022-12-02 20:10:11 +00:00
|
|
|
fi
|
2022-12-06 05:51:46 +00:00
|
|
|
pick "$*" $TERM_COLORS
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echop() {
|
|
|
|
|
prefix="$1"
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
|
|
if [ "$#" -gt 0 ]; then
|
|
|
|
|
printfp "$prefix" "%s\n" "$*"
|
|
|
|
|
else
|
|
|
|
|
printfp "$prefix"
|
|
|
|
|
printf '\n'
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printfp() {(
|
|
|
|
|
prefix="$1"
|
|
|
|
|
shift
|
|
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
_FGCOLOR=${FGCOLOR:-$(get_rand_color "$prefix")}
|
2022-11-20 13:19:39 +00:00
|
|
|
should_color || true
|
2022-11-20 11:19:40 +00:00
|
|
|
if [ $# -eq 0 ]; then
|
2022-12-06 05:51:46 +00:00
|
|
|
printf '%s' "$(COLOR=$__COLOR setaf "$_FGCOLOR" "$prefix")"
|
2022-11-20 11:19:40 +00:00
|
|
|
else
|
2022-12-06 05:51:46 +00:00
|
|
|
printf '%s: %s\n' "$(COLOR=$__COLOR setaf "$_FGCOLOR" "$prefix")" "$(printf "$@")"
|
2022-11-14 05:58:55 +00:00
|
|
|
fi
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
catp() {
|
|
|
|
|
prefix="$1"
|
|
|
|
|
shift
|
|
|
|
|
|
2022-11-20 10:09:32 +00:00
|
|
|
should_color || true
|
2022-12-01 14:49:48 +00:00
|
|
|
sed "s/^/$(COLOR=$__COLOR printfp "$prefix" '')/"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
repeat() {
|
|
|
|
|
char="$1"
|
|
|
|
|
times="$2"
|
|
|
|
|
seq -s "$char" "$times" | tr -d '[:digit:]'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strlen() {
|
|
|
|
|
printf %s "$1" | wc -c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echoerr() {
|
2022-11-29 23:25:26 +00:00
|
|
|
FGCOLOR=1 logp err "$*"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
caterr() {
|
2022-11-29 23:25:26 +00:00
|
|
|
FGCOLOR=1 logpcat err "$@"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printferr() {
|
2022-11-29 23:25:26 +00:00
|
|
|
FGCOLOR=1 logfp err "$@"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logp() {
|
2022-11-20 10:09:32 +00:00
|
|
|
should_color >&2 || true
|
2022-12-01 14:49:48 +00:00
|
|
|
COLOR=$__COLOR echop "$@" | humanpath >&2
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logfp() {
|
2022-11-20 10:09:32 +00:00
|
|
|
should_color >&2 || true
|
2022-12-01 14:49:48 +00:00
|
|
|
COLOR=$__COLOR printfp "$@" | humanpath >&2
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logpcat() {
|
2022-11-20 10:09:32 +00:00
|
|
|
should_color >&2 || true
|
2022-12-01 14:49:48 +00:00
|
|
|
COLOR=$__COLOR catp "$@" | humanpath >&2
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=5 logp log "$@"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logf() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=5 logfp log "$@"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logcat() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=5 logpcat log "$@"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 06:41:58 +00:00
|
|
|
warn() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=3 logp warn "$@"
|
2022-11-14 06:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
warnf() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=3 logfp warn "$@"
|
2022-11-14 06:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 10:09:32 +00:00
|
|
|
warncat() {
|
|
|
|
|
FGCOLOR=3 logpcat warn "$@"
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 05:58:55 +00:00
|
|
|
sh_c() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=3 logp exec "$*"
|
2022-11-14 07:20:30 +00:00
|
|
|
if [ -z "${DRY_RUN-}" ]; then
|
2022-11-14 05:58:55 +00:00
|
|
|
eval "$@"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 10:48:47 +00:00
|
|
|
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
|
2022-12-06 06:15:48 +00:00
|
|
|
Unable to run the following command as root:
|
2022-11-14 10:48:47 +00:00
|
|
|
$*
|
|
|
|
|
Please install doas, sudo, or su.
|
|
|
|
|
EOF
|
2022-11-17 19:30:42 +00:00
|
|
|
return 1
|
2022-11-14 10:48:47 +00:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 05:58:55 +00:00
|
|
|
header() {
|
2022-12-01 23:08:19 +00:00
|
|
|
FGCOLOR=${FGCOLOR:-4} logp "/* $1 */"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
bigheader() {
|
2022-11-29 22:18:52 +00:00
|
|
|
set -- "$(echo "$*" | sed "s/^/ * /")"
|
2022-12-16 08:47:36 +00:00
|
|
|
FGCOLOR=${FGCOLOR:-6} logp "/****************************************************************
|
2022-11-29 22:18:52 +00:00
|
|
|
$*
|
2022-12-06 15:54:14 +00:00
|
|
|
****************************************************************/"
|
2022-11-20 13:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 20:42:07 +00:00
|
|
|
# humanpath replaces all occurrences of " $HOME" with " ~"
|
2022-11-14 21:18:31 +00:00
|
|
|
# and all occurrences of '$HOME' with the literal '$HOME'.
|
2022-11-14 10:48:47 +00:00
|
|
|
humanpath() {
|
|
|
|
|
if [ -z "${HOME-}" ]; then
|
|
|
|
|
cat
|
|
|
|
|
else
|
2022-11-14 21:18:31 +00:00
|
|
|
sed -e "s# $HOME# ~#g" -e "s#$HOME#\$HOME#g"
|
2022-11-14 10:48:47 +00:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 05:58:55 +00:00
|
|
|
hide() {
|
2022-12-06 06:15:48 +00:00
|
|
|
out="$(mktempd)/hideout"
|
|
|
|
|
capcode "$@" >"$out" 2>&1
|
2022-11-14 05:58:55 +00:00
|
|
|
if [ "$code" -eq 0 ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
cat "$out" >&2
|
2022-11-17 19:30:42 +00:00
|
|
|
return "$code"
|
2022-11-14 05:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-13 06:55:44 +00:00
|
|
|
hide_stderr() {
|
|
|
|
|
out="$(mktempd)/hideout"
|
|
|
|
|
capcode "$@" 2>"$out"
|
|
|
|
|
if [ "$code" -eq 0 ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
cat "$out" >&2
|
|
|
|
|
return "$code"
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 05:58:55 +00:00
|
|
|
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"
|
2022-12-06 06:15:48 +00:00
|
|
|
tmp="$(mktempd)/sponge"
|
2022-11-14 05:58:55 +00:00
|
|
|
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
|
|
|
|
|
}
|
2022-12-01 14:49:48 +00:00
|
|
|
|
|
|
|
|
capcode() {
|
|
|
|
|
set +e
|
|
|
|
|
"$@"
|
|
|
|
|
code=$?
|
|
|
|
|
set -e
|
|
|
|
|
}
|
2022-12-06 05:51:46 +00:00
|
|
|
|
|
|
|
|
strjoin() {
|
|
|
|
|
(IFS="$1"; shift; echo "$*")
|
|
|
|
|
}
|
2022-11-14 05:58:55 +00:00
|
|
|
#!/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
|
|
|
|
|
#
|
2022-11-16 18:48:39 +00:00
|
|
|
# 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 --
|
2022-11-14 05:58:55 +00:00
|
|
|
#
|
|
|
|
|
# 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#-}"
|
2022-11-14 07:20:30 +00:00
|
|
|
FLAGRAW="$(flag_fmt)"
|
2022-11-14 05:58:55 +00:00
|
|
|
# Remove everything before first equal sign.
|
|
|
|
|
FLAGARG="${1#*=}"
|
|
|
|
|
FLAGSHIFT=1
|
2022-11-16 18:48:39 +00:00
|
|
|
return 0
|
2022-11-14 05:58:55 +00:00
|
|
|
;;
|
|
|
|
|
-)
|
|
|
|
|
FLAGSHIFT=0
|
2022-11-16 18:48:39 +00:00
|
|
|
return 1
|
2022-11-14 05:58:55 +00:00
|
|
|
;;
|
|
|
|
|
--)
|
|
|
|
|
FLAGSHIFT=1
|
2022-11-16 18:48:39 +00:00
|
|
|
return 1
|
2022-11-14 05:58:55 +00:00
|
|
|
;;
|
|
|
|
|
-*)
|
|
|
|
|
# Remove leading hyphens.
|
|
|
|
|
FLAG="${1#-}"; FLAG="${FLAG#-}"
|
2022-11-14 07:20:30 +00:00
|
|
|
FLAGRAW=$(flag_fmt)
|
2022-11-14 05:58:55 +00:00
|
|
|
unset FLAGARG
|
|
|
|
|
FLAGSHIFT=1
|
|
|
|
|
if [ $# -gt 1 ]; then
|
|
|
|
|
case "$2" in
|
|
|
|
|
-)
|
|
|
|
|
FLAGARG="$2"
|
|
|
|
|
FLAGSHIFT=2
|
|
|
|
|
;;
|
|
|
|
|
-*)
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
FLAGARG="$2"
|
|
|
|
|
FLAGSHIFT=2
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
fi
|
2022-11-16 18:48:39 +00:00
|
|
|
return 0
|
2022-11-14 05:58:55 +00:00
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
FLAGSHIFT=0
|
2022-11-16 18:48:39 +00:00
|
|
|
return 1
|
2022-11-14 05:58:55 +00:00
|
|
|
;;
|
|
|
|
|
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
|
|
|
|
|
}
|
2022-11-14 07:20:30 +00:00
|
|
|
|
|
|
|
|
flag_fmt() {
|
|
|
|
|
if [ "$(printf %s "$FLAG" | wc -c)" -eq 1 ]; then
|
|
|
|
|
echo "-$FLAG"
|
|
|
|
|
else
|
|
|
|
|
echo "--$FLAG"
|
|
|
|
|
fi
|
|
|
|
|
}
|
2022-11-14 05:58:55 +00:00
|
|
|
#!/bin/sh
|
2022-11-14 10:48:47 +00:00
|
|
|
if [ "${LIB_RELEASE-}" ]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
LIB_RELEASE=1
|
|
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
ensure_os() {
|
2022-12-06 12:41:06 +00:00
|
|
|
if [ -n "${OS-}" ]; then
|
|
|
|
|
# Windows defines OS=Windows_NT.
|
2022-12-06 16:00:14 +00:00
|
|
|
if [ "$OS" = Windows_NT ]; then
|
2022-12-06 12:41:06 +00:00
|
|
|
OS=windows
|
|
|
|
|
fi
|
2022-12-06 05:51:46 +00:00
|
|
|
return
|
|
|
|
|
fi
|
2022-11-14 10:48:47 +00:00
|
|
|
uname=$(uname)
|
|
|
|
|
case $uname in
|
2022-12-06 05:51:46 +00:00
|
|
|
Linux) OS=linux;;
|
|
|
|
|
Darwin) OS=macos;;
|
|
|
|
|
FreeBSD) OS=freebsd;;
|
|
|
|
|
*) OS=$uname;;
|
2022-11-14 10:48:47 +00:00
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
ensure_arch() {
|
|
|
|
|
if [ -n "${ARCH-}" ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
2022-11-14 10:48:47 +00:00
|
|
|
uname_m=$(uname -m)
|
|
|
|
|
case $uname_m in
|
2022-12-06 05:51:46 +00:00
|
|
|
aarch64) ARCH=arm64;;
|
|
|
|
|
x86_64) ARCH=amd64;;
|
|
|
|
|
*) ARCH=$uname_m;;
|
2022-11-14 10:48:47 +00:00
|
|
|
esac
|
|
|
|
|
}
|
2022-11-14 18:26:37 +00:00
|
|
|
|
2022-12-06 12:41:06 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 18:26:37 +00:00
|
|
|
gh_repo() {
|
|
|
|
|
gh repo view --json nameWithOwner --template '{{ .nameWithOwner }}'
|
|
|
|
|
}
|
2022-11-14 21:00:52 +00:00
|
|
|
|
|
|
|
|
manpath() {
|
|
|
|
|
if command -v manpath >/dev/null; then
|
|
|
|
|
command manpath
|
|
|
|
|
elif man -w 2>/dev/null; then
|
|
|
|
|
man -w
|
|
|
|
|
else
|
|
|
|
|
echo "${MANPATH-}"
|
|
|
|
|
fi
|
|
|
|
|
}
|
2022-12-01 20:26:32 +00:00
|
|
|
|
|
|
|
|
is_writable_dir() {
|
2022-12-06 11:22:55 +00:00
|
|
|
mkdir -p "$1" 2>/dev/null
|
|
|
|
|
# directory must exist otherwise -w returns 1 even for paths that should be writable.
|
|
|
|
|
[ -w "$1" ]
|
2022-12-06 05:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ensure_prefix() {
|
|
|
|
|
if [ -n "${PREFIX-}" ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
# The reason for checking whether bin 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/bin"; 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 bin 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/bin"; then
|
|
|
|
|
sh_c="sudo_sh_c"
|
2022-12-01 20:26:32 +00:00
|
|
|
fi
|
|
|
|
|
}
|
2022-11-14 10:48:47 +00:00
|
|
|
#!/bin/sh
|
2022-11-14 05:58:55 +00:00
|
|
|
set -eu
|
|
|
|
|
|
|
|
|
|
|
2022-11-14 06:41:58 +00:00
|
|
|
help() {
|
|
|
|
|
arg0="$0"
|
|
|
|
|
if [ "$0" = sh ]; then
|
|
|
|
|
arg0="curl -fsSL https://d2lang.com/install.sh | sh -s --"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
cat <<EOF
|
2022-12-06 05:51:46 +00:00
|
|
|
usage: $arg0 [-d|--dry-run] [--version vX.X.X] [--edge] [--method detect] [--prefix path]
|
2022-12-06 12:18:40 +00:00
|
|
|
[--tala latest] [--force] [--uninstall] [-x|--trace]
|
2022-11-14 06:41:58 +00:00
|
|
|
|
|
|
|
|
install.sh automates the installation of D2 onto your system. It currently only supports
|
2022-11-20 13:19:39 +00:00
|
|
|
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.
|
2022-11-14 06:41:58 +00:00
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
\$PREFIX in the docs below refers to the path set by --prefix. See docs on the --prefix
|
|
|
|
|
flag below for the default.
|
|
|
|
|
|
2022-11-14 06:41:58 +00:00
|
|
|
Flags:
|
|
|
|
|
|
2022-11-29 22:18:52 +00:00
|
|
|
-d, --dry-run
|
2022-11-14 06:41:58 +00:00
|
|
|
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.
|
2022-11-20 13:19:39 +00:00
|
|
|
warn: The version may not be obeyed with package manager installations. Use
|
|
|
|
|
--method=standalone to enforce the version.
|
2022-11-14 06:41:58 +00:00
|
|
|
|
|
|
|
|
--edge
|
2022-11-14 07:20:30 +00:00
|
|
|
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
|
2022-12-06 09:30:11 +00:00
|
|
|
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.
|
|
|
|
|
|
2022-11-14 07:20:30 +00:00
|
|
|
note: currently unimplemented.
|
2022-11-20 13:19:39 +00:00
|
|
|
warn: incompatible with --tala as TALA is closed source.
|
2022-11-14 06:41:58 +00:00
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
--method [detect | standalone | homebrew ]
|
2022-11-14 06:41:58 +00:00
|
|
|
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.
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
- 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.
|
2022-11-14 10:48:47 +00:00
|
|
|
- standalone installs a standalone release archive into the unix hierarchy path
|
2022-12-06 05:51:46 +00:00
|
|
|
specified by --prefix
|
2022-11-14 06:41:58 +00:00
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
--prefix path
|
2022-11-14 06:41:58 +00:00
|
|
|
Controls the unix hierarchy path into which standalone releases are installed.
|
2022-12-06 05:51:46 +00:00
|
|
|
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.
|
2022-11-14 06:41:58 +00:00
|
|
|
|
2022-11-14 11:40:35 +00:00
|
|
|
--tala [latest]
|
2022-11-14 06:41:58 +00:00
|
|
|
Install Terrastruct's closed source TALA for improved layouts.
|
2022-11-20 13:19:39 +00:00
|
|
|
See https://github.com/terrastruct/tala
|
2022-11-14 11:40:35 +00:00
|
|
|
It optionally takes an argument of the TALA version to install.
|
|
|
|
|
Installation obeys all other flags, just like the installation of d2. For example,
|
2022-12-06 05:51:46 +00:00
|
|
|
the d2plugin-tala binary will be installed into \$PREFIX/bin/d2plugin-tala
|
2022-11-20 13:19:39 +00:00
|
|
|
warn: The version may not be obeyed with package manager installations. Use
|
|
|
|
|
--method=standalone to enforce the version.
|
2022-11-14 06:41:58 +00:00
|
|
|
|
|
|
|
|
--force:
|
2022-11-14 10:48:47 +00:00
|
|
|
Force installation over the existing version even if they match. It will attempt a
|
2022-11-14 11:40:35 +00:00
|
|
|
uninstall first before installing the new version. The installed release tree
|
2022-12-06 05:51:46 +00:00
|
|
|
will be deleted from \$PREFIX/lib/d2/d2-<VERSION> but the release archive in
|
2022-11-14 11:40:35 +00:00
|
|
|
~/.cache/d2/release will remain.
|
2022-11-14 06:41:58 +00:00
|
|
|
|
|
|
|
|
--uninstall:
|
2022-11-14 10:48:47 +00:00
|
|
|
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.
|
2022-11-21 04:37:06 +00:00
|
|
|
note: tala will also be uninstalled if installed.
|
2022-11-14 06:41:58 +00:00
|
|
|
|
2022-12-06 12:18:40 +00:00
|
|
|
-x, --trace:
|
|
|
|
|
Run script with set -x.
|
|
|
|
|
|
2022-11-14 10:48:47 +00:00
|
|
|
All downloaded archives are cached into ~/.cache/d2/release. use \$XDG_CACHE_HOME to change
|
2022-12-06 05:51:46 +00:00
|
|
|
path of the cached assets. Release archives are unarchived into \$PREFIX/lib/d2/d2-<VERSION>
|
2022-11-14 10:48:47 +00:00
|
|
|
|
|
|
|
|
note: Deleting the unarchived releases will cause --uninstall to stop working.
|
2022-11-14 06:41:58 +00:00
|
|
|
|
|
|
|
|
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.
|
2022-12-01 20:59:53 +00:00
|
|
|
|
|
|
|
|
See https://github.com/terrastruct/d2/blob/master/docs/INSTALL.md#security for
|
|
|
|
|
documentation on its security.
|
2022-11-14 06:41:58 +00:00
|
|
|
EOF
|
|
|
|
|
}
|
2022-11-14 07:20:30 +00:00
|
|
|
|
|
|
|
|
main() {
|
2022-11-16 18:48:39 +00:00
|
|
|
while flag_parse "$@"; do
|
2022-11-14 07:20:30 +00:00
|
|
|
case "$FLAG" in
|
|
|
|
|
h|help)
|
|
|
|
|
help
|
|
|
|
|
return 0
|
|
|
|
|
;;
|
2022-11-29 22:18:52 +00:00
|
|
|
d|dry-run)
|
2022-11-14 07:20:30 +00:00
|
|
|
flag_noarg && shift "$FLAGSHIFT"
|
|
|
|
|
DRY_RUN=1
|
|
|
|
|
;;
|
|
|
|
|
version)
|
|
|
|
|
flag_nonemptyarg && shift "$FLAGSHIFT"
|
|
|
|
|
VERSION=$FLAGARG
|
|
|
|
|
;;
|
2022-11-14 11:40:35 +00:00
|
|
|
tala)
|
|
|
|
|
shift "$FLAGSHIFT"
|
|
|
|
|
TALA=${FLAGARG:-latest}
|
2022-11-14 07:20:30 +00:00
|
|
|
;;
|
|
|
|
|
edge)
|
|
|
|
|
flag_noarg && shift "$FLAGSHIFT"
|
|
|
|
|
EDGE=1
|
|
|
|
|
echoerr "$FLAGRAW is currently unimplemented"
|
2022-11-14 11:40:35 +00:00
|
|
|
return 1
|
2022-11-14 07:20:30 +00:00
|
|
|
;;
|
|
|
|
|
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
|
|
|
|
|
;;
|
2022-12-06 12:18:40 +00:00
|
|
|
x|trace)
|
|
|
|
|
flag_noarg && shift "$FLAGSHIFT"
|
|
|
|
|
set -x
|
|
|
|
|
export TRACE=1
|
|
|
|
|
;;
|
2022-11-14 07:20:30 +00:00
|
|
|
*)
|
|
|
|
|
flag_errusage "unrecognized flag $FLAGRAW"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
2022-11-16 18:48:39 +00:00
|
|
|
shift "$FLAGSHIFT"
|
2022-11-14 07:20:30 +00:00
|
|
|
|
|
|
|
|
if [ $# -gt 0 ]; then
|
|
|
|
|
flag_errusage "no arguments are accepted"
|
|
|
|
|
fi
|
2022-11-14 10:13:37 +00:00
|
|
|
|
|
|
|
|
REPO=${REPO:-terrastruct/d2}
|
2022-12-06 05:51:46 +00:00
|
|
|
ensure_os
|
|
|
|
|
ensure_arch
|
|
|
|
|
ensure_prefix
|
2022-11-14 10:48:47 +00:00
|
|
|
CACHE_DIR=$(cache_dir)
|
|
|
|
|
mkdir -p "$CACHE_DIR"
|
2022-11-20 13:19:39 +00:00
|
|
|
METHOD=${METHOD:-detect}
|
2022-11-14 11:40:35 +00:00
|
|
|
INSTALL_DIR=$PREFIX/lib/d2
|
2022-11-14 10:48:47 +00:00
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
case $METHOD in
|
|
|
|
|
detect)
|
|
|
|
|
case "$OS" in
|
|
|
|
|
macos)
|
2022-11-21 04:16:29 +00:00
|
|
|
if command -v brew >/dev/null; then
|
2022-12-06 09:30:11 +00:00
|
|
|
log "detected macOS with homebrew, using homebrew for installation"
|
2022-11-21 04:16:29 +00:00
|
|
|
METHOD=homebrew
|
|
|
|
|
else
|
|
|
|
|
warn "detected macOS without homebrew, falling back to --method=standalone"
|
|
|
|
|
METHOD=standalone
|
|
|
|
|
fi
|
2022-11-20 13:19:39 +00:00
|
|
|
;;
|
2022-12-06 12:03:47 +00:00
|
|
|
linux|windows)
|
|
|
|
|
METHOD=standalone
|
|
|
|
|
;;
|
2022-11-20 13:19:39 +00:00
|
|
|
*)
|
|
|
|
|
warn "unrecognized OS $OS, falling back to --method=standalone"
|
|
|
|
|
METHOD=standalone
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
standalone) ;;
|
|
|
|
|
homebrew) ;;
|
|
|
|
|
*)
|
2022-12-06 09:30:11 +00:00
|
|
|
echoerr "unknown installation method $METHOD"
|
2022-11-20 13:19:39 +00:00
|
|
|
return 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
2022-11-14 10:48:47 +00:00
|
|
|
if [ -n "${UNINSTALL-}" ]; then
|
2022-11-14 12:02:23 +00:00
|
|
|
uninstall
|
2022-11-21 04:16:29 +00:00
|
|
|
if [ -n "${DRY_RUN-}" ]; then
|
2022-12-06 15:54:14 +00:00
|
|
|
bigheader "Rerun without --dry-run to execute printed commands and perform install."
|
2022-11-21 04:16:29 +00:00
|
|
|
fi
|
2022-11-20 13:19:39 +00:00
|
|
|
else
|
|
|
|
|
install
|
2022-11-21 04:16:29 +00:00
|
|
|
if [ -n "${DRY_RUN-}" ]; then
|
2022-12-06 15:54:14 +00:00
|
|
|
bigheader "Rerun without --dry-run to execute printed commands and perform install."
|
2022-11-21 04:16:29 +00:00
|
|
|
fi
|
2022-11-14 10:48:47 +00:00
|
|
|
fi
|
2022-11-14 11:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
install() {
|
2022-11-20 13:19:39 +00:00
|
|
|
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
|
2022-12-06 09:30:11 +00:00
|
|
|
install_post_warn
|
2022-11-20 13:19:39 +00:00
|
|
|
}
|
2022-11-14 10:48:47 +00:00
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
install_post_standalone() {
|
2022-11-14 12:10:40 +00:00
|
|
|
log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
|
2022-11-14 22:46:52 +00:00
|
|
|
if [ -n "${TALA-}" ]; then
|
|
|
|
|
log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
|
|
|
|
|
fi
|
2022-11-20 13:19:39 +00:00
|
|
|
log "Rerun this install script with --uninstall to uninstall."
|
|
|
|
|
log
|
2022-11-14 11:40:35 +00:00
|
|
|
if ! echo "$PATH" | grep -qF "$PREFIX/bin"; then
|
|
|
|
|
logcat >&2 <<EOF
|
2022-11-14 22:46:52 +00:00
|
|
|
Extend your \$PATH to use d2:
|
2022-11-14 21:00:52 +00:00
|
|
|
export PATH=$PREFIX/bin:\$PATH
|
2022-11-14 11:40:35 +00:00
|
|
|
Then run:
|
2022-11-14 22:46:52 +00:00
|
|
|
${TALA+D2_LAYOUT=tala }d2 --help
|
2022-11-14 11:40:35 +00:00
|
|
|
EOF
|
|
|
|
|
else
|
2022-11-20 13:19:39 +00:00
|
|
|
log "Run ${TALA+D2_LAYOUT=tala }d2 --help for usage."
|
2022-11-14 21:00:52 +00:00
|
|
|
fi
|
2022-12-06 12:03:47 +00:00
|
|
|
if ! manpath 2>/dev/null | grep -qF "$PREFIX/share/man"; then
|
2022-11-14 21:00:52 +00:00
|
|
|
logcat >&2 <<EOF
|
|
|
|
|
Extend your \$MANPATH to view d2's manpages:
|
2022-11-16 13:17:57 +00:00
|
|
|
export MANPATH=$PREFIX/share/man:\$MANPATH
|
2022-11-14 21:00:52 +00:00
|
|
|
Then run:
|
|
|
|
|
man d2
|
|
|
|
|
EOF
|
2022-11-14 22:46:52 +00:00
|
|
|
if [ -n "${TALA-}" ]; then
|
|
|
|
|
log " man d2plugin-tala"
|
|
|
|
|
fi
|
2022-11-14 21:00:52 +00:00
|
|
|
else
|
2022-11-20 13:19:39 +00:00
|
|
|
log "Run man d2 for detailed docs."
|
2022-11-14 22:46:52 +00:00
|
|
|
if [ -n "${TALA-}" ]; then
|
2022-11-20 13:19:39 +00:00
|
|
|
log "Run man d2plugin-tala for detailed TALA docs."
|
2022-11-14 22:46:52 +00:00
|
|
|
fi
|
2022-11-14 11:40:35 +00:00
|
|
|
fi
|
2022-11-14 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
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
|
2022-12-06 09:30:11 +00:00
|
|
|
|
|
|
|
|
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
|
2022-11-20 13:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
install_d2_standalone() {
|
|
|
|
|
VERSION=${VERSION:-latest}
|
2022-12-01 23:08:19 +00:00
|
|
|
header "installing d2-$VERSION"
|
2022-11-20 13:19:39 +00:00
|
|
|
|
|
|
|
|
if [ "$VERSION" = latest ]; then
|
|
|
|
|
fetch_release_info
|
|
|
|
|
fi
|
|
|
|
|
|
2022-11-14 12:02:23 +00:00
|
|
|
if command -v d2 >/dev/null; then
|
2022-12-06 09:30:11 +00:00
|
|
|
INSTALLED_VERSION="$(d2 --version)"
|
2022-11-14 12:02:23 +00:00
|
|
|
if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then
|
2022-11-20 13:19:39 +00:00
|
|
|
log "skipping installation as d2 $VERSION is already installed."
|
2022-11-14 12:02:23 +00:00
|
|
|
return 0
|
|
|
|
|
fi
|
2022-11-20 13:19:39 +00:00
|
|
|
log "uninstalling d2 $INSTALLED_VERSION to install $VERSION"
|
|
|
|
|
if ! uninstall_d2_standalone; then
|
|
|
|
|
warn "failed to uninstall d2 $INSTALLED_VERSION"
|
2022-11-14 12:02:23 +00:00
|
|
|
fi
|
2022-11-14 11:40:35 +00:00
|
|
|
fi
|
2022-11-14 12:02:23 +00:00
|
|
|
|
2022-11-14 10:48:47 +00:00
|
|
|
ARCHIVE="d2-$VERSION-$OS-$ARCH.tar.gz"
|
2022-11-14 12:10:40 +00:00
|
|
|
log "installing standalone release $ARCHIVE from github"
|
2022-11-14 10:48:47 +00:00
|
|
|
|
2022-11-14 11:40:35 +00:00
|
|
|
fetch_release_info
|
2022-11-14 20:42:07 +00:00
|
|
|
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"')
|
2022-11-14 10:48:47 +00:00
|
|
|
fetch_gh "$asset_url" "$CACHE_DIR/$ARCHIVE" 'application/octet-stream'
|
|
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
ensure_prefix_sh_c
|
2022-11-14 23:38:48 +00:00
|
|
|
"$sh_c" mkdir -p "'$INSTALL_DIR'"
|
2022-11-14 11:40:35 +00:00
|
|
|
"$sh_c" tar -C "$INSTALL_DIR" -xzf "$CACHE_DIR/$ARCHIVE"
|
|
|
|
|
"$sh_c" sh -c "'cd \"$INSTALL_DIR/d2-$VERSION\" && make install PREFIX=\"$PREFIX\"'"
|
2022-11-14 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
install_d2_brew() {
|
2022-12-01 23:08:19 +00:00
|
|
|
header "installing d2 with homebrew"
|
2022-12-08 01:15:51 +00:00
|
|
|
sh_c brew update
|
2022-11-20 13:19:39 +00:00
|
|
|
sh_c brew install d2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
install_tala_standalone() {
|
|
|
|
|
REPO="${REPO_TALA:-terrastruct/tala}"
|
2022-11-14 12:10:40 +00:00
|
|
|
VERSION=$TALA
|
2022-11-20 13:19:39 +00:00
|
|
|
|
2022-12-01 23:08:19 +00:00
|
|
|
header "installing tala-$VERSION"
|
2022-11-14 12:02:23 +00:00
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
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
|
|
|
|
|
|
2022-11-14 12:33:22 +00:00
|
|
|
ARCHIVE="tala-$VERSION-$OS-$ARCH.tar.gz"
|
2022-11-14 12:10:40 +00:00
|
|
|
log "installing standalone release $ARCHIVE from github"
|
2022-11-14 12:02:23 +00:00
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
fetch_release_info
|
2022-11-14 20:42:07 +00:00
|
|
|
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"')
|
2022-11-14 12:02:23 +00:00
|
|
|
|
|
|
|
|
fetch_gh "$asset_url" "$CACHE_DIR/$ARCHIVE" 'application/octet-stream'
|
|
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
ensure_prefix_sh_c
|
2022-11-14 23:38:48 +00:00
|
|
|
"$sh_c" mkdir -p "'$INSTALL_DIR'"
|
2022-11-14 12:02:23 +00:00
|
|
|
"$sh_c" tar -C "$INSTALL_DIR" -xzf "$CACHE_DIR/$ARCHIVE"
|
2022-11-14 12:33:22 +00:00
|
|
|
"$sh_c" sh -c "'cd \"$INSTALL_DIR/tala-$VERSION\" && make install PREFIX=\"$PREFIX\"'"
|
2022-11-14 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
install_tala_brew() {
|
2022-12-01 23:08:19 +00:00
|
|
|
header "installing tala with homebrew"
|
2022-12-08 01:35:10 +00:00
|
|
|
sh_c brew update
|
|
|
|
|
sh_c brew install terrastruct/tap/tala
|
2022-11-20 13:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 12:02:23 +00:00
|
|
|
uninstall() {
|
2022-11-20 13:19:39 +00:00
|
|
|
# We uninstall tala first as package managers require that it be uninstalled before
|
|
|
|
|
# uninstalling d2 as TALA depends on d2.
|
2022-11-21 04:37:06 +00:00
|
|
|
if command -v d2plugin-tala >/dev/null; then
|
|
|
|
|
INSTALLED_VERSION="$(d2plugin-tala --version)"
|
2022-12-01 23:08:19 +00:00
|
|
|
header "uninstalling tala-$INSTALLED_VERSION"
|
2022-11-21 04:37:06 +00:00
|
|
|
case $METHOD in
|
|
|
|
|
standalone) uninstall_tala_standalone ;;
|
|
|
|
|
homebrew) uninstall_tala_brew ;;
|
|
|
|
|
esac
|
|
|
|
|
elif [ "${TALA-}" ]; then
|
|
|
|
|
warn "no version of tala installed"
|
2022-11-14 12:02:23 +00:00
|
|
|
fi
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
if ! command -v d2 >/dev/null; then
|
|
|
|
|
warn "no version of d2 installed"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
INSTALLED_VERSION="$(d2 --version)"
|
2022-12-01 23:08:19 +00:00
|
|
|
header "uninstalling d2-$INSTALLED_VERSION"
|
2022-11-20 13:19:39 +00:00
|
|
|
case $METHOD in
|
|
|
|
|
standalone) uninstall_d2_standalone ;;
|
|
|
|
|
homebrew) uninstall_d2_brew ;;
|
|
|
|
|
esac
|
2022-11-14 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
uninstall_d2_standalone() {
|
2022-11-14 12:10:40 +00:00
|
|
|
log "uninstalling standalone release of d2-$INSTALLED_VERSION"
|
2022-11-14 10:48:47 +00:00
|
|
|
|
2022-11-14 11:40:35 +00:00
|
|
|
if [ ! -e "$INSTALL_DIR/d2-$INSTALLED_VERSION" ]; then
|
2022-11-14 22:46:52 +00:00
|
|
|
warn "missing standalone install release directory $INSTALL_DIR/d2-$INSTALLED_VERSION"
|
2022-11-17 19:38:21 +00:00
|
|
|
warn "d2 must have been installed via some other installation method."
|
2022-11-14 10:48:47 +00:00
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
ensure_prefix_sh_c
|
2022-11-14 11:40:35 +00:00
|
|
|
"$sh_c" sh -c "'cd \"$INSTALL_DIR/d2-$INSTALLED_VERSION\" && make uninstall PREFIX=\"$PREFIX\"'"
|
|
|
|
|
"$sh_c" rm -rf "$INSTALL_DIR/d2-$INSTALLED_VERSION"
|
2022-11-14 10:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
uninstall_d2_brew() {
|
|
|
|
|
sh_c brew remove d2
|
2022-11-14 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
uninstall_tala_standalone() {
|
2022-11-14 12:33:22 +00:00
|
|
|
log "uninstalling standalone release tala-$INSTALLED_VERSION"
|
2022-11-14 12:02:23 +00:00
|
|
|
|
2022-11-14 12:33:22 +00:00
|
|
|
if [ ! -e "$INSTALL_DIR/tala-$INSTALLED_VERSION" ]; then
|
2022-11-14 22:46:52 +00:00
|
|
|
warn "missing standalone install release directory $INSTALL_DIR/tala-$INSTALLED_VERSION"
|
2022-11-17 19:38:21 +00:00
|
|
|
warn "tala must have been installed via some other installation method."
|
2022-11-14 12:02:23 +00:00
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
2022-12-06 05:51:46 +00:00
|
|
|
ensure_prefix_sh_c
|
2022-11-14 12:33:22 +00:00
|
|
|
"$sh_c" sh -c "'cd \"$INSTALL_DIR/tala-$INSTALLED_VERSION\" && make uninstall PREFIX=\"$PREFIX\"'"
|
|
|
|
|
"$sh_c" rm -rf "$INSTALL_DIR/tala-$INSTALLED_VERSION"
|
2022-11-14 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 13:19:39 +00:00
|
|
|
uninstall_tala_brew() {
|
|
|
|
|
sh_c brew remove tala
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 10:48:47 +00:00
|
|
|
cache_dir() {
|
|
|
|
|
if [ -n "${XDG_CACHE_HOME-}" ]; then
|
|
|
|
|
echo "$XDG_CACHE_HOME/d2/release"
|
|
|
|
|
elif [ -n "${HOME-}" ]; then
|
|
|
|
|
echo "$HOME/.cache/d2/release"
|
|
|
|
|
else
|
|
|
|
|
echo "/tmp/d2-cache/release"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 11:40:35 +00:00
|
|
|
fetch_release_info() {
|
|
|
|
|
if [ -n "${RELEASE_INFO-}" ]; then
|
2022-11-14 10:48:47 +00:00
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
2022-11-14 18:26:37 +00:00
|
|
|
log "fetching info on $VERSION version of $REPO"
|
2022-12-06 06:21:06 +00:00
|
|
|
RELEASE_INFO=$(mktempd)/release-info.json
|
2022-11-14 11:40:35 +00:00
|
|
|
if [ "$VERSION" = latest ]; then
|
|
|
|
|
release_info_url="https://api.github.com/repos/$REPO/releases/$VERSION"
|
2022-11-14 10:48:47 +00:00
|
|
|
else
|
2022-11-14 11:40:35 +00:00
|
|
|
release_info_url="https://api.github.com/repos/$REPO/releases/tags/$VERSION"
|
2022-11-14 10:48:47 +00:00
|
|
|
fi
|
2022-12-06 12:25:02 +00:00
|
|
|
DRY_RUN= fetch_gh "$release_info_url" "$RELEASE_INFO" \
|
2022-11-14 10:48:47 +00:00
|
|
|
'application/json'
|
2022-11-14 11:40:35 +00:00
|
|
|
VERSION=$(cat "$RELEASE_INFO" | grep -m1 tag_name | sed 's/^.*: "\(.*\)",$/\1/g')
|
2022-11-14 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curl_gh() {
|
|
|
|
|
sh_c curl -fL ${GITHUB_TOKEN+"-H \"Authorization: Bearer \$GITHUB_TOKEN\""} "$@"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetch_gh() {
|
|
|
|
|
url=$1
|
|
|
|
|
file=$2
|
|
|
|
|
accept=$3
|
|
|
|
|
|
|
|
|
|
if [ -e "$file" ]; then
|
|
|
|
|
log "reusing $file"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
curl_gh -#o "$file.inprogress" -C- -H "'Accept: $accept'" "$url"
|
|
|
|
|
sh_c mv "$file.inprogress" "$file"
|
2022-11-14 07:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-01 20:59:53 +00:00
|
|
|
# The main function does more than provide organization. It provides robustness in that if
|
|
|
|
|
# the install script was to only partial download into sh, sh will not execute it because
|
|
|
|
|
# main is not invoked until the very last byte.
|
2022-11-14 07:20:30 +00:00
|
|
|
main "$@"
|