Merge pull request #119 from terrastruct/ci

ci: Fix
This commit is contained in:
Anmol Sethi 2022-11-20 20:54:16 -05:00 committed by GitHub
commit 1ae03fe8e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 319 additions and 120 deletions

View file

@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: with:
submodules: recursive submodules: recursive
- run: TERM=xterm-256color ./make.sh assert-linear - run: COLOR=1 ./make.sh assert-linear
env: env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
@ -25,7 +25,21 @@ jobs:
with: with:
go-version-file: ./go.mod go-version-file: ./go.mod
cache: true cache: true
- run: TERM=xterm-256color ./make.sh fmt - run: COLOR=1 ./make.sh fmt
env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
gen:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: actions/setup-go@v3
with:
go-version-file: ./go.mod
cache: true
- run: COLOR=1 ./make.sh gen
env: env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
@ -39,7 +53,7 @@ jobs:
with: with:
go-version-file: ./go.mod go-version-file: ./go.mod
cache: true cache: true
- run: TERM=xterm-256color ./make.sh lint - run: COLOR=1 ./make.sh lint
env: env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
@ -53,7 +67,7 @@ jobs:
with: with:
go-version-file: ./go.mod go-version-file: ./go.mod
cache: true cache: true
- run: TERM=xterm-256color ./make.sh build - run: COLOR=1 ./make.sh build
env: env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
@ -67,7 +81,7 @@ jobs:
with: with:
go-version-file: ./go.mod go-version-file: ./go.mod
cache: true cache: true
- run: TERM=xterm-256color ./make.sh test - run: COLOR=1 ./make.sh test
env: env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
@ -86,7 +100,7 @@ jobs:
with: with:
go-version-file: ./go.mod go-version-file: ./go.mod
cache: true cache: true
- run: TERM=xterm-256color ./make.sh race - run: COLOR=1 ./make.sh race
env: env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}

View file

@ -20,7 +20,7 @@ jobs:
with: with:
go-version-file: ./go.mod go-version-file: ./go.mod
cache: true cache: true
- run: CI_ALL=1 TERM=xterm-256color ./make.sh - run: CI_ALL=1 COLOR=1 ./make.sh
env: env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}

View file

@ -1,7 +1,7 @@
.POSIX: .POSIX:
.PHONY: all .PHONY: all
all: fmt lint build test all: fmt gen lint build test
ifdef CI ifdef CI
all: assert-linear all: assert-linear
endif endif
@ -9,6 +9,9 @@ endif
.PHONY: fmt .PHONY: fmt
fmt: fmt:
prefix "$@" ./ci/sub/fmt/make.sh prefix "$@" ./ci/sub/fmt/make.sh
.PHONY: gen
gen:
prefix "$@" ./ci/gen.sh
.PHONY: lint .PHONY: lint
lint: lint:
prefix "$@" go vet --composites=false ./... prefix "$@" go vet --composites=false ./...

11
ci/gen.sh Executable file
View file

@ -0,0 +1,11 @@
#!/bin/sh
set -eu
cd -- "$(dirname "$0")/.."
. ./ci/sub/lib.sh
./ci/release/gen_install.sh
./ci/release/gen_template_lib.sh
if [ -n "${CI-}" ]; then
git_assert_clean
fi

View file

