diff --git a/ci/release/_build.sh b/ci/release/_build.sh index d30527b0c..db1b27de9 100755 --- a/ci/release/_build.sh +++ b/ci/release/_build.sh @@ -8,12 +8,12 @@ sh_c rsync --recursive --perms --delete \ --human-readable --copy-links ./ci/release/template/ "$HW_BUILD_DIR/" VERSION=$VERSION sh_c eval "'$HW_BUILD_DIR/README.md.sh'" \> "'$HW_BUILD_DIR/README.md'" sh_c rm -f "$HW_BUILD_DIR/README.md.sh" -sh_c find "$HW_BUILD_DIR" -exec touch {} \; +sh_c find "$HW_BUILD_DIR" -exec touch {} \\\; export GOOS=$(goos "$OS") export GOARCH="$ARCH" sh_c mkdir -p "$HW_BUILD_DIR/bin" -sh_c go build -ldflags "-X oss.terrastruct.com/d2/lib/version.Version=$VERSION" \ +sh_c go build -ldflags "'-X oss.terrastruct.com/d2/lib/version.Version=$VERSION'" \ -o "$HW_BUILD_DIR/bin/d2" ./cmd/d2 sh_c tar czf "$ARCHIVE" "$HW_BUILD_DIR" diff --git a/ci/release/_install.sh b/ci/release/_install.sh index 6f4f2d794..08611160e 100755 --- a/ci/release/_install.sh +++ b/ci/release/_install.sh @@ -1,5 +1,9 @@ #!/bin/sh set -eu -$LOG_SH -$RAND_SH +cd -- "$(dirname "$0")/../.." +. ./ci/sub/lib/log.sh +. ./ci/sub/lib/flag.sh +cd - + +doing installation here diff --git a/ci/release/build.sh b/ci/release/build.sh index 34570ed52..b9a05d92d 100755 --- a/ci/release/build.sh +++ b/ci/release/build.sh @@ -37,24 +37,21 @@ main() { return 0 ;; rebuild) - flag_noarg + flag_noarg && shift "$FLAGSHIFT" REBUILD=1 - shift "$FLAGSHIFT" ;; local) - flag_noarg + flag_noarg && shift "$FLAGSHIFT" LOCAL=1 - shift "$FLAGSHIFT" ;; dryrun) - flag_noarg + flag_noarg && shift "$FLAGSHIFT" DRYRUN=1 - shift "$FLAGSHIFT" ;; run) - flag_reqarg - JOB_FILTER="$FLAGARG" - shift "$FLAGSHIFT" + flag_reqarg && shift "$FLAGSHIFT" + JOBFILTER="$FLAGARG" + ;; '') shift "$FLAGSHIFT" break @@ -133,6 +130,7 @@ VERSION=$VERSION \ OS=$OS \ ARCH=$ARCH \ ARCHIVE=$ARCHIVE \ +TERM=$TERM \ ./src/d2/ci/release/build_docker.sh" sh_c rsync --archive --human-readable "$RHOST:src/d2/$ARCHIVE" "$ARCHIVE" } diff --git a/ci/release/build_docker.sh b/ci/release/build_docker.sh index 88aacac87..e9d331800 100755 --- a/ci/release/build_docker.sh +++ b/ci/release/build_docker.sh @@ -5,17 +5,13 @@ cd -- "$(dirname "$0")/../.." tag="$(sh_c docker build \ --build-arg GOVERSION="1.19.3.linux-$ARCH" \ - -qf ./ci/release/builders/Dockerfile ./ci/release/builders )" -sh_c docker run -it --rm \ - -v "$HOME:$HOME" \ - -u "$(id -u):$(id -g)" \ - -w "$HOME" \ - -e DRYRUN="${DRYRUN-}" \ - -e HW_BUILD_DIR="$HW_BUILD_DIR" \ - -e VERSION="$VERSION" \ - -e OS="$OS" \ - -e ARCH="$ARCH" \ - -e ARCHIVE="$ARCHIVE" \ - -e TERM="$TERM" \ - -e HOME="$HOME" \ + -qf ./ci/release/builders/Dockerfile ./ci/release/builders)" +docker_run \ + -e DRYRUN \ + -e HW_BUILD_DIR \ + -e VERSION \ + -e OS \ + -e ARCH \ + -e ARCHIVE \ + -e TERM \ "$tag" ./src/d2/ci/release/_build.sh diff --git a/ci/release/builders/ensure_aws.sh b/ci/release/builders/ensure_aws.sh index 5f2aede0e..b7d0a682f 100755 --- a/ci/release/builders/ensure_aws.sh +++ b/ci/release/builders/ensure_aws.sh @@ -21,9 +21,8 @@ main() { return 0 ;; dryrun) - flag_noarg + flag_noarg && shift "$FLAGSHIFT" DRYRUN=1 - shift "$FLAGSHIFT" ;; '') shift "$FLAGSHIFT" diff --git a/ci/release/gen_install.sh b/ci/release/gen_install.sh index 314b468d1..fd49041d4 100755 --- a/ci/release/gen_install.sh +++ b/ci/release/gen_install.sh @@ -21,6 +21,7 @@ set -eu # # Generated by ./ci/release/gen_install.sh. # ************* + EOF # sed removes the sourcing dependency lines as we're bundled everything into a single @@ -29,5 +30,6 @@ sh_c cat \ ./ci/sub/lib/rand.sh \ ./ci/sub/lib/log.sh \ ./ci/sub/lib/flag.sh \ - ./ci/release/_install.sh \ - | sed '/^\. /d' >> ./install.sh + \| sed "-e'/^\. /d'" \>\> ./install.sh +sh_c cat ./ci/release/_install.sh \ + \| sed -n "'/cd -- \"\$(dirname/,/cd -/!p'" \>\> install.sh diff --git a/ci/release/release.sh b/ci/release/release.sh index a87b7f37e..6f0aa5395 100755 --- a/ci/release/release.sh +++ b/ci/release/release.sh @@ -71,19 +71,16 @@ main() { return 0 ;; rebuild) - flag_noarg + flag_noarg && shift "$FLAGSHIFT" REBUILD=1 - shift "$FLAGSHIFT" ;; prerelease) - flag_noarg + flag_noarg && shift "$FLAGSHIFT" PRERELEASE=1 - shift "$FLAGSHIFT" ;; dryrun) - flag_noarg + flag_noarg && shift "$FLAGSHIFT" DRYRUN=1 - shift "$FLAGSHIFT" ;; '') shift "$FLAGSHIFT" diff --git a/ci/sub b/ci/sub index 5d23dec92..d064c179a 160000 --- a/ci/sub +++ b/ci/sub @@ -1 +1 @@ -Subproject commit 5d23dec92cee36692bc8c8cece74890cce9631e4 +Subproject commit d064c179a5c102c4f54cf63be058461b21b8e28a diff --git a/install.sh b/install.sh new file mode 100644 index 000000000..e8f5cf3c4 --- /dev/null +++ b/install.sh @@ -0,0 +1,350 @@ +#!/bin/sh +set -eu + +# ************* +# DO NOT EDIT +# +# install.sh was bundled together from +# +# - ./ci/sub/lib/rand.sh +# - ./ci/sub/lib/log.sh +# - ./ci/sub/lib/flag.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 + +rand() { + seed="$1" + range="$2" + + seed_file="$(mktemp)" + _echo "$seed" | md5sum > "$seed_file" + shuf -i "$range" -n 1 --random-source="$seed_file" +} + +pick() { + seed="$1" + shift + i="$(rand "$seed" "1-$#")" + eval "_echo \"\$$i\"" +} +#!/bin/sh +if [ "${LIB_LOG-}" ]; then + return 0 +fi +LIB_LOG=1 + +tput() { + if [ -n "$TERM" ]; then + command tput "$@" + fi +} + +setaf() { + tput setaf "$1" + shift + printf '%s' "$*" + tput sgr0 +} + +_echo() { + printf '%s\n' "$*" +} + +get_rand_color() { + # 1-6 are regular and 9-14 are bright. + # 1,2 and 9,10 are red and green but we use those for success and failure. + pick "$*" 3 4 5 6 11 12 13 14 +} + +echop() { + prefix="$1" + shift + + if [ "$#" -gt 0 ]; then + printfp "$prefix" "%s\n" "$*" + else + printfp "$prefix" + printf '\n' + fi +} + +printfp() {( + prefix="$1" + shift + + if [ -z "${COLOR:-}" ]; then + COLOR="$(get_rand_color "$prefix")" + fi + printf '%s' "$(setaf "$COLOR" "$prefix")" + + if [ $# -gt 0 ]; then + printf ': ' + printf "$@" + fi +)} + +catp() { + prefix="$1" + shift + + printfp "$prefix" + printf ': ' + read -r line + _echo "$line" + + indent=$(repeat ' ' 2) + sed "s/^/$indent/" +} + +repeat() { + char="$1" + times="$2" + seq -s "$char" "$times" | tr -d '[:digit:]' +} + +strlen() { + printf %s "$1" | wc -c +} + +echoerr() { + COLOR=1 echop err "$*" >&2 +} + +caterr() { + COLOR=1 catp err "$@" >&2 +} + +printferr() { + COLOR=1 printfp err "$@" >&2 +} + +logp() { + echop "$@" >&2 +} + +logfp() { + printfp "$@" >&2 +} + +logpcat() { + catp "$@" >&2 +} + +log() { + COLOR=5 logp log "$@" +} + +logf() { + COLOR=5 logfp log "$@" +} + +logcat() { + COLOR=5 catp log "$@" >&2 +} + +sh_c() { + COLOR=3 logp exec "$*" + if [ -z "${DRYRUN-}" ]; then + eval "$@" + fi +} + +header() { + logp "/* $1 */" +} + +hide() { + out="$(mktemp)" + set +e + "$@" >"$out" 2>&1 + code="$?" + set -e + if [ "$code" -eq 0 ]; then + return + fi + cat "$out" >&2 + exit "$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 +} +#!/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 +# +# After each call check $FLAG for the name of the parsed flag. +# If empty, then no more flags are left. +# 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%%=*}" + FLAGRAW="$FLAG" + # Remove leading hyphens. + FLAG="${FLAG#-}"; FLAG="${FLAG#-}" + # Remove everything before first equal sign. + FLAGARG="${1#*=}" + FLAGSHIFT=1 + ;; + -) + FLAG= + FLAGRAW= + unset FLAGARG + FLAGSHIFT=0 + ;; + --) + FLAG= + FLAGRAW= + unset FLAGARG + FLAGSHIFT=1 + ;; + -*) + # Remove leading hyphens. + FLAG="${1#-}"; FLAG="${FLAG#-}" + FLAGRAW=$1 + unset FLAGARG + FLAGSHIFT=1 + if [ $# -gt 1 ]; then + case "$2" in + -) + FLAGARG="$2" + FLAGSHIFT=2 + ;; + -*) + ;; + *) + FLAGARG="$2" + FLAGSHIFT=2 + ;; + esac + fi + ;; + *) + FLAG= + FLAGRAW= + unset FLAGARG + FLAGSHIFT=0 + ;; + esac + return 0 +} + +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 <