@ -86,8 +86,7 @@ EOF
main() { main() {
METHOD=standalone METHOD=standalone
while :; do while flag_parse "$@"; do
flag_parse "$@"
case "$FLAG" in case "$FLAG" in
h|help) h|help)
help help
@ -129,15 +128,12 @@ main() {
flag_noarg && shift "$FLAGSHIFT" flag_noarg && shift "$FLAGSHIFT"
UNINSTALL=1 UNINSTALL=1
;; ;;
'')
shift "$FLAGSHIFT"
break
;;
*) *)
flag_errusage "unrecognized flag $FLAGRAW" flag_errusage "unrecognized flag $FLAGRAW"
;; ;;
esac esac
done done
shift "$FLAGSHIFT"
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
flag_errusage "no arguments are accepted" flag_errusage "no arguments are accepted"
@ -176,7 +172,7 @@ install() {
TALA_VERSION="$( install_tala && echo "$VERSION" )" TALA_VERSION="$( install_tala && echo "$VERSION" )"
fi fi
COLOR=2 header success FGCOLOR=2 header success
log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX" log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
if [ -n "${TALA-}" ]; then if [ -n "${TALA-}" ]; then
log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX" log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX"

View file

@ -42,8 +42,7 @@ EOF
} }
main() { main() {
while :; do while flag_parse "$@"; do
flag_parse "$@"
case "$FLAG" in case "$FLAG" in
h|help) h|help)
help help
@ -78,16 +77,12 @@ main() {
flag_noarg && shift "$FLAGSHIFT" flag_noarg && shift "$FLAGSHIFT"
LOCKFILE_FORCE=1 LOCKFILE_FORCE=1
;; ;;
'')
shift "$FLAGSHIFT"
break
;;
*) *)
flag_errusage "unrecognized flag $FLAGRAW" flag_errusage "unrecognized flag $FLAGRAW"
;; ;;
esac esac
done done
shift "$FLAGSHIFT"
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
flag_errusage "no arguments are accepted" flag_errusage "no arguments are accepted"
fi fi
@ -169,16 +164,16 @@ build_local() {
build_remote_macos() { build_remote_macos() {
sh_c lockfile_ssh "$REMOTE_HOST" .d2-build-lock sh_c lockfile_ssh "$REMOTE_HOST" .d2-build-lock
trap unlockfile_ssh EXIT
sh_c ssh "$REMOTE_HOST" mkdir -p src sh_c ssh "$REMOTE_HOST" mkdir -p src
sh_c rsync --archive --human-readable --delete ./ "$REMOTE_HOST:src/d2/" sh_c rsync --archive --human-readable --delete ./ "$REMOTE_HOST:src/d2/"
sh_c ssh "$REMOTE_HOST" "DRY_RUN=${DRY_RUN-} \ sh_c ssh "$REMOTE_HOST" "COLOR=${COLOR-} \
TERM=${TERM-} \
DRY_RUN=${DRY_RUN-} \
HW_BUILD_DIR=$HW_BUILD_DIR \ HW_BUILD_DIR=$HW_BUILD_DIR \
VERSION=$VERSION \ VERSION=$VERSION \
OS=$OS \ OS=$OS \
ARCH=$ARCH \ ARCH=$ARCH \
ARCHIVE=$ARCHIVE \ ARCHIVE=$ARCHIVE \
TERM=$TERM \
PATH=\\\"/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin\\\${PATH+:\\\$PATH}\\\" \ PATH=\\\"/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin\\\${PATH+:\\\$PATH}\\\" \
./src/d2/ci/release/_build.sh" ./src/d2/ci/release/_build.sh"
sh_c mkdir -p "$HW_BUILD_DIR" sh_c mkdir -p "$HW_BUILD_DIR"
@ -187,16 +182,16 @@ PATH=\\\"/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin\\\$
build_remote_linux() { build_remote_linux() {
sh_c lockfile_ssh "$REMOTE_HOST" .d2-build-lock sh_c lockfile_ssh "$REMOTE_HOST" .d2-build-lock
trap unlockfile_ssh EXIT
sh_c ssh "$REMOTE_HOST" mkdir -p src sh_c ssh "$REMOTE_HOST" mkdir -p src
sh_c rsync --archive --human-readable --delete ./ "$REMOTE_HOST:src/d2/" sh_c rsync --archive --human-readable --delete ./ "$REMOTE_HOST:src/d2/"
sh_c ssh "$REMOTE_HOST" "DRY_RUN=${DRY_RUN-} \ sh_c ssh "$REMOTE_HOST" "COLOR=${COLOR-} \
TERM=${TERM-} \
DRY_RUN=${DRY_RUN-} \
HW_BUILD_DIR=$HW_BUILD_DIR \ HW_BUILD_DIR=$HW_BUILD_DIR \
VERSION=$VERSION \ VERSION=$VERSION \
OS=$OS \ OS=$OS \
ARCH=$ARCH \ ARCH=$ARCH \
ARCHIVE=$ARCHIVE \ ARCHIVE=$ARCHIVE \
TERM=$TERM \
./src/d2/ci/release/build_docker.sh" ./src/d2/ci/release/build_docker.sh"
sh_c mkdir -p "$HW_BUILD_DIR" sh_c mkdir -p "$HW_BUILD_DIR"
sh_c rsync --archive --human-readable "$REMOTE_HOST:src/d2/$ARCHIVE" "$ARCHIVE" sh_c rsync --archive --human-readable "$REMOTE_HOST:src/d2/$ARCHIVE" "$ARCHIVE"

View file

@ -13,5 +13,4 @@ docker_run \
-e OS \ -e OS \
-e ARCH \ -e ARCH \
-e ARCHIVE \ -e ARCHIVE \
-e TERM \
"$tag" ./src/d2/ci/release/_build.sh "$tag" ./src/d2/ci/release/_build.sh

View file

@ -12,8 +12,7 @@ EOF
} }
main() { main() {
while :; do while flag_parse "$@"; do
flag_parse "$@"
case "$FLAG" in case "$FLAG" in
h|help) h|help)
help help
@ -27,15 +26,12 @@ main() {
flag_nonemptyarg && shift "$FLAGSHIFT" flag_nonemptyarg && shift "$FLAGSHIFT"
KEY_FILE=$FLAGARG KEY_FILE=$FLAGARG
;; ;;
'')
shift "$FLAGSHIFT"
break
;;
*) *)
flag_errusage "unrecognized flag $FLAGRAW" flag_errusage "unrecognized flag $FLAGRAW"
;; ;;
esac esac
done done
shift "$FLAGSHIFT"
if [ -z "${KEY_FILE-}" ]; then if [ -z "${KEY_FILE-}" ]; then
echoerr "-i is required" echoerr "-i is required"
exit 1 exit 1

View file

@ -12,8 +12,7 @@ EOF
} }
main() { main() {
while :; do while flag_parse "$@"; do
flag_parse "$@"
case "$FLAG" in case "$FLAG" in
h|help) h|help)
help help
@ -27,15 +26,12 @@ main() {
flag_noarg && shift "$FLAGSHIFT" flag_noarg && shift "$FLAGSHIFT"
SKIP_CREATE=1 SKIP_CREATE=1
;; ;;
'')
shift "$FLAGSHIFT"
break
;;
*) *)
flag_errusage "unrecognized flag $FLAGRAW" flag_errusage "unrecognized flag $FLAGRAW"
;; ;;
esac esac
done done
shift "$FLAGSHIFT"
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
flag_errusage "no arguments are accepted" flag_errusage "no arguments are accepted"
fi fi
@ -204,7 +200,7 @@ init_remote_hosts() {
header macos-arm64 header macos-arm64
REMOTE_HOST=$TSTRUCT_MACOS_ARM64_BUILDER init_remote_macos REMOTE_HOST=$TSTRUCT_MACOS_ARM64_BUILDER init_remote_macos
COLOR=2 header summary FGCOLOR=2 header summary
log "export TSTRUCT_LINUX_AMD64_BUILDER=$TSTRUCT_LINUX_AMD64_BUILDER" log "export TSTRUCT_LINUX_AMD64_BUILDER=$TSTRUCT_LINUX_AMD64_BUILDER"
log "export TSTRUCT_LINUX_ARM64_BUILDER=$TSTRUCT_LINUX_ARM64_BUILDER" log "export TSTRUCT_LINUX_ARM64_BUILDER=$TSTRUCT_LINUX_ARM64_BUILDER"
log "export TSTRUCT_MACOS_AMD64_BUILDER=$TSTRUCT_MACOS_AMD64_BUILDER" log "export TSTRUCT_MACOS_AMD64_BUILDER=$TSTRUCT_MACOS_AMD64_BUILDER"

View file

@ -12,8 +12,7 @@ EOF
} }
main() { main() {
while :; do while flag_parse "$@"; do
flag_parse "$@"
case "$FLAG" in case "$FLAG" in
h|help) h|help)
help help
@ -27,15 +26,12 @@ main() {
flag_reqarg && shift "$FLAGSHIFT" flag_reqarg && shift "$FLAGSHIFT"
JOBFILTER="$FLAGARG" JOBFILTER="$FLAGARG"
;; ;;
'')
shift "$FLAGSHIFT"
break
;;
*) *)
flag_errusage "unrecognized flag $FLAGRAW" flag_errusage "unrecognized flag $FLAGRAW"
;; ;;
esac esac
done done
shift "$FLAGSHIFT"
REMOTE_HOST=$TSTRUCT_LINUX_AMD64_BUILDER; runjob linux-amd64 ssh "$REMOTE_HOST" "$@" REMOTE_HOST=$TSTRUCT_LINUX_AMD64_BUILDER; runjob linux-amd64 ssh "$REMOTE_HOST" "$@"
REMOTE_HOST=$TSTRUCT_LINUX_ARM64_BUILDER; runjob linux-arm64 ssh "$REMOTE_HOST" "$@" REMOTE_HOST=$TSTRUCT_LINUX_ARM64_BUILDER; runjob linux-arm64 ssh "$REMOTE_HOST" "$@"

29
ci/release/gen_template_lib.sh Executable file
View file

@ -0,0 +1,29 @@
#!/bin/sh
set -eu
cd -- "$(dirname "$0")/../.."
. ./ci/sub/lib.sh
sh_c chmod +w ./ci/release/template/scripts/lib.sh
sh_c cat >./ci/release/template/scripts/lib.sh <<EOF
#!/bin/sh
# *************
# DO NOT EDIT
#
# lib.sh was bundled together from
#
# - ./ci/sub/lib/rand.sh
# - ./ci/sub/lib/log.sh
#
# Generated by ./ci/release/gen_template_lib.sh.
# *************
EOF
# sed removes the sourcing dependency lines as we're bundled everything into a single
# script.
sh_c cat \
./ci/sub/lib/rand.sh \
./ci/sub/lib/log.sh \
\| sed "-e'/^\. /d'" \>\>./ci/release/template/scripts/lib.sh
sh_c chmod -w ./ci/release/template/scripts/lib.sh

View file

@ -1,8 +1,21 @@
#!/bin/sh #!/bin/sh
if [ -n "${DEBUG-}" ]; then # *************
set -x # DO NOT EDIT
#
# lib.sh was bundled together from
#
# - ./ci/sub/lib/rand.sh
# - ./ci/sub/lib/log.sh
#
# Generated by ./ci/release/gen_template_lib.sh.
# *************
#!/bin/sh
if [ "${LIB_RAND-}" ]; then
return 0
fi fi
LIB_RAND=1
rand() { rand() {
seed="$1" seed="$1"
@ -14,15 +27,51 @@ rand() {
} }
pick() { pick() {
if ! command -v shuf >/dev/null || ! command -v md5sum >/dev/null; then
eval "_echo \"\$3\""
return
fi
seed="$1" seed="$1"
shift shift
i="$(rand "$seed" "1-$#")" i="$(rand "$seed" "1-$#")"
eval "_echo \"\$$i\"" eval "_echo \"\$$i\""
} }
#!/bin/sh
if [ "${LIB_LOG-}" ]; then
return 0
fi
LIB_LOG=1
if [ -n "${DEBUG-}" ]; then
set -x
fi
tput() { tput() {
if [ -n "$TERM" ]; then if should_color; then
command tput "$@" TERM=${TERM:-xterm-256color} command tput "$@"
fi
}
should_color() {
if [ -n "${COLOR-}" ]; then
if [ "$COLOR" = 0 -o "$COLOR" = false ]; then
_COLOR=
return 1
elif [ "$COLOR" = 1 -o "$COLOR" = true ]; then
_COLOR=1
return 0
else
printf '$COLOR must be 0, 1, false or true but got %s' "$COLOR" >&2
fi
fi
if [ -t 1 ]; then
_COLOR=1
return 0
else
_COLOR=
return 1
fi fi
} }
@ -59,14 +108,15 @@ printfp() {(
prefix="$1" prefix="$1"
shift shift
if [ -z "${COLOR:-}" ]; then if [ -z "${FGCOLOR-}" ]; then
COLOR="$(get_rand_color "$prefix")" FGCOLOR="$(get_rand_color "$prefix")"
fi fi
printf '%s' "$(setaf "$COLOR" "$prefix")" if [ $# -eq 0 ]; then
should_color || true
if [ $# -gt 0 ]; then printf '%s' $(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix")
printf ': ' else
printf "$@" should_color || true
printf '%s: %s\n' $(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix") "$(printf "$@")"
fi fi
)} )}
@ -74,13 +124,8 @@ catp() {
prefix="$1" prefix="$1"
shift shift
printfp "$prefix" should_color || true
printf ': ' sed "s/^/$(COLOR=${_COLOR-} printfp "$prefix" '')/"
read -r line
_echo "$line"
indent=$(repeat ' ' 2)
sed "s/^/$indent/"
} }
repeat() { repeat() {
@ -94,48 +139,144 @@ strlen() {
} }
echoerr() { echoerr() {
COLOR=1 echop err "$*" >&2 FGCOLOR=1 logp err "$*" | humanpath>&2
} }
caterr() { caterr() {
COLOR=1 catp err "$@" >&2 FGCOLOR=1 logpcat err "$@" | humanpath >&2
} }
printferr() { printferr() {
COLOR=1 printfp err "$@" >&2 FGCOLOR=1 logfp err "$@" | humanpath >&2
} }
logp() { logp() {
echop "$@" >&2 should_color >&2 || true
COLOR=${_COLOR-} echop "$@" | humanpath >&2
} }
logfp() { logfp() {
printfp "$@" >&2 should_color >&2 || true
COLOR=${_COLOR-} printfp "$@" | humanpath >&2
} }
logpcat() { logpcat() {
catp "$@" >&2 should_color >&2 || true
COLOR=${_COLOR-} catp "$@" | humanpath >&2
} }
log() { log() {
COLOR=5 logp log "$@" FGCOLOR=5 logp log "$@"
} }
logf() { logf() {
COLOR=5 logfp log "$@" FGCOLOR=5 logfp log "$@"
} }
logcat() { logcat() {
COLOR=5 catp log "$@" >&2 FGCOLOR=5 logpcat log "$@"
}
warn() {
FGCOLOR=3 logp warn "$@"
}
warnf() {
FGCOLOR=3 logfp warn "$@"
}
warncat() {
FGCOLOR=3 logpcat warn "$@"
} }
sh_c() { sh_c() {
COLOR=3 logp exec "$*" FGCOLOR=3 logp exec "$*"
if [ -z "${DRY_RUN-}" ]; then 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
This script needs to run the following command as root:
$*
Please install doas, sudo, or su.
EOF
return 1
fi fi
} }
header() { header() {
logp "/* $1 */" logp "/* $1 */"
} }
# 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="$(mktemp)"
set +e
"$@" >"$out" 2>&1
code="$?"
set -e
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="$(mktemp)"
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
}

2
ci/sub

@ -1 +1 @@
Subproject commit df51b90892737ebe9feca3dd982bcdfc7f684834 Subproject commit 8f9a24045546e76e39175ed148fdadd770c750ba

2
go.sum
View file

@ -769,6 +769,8 @@ honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
oss.terrastruct.com/cmdlog v0.0.0-20221116181457-07977d95ac37 h1:Xy1JKJHc4hcuwi57s0BvGUY16GjxTtBmLUybsuGDU7E= oss.terrastruct.com/cmdlog v0.0.0-20221116181457-07977d95ac37 h1:Xy1JKJHc4hcuwi57s0BvGUY16GjxTtBmLUybsuGDU7E=
oss.terrastruct.com/cmdlog v0.0.0-20221116181457-07977d95ac37 h1:Xy1JKJHc4hcuwi57s0BvGUY16GjxTtBmLUybsuGDU7E=
oss.terrastruct.com/cmdlog v0.0.0-20221116181457-07977d95ac37/go.mod h1:ROL3yxl2X+S3O+Rls00qdX6aMh+p1dF8IdxDRwDDpsg=
oss.terrastruct.com/cmdlog v0.0.0-20221116181457-07977d95ac37/go.mod h1:ROL3yxl2X+S3O+Rls00qdX6aMh+p1dF8IdxDRwDDpsg= oss.terrastruct.com/cmdlog v0.0.0-20221116181457-07977d95ac37/go.mod h1:ROL3yxl2X+S3O+Rls00qdX6aMh+p1dF8IdxDRwDDpsg=
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541 h1:I9B1O1IJ6spivIQxbFRZmbhAwVeLwrcQRR1JbYUOvrI= oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541 h1:I9B1O1IJ6spivIQxbFRZmbhAwVeLwrcQRR1JbYUOvrI=
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541/go.mod h1:ags2QDy/T6jr69hT6bpmAmhr2H98n9o8Atf3QlUJPiU= oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541/go.mod h1:ags2QDy/T6jr69hT6bpmAmhr2H98n9o8Atf3QlUJPiU=

View file

@ -54,8 +54,30 @@ if [ -n "${DEBUG-}" ]; then
fi fi
tput() { tput() {
if [ -n "$TERM" ]; then if should_color; then
command tput "$@" TERM=${TERM:-xterm-256color} command tput "$@"
fi
}
should_color() {
if [ -n "${COLOR-}" ]; then
if [ "$COLOR" = 0 -o "$COLOR" = false ]; then
_COLOR=
return 1
elif [ "$COLOR" = 1 -o "$COLOR" = true ]; then
_COLOR=1
return 0
else
printf '$COLOR must be 0, 1, false or true but got %s' "$COLOR" >&2
fi
fi
if [ -t 1 ]; then
_COLOR=1
return 0
else
_COLOR=
return 1
fi fi
} }
@ -92,14 +114,15 @@ printfp() {(
prefix="$1" prefix="$1"
shift shift
if [ -z "${COLOR:-}" ]; then if [ -z "${FGCOLOR-}" ]; then
COLOR="$(get_rand_color "$prefix")" FGCOLOR="$(get_rand_color "$prefix")"
fi fi
printf '%s' "$(setaf "$COLOR" "$prefix")" if [ $# -eq 0 ]; then
should_color || true
if [ $# -gt 0 ]; then printf '%s' $(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix")
printf ': ' else
printf "$@" should_color || true
printf '%s: %s\n' $(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix") "$(printf "$@")"
fi fi
)} )}
@ -107,7 +130,8 @@ catp() {
prefix="$1" prefix="$1"
shift shift
sed "s/^/$(printfp "$prefix" '')/" should_color || true
sed "s/^/$(COLOR=${_COLOR-} printfp "$prefix" '')/"
} }
repeat() { repeat() {
@ -121,51 +145,58 @@ strlen() {
} }
echoerr() { echoerr() {
COLOR=1 echop err "$*" | humanpath>&2 FGCOLOR=1 logp err "$*" | humanpath>&2
} }
caterr() { caterr() {
COLOR=1 catp err "$@" | humanpath >&2 FGCOLOR=1 logpcat err "$@" | humanpath >&2
} }
printferr() { printferr() {
COLOR=1 printfp err "$@" | humanpath >&2 FGCOLOR=1 logfp err "$@" | humanpath >&2
} }
logp() { logp() {
echop "$@" | humanpath >&2 should_color >&2 || true
COLOR=${_COLOR-} echop "$@" | humanpath >&2
} }
logfp() { logfp() {
printfp "$@" | humanpath >&2 should_color >&2 || true
COLOR=${_COLOR-} printfp "$@" | humanpath >&2
} }
logpcat() { logpcat() {
catp "$@" | humanpath >&2 should_color >&2 || true
COLOR=${_COLOR-} catp "$@" | humanpath >&2
} }
log() { log() {
COLOR=5 logp log "$@" FGCOLOR=5 logp log "$@"
} }
logf() { logf() {
COLOR=5 logfp log "$@" FGCOLOR=5 logfp log "$@"
} }
logcat() { logcat() {
COLOR=5 logpcat log "$@" FGCOLOR=5 logpcat log "$@"
} }
warn() { warn() {
COLOR=3 logp warn "$@" FGCOLOR=3 logp warn "$@"
} }
warnf() { warnf() {
COLOR=3 logfp warn "$@" FGCOLOR=3 logfp warn "$@"
}
warncat() {
FGCOLOR=3 logpcat warn "$@"
} }
sh_c() { sh_c() {
COLOR=3 logp exec "$*" FGCOLOR=3 logp exec "$*"
if [ -z "${DRY_RUN-}" ]; then if [ -z "${DRY_RUN-}" ]; then
eval "$@" eval "$@"
fi fi
@ -280,9 +311,8 @@ LIB_FLAG=1
# FLAGSHIFT contains the number by which the arguments should be shifted to # FLAGSHIFT contains the number by which the arguments should be shifted to
# start at the next flag/argument # start at the next flag/argument
# #
# After each call check $FLAG for the name of the parsed flag. # flag_parse exits with a non zero code when there are no more flags
# If empty, then no more flags are left. # to be parsed. Still, call shift "$FLAGSHIFT" in case there was a --
# Still, call shift "$FLAGSHIFT" in case there was a --
# #
# If the argument for the flag is optional, then use ${FLAGARG-} to access # 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. # the argument if one was passed. Use ${FLAGARG+x} = x to check if it was set.
@ -310,18 +340,15 @@ flag_parse() {
# Remove everything before first equal sign. # Remove everything before first equal sign.
FLAGARG="${1#*=}" FLAGARG="${1#*=}"
FLAGSHIFT=1 FLAGSHIFT=1
return 0
;; ;;
-) -)
FLAG=
FLAGRAW=
unset FLAGARG
FLAGSHIFT=0 FLAGSHIFT=0
return 1
;; ;;
--) --)
FLAG=
FLAGRAW=
unset FLAGARG
FLAGSHIFT=1 FLAGSHIFT=1
return 1
;; ;;
-*) -*)
# Remove leading hyphens. # Remove leading hyphens.
@ -343,15 +370,13 @@ flag_parse() {
;; ;;
esac esac
fi fi
return 0
;; ;;
*) *)
FLAG=
FLAGRAW=
unset FLAGARG
FLAGSHIFT=0 FLAGSHIFT=0
return 1
;; ;;
esac esac
return 0
} }
flag_reqarg() { flag_reqarg() {
@ -520,8 +545,7 @@ EOF
main() { main() {
METHOD=standalone METHOD=standalone
while :; do while flag_parse "$@"; do
flag_parse "$@"
case "$FLAG" in case "$FLAG" in
h|help) h|help)
help help
@ -563,15 +587,12 @@ main() {
flag_noarg && shift "$FLAGSHIFT" flag_noarg && shift "$FLAGSHIFT"
UNINSTALL=1 UNINSTALL=1
;; ;;
'')
shift "$FLAGSHIFT"
break
;;
*) *)
flag_errusage "unrecognized flag $FLAGRAW" flag_errusage "unrecognized flag $FLAGRAW"
;; ;;
esac esac
done done
shift "$FLAGSHIFT"
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
flag_errusage "no arguments are accepted" flag_errusage "no arguments are accepted"
@ -610,7 +631,7 @@ install() {
TALA_VERSION="$( install_tala && echo "$VERSION" )" TALA_VERSION="$( install_tala && echo "$VERSION" )"
fi fi
COLOR=2 header success FGCOLOR=2 header success
log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX" log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
if [ -n "${TALA-}" ]; then if [ -n "${TALA-}" ]; then
log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX" log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX"