ci: Update gen_install and gen_template_lib

This commit is contained in:
Anmol Sethi 2022-12-05 22:15:48 -08:00
parent 6e52966b52
commit b90c37d4a0
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
5 changed files with 97 additions and 21 deletions

View file

@ -14,6 +14,7 @@ set -eu
# install.sh was bundled together from # install.sh was bundled together from
# #
# - ./ci/sub/lib/rand.sh # - ./ci/sub/lib/rand.sh
# - ./ci/sub/lib/temp.sh
# - ./ci/sub/lib/log.sh # - ./ci/sub/lib/log.sh
# - ./ci/sub/lib/flag.sh # - ./ci/sub/lib/flag.sh
# - ./ci/sub/lib/release.sh # - ./ci/sub/lib/release.sh
@ -30,6 +31,7 @@ EOF
# script. # script.
sh_c cat \ sh_c cat \
./ci/sub/lib/rand.sh \ ./ci/sub/lib/rand.sh \
./ci/sub/lib/temp.sh \
./ci/sub/lib/log.sh \ ./ci/sub/lib/log.sh \
./ci/sub/lib/flag.sh \ ./ci/sub/lib/flag.sh \
./ci/sub/lib/release.sh \ ./ci/sub/lib/release.sh \

View file

@ -13,6 +13,7 @@ sh_c cat >./ci/release/template/scripts/lib.sh <<EOF
# lib.sh was bundled together from # lib.sh was bundled together from
# #
# - ./ci/sub/lib/rand.sh # - ./ci/sub/lib/rand.sh
# - ./ci/sub/lib/temp.sh
# - ./ci/sub/lib/log.sh # - ./ci/sub/lib/log.sh
# - ./ci/sub/lib/release.sh # - ./ci/sub/lib/release.sh
# #
@ -25,6 +26,7 @@ EOF
# script. # script.
sh_c cat \ sh_c cat \
./ci/sub/lib/rand.sh \ ./ci/sub/lib/rand.sh \
./ci/sub/lib/temp.sh \
./ci/sub/lib/log.sh \ ./ci/sub/lib/log.sh \
./ci/sub/lib/release.sh \ ./ci/sub/lib/release.sh \
\| sed "-e'/^\. /d'" \>\>./ci/release/template/scripts/lib.sh \| sed "-e'/^\. /d'" \>\>./ci/release/template/scripts/lib.sh

View file

@ -6,6 +6,7 @@
# lib.sh was bundled together from # lib.sh was bundled together from
# #
# - ./ci/sub/lib/rand.sh # - ./ci/sub/lib/rand.sh
# - ./ci/sub/lib/temp.sh
# - ./ci/sub/lib/log.sh # - ./ci/sub/lib/log.sh
# - ./ci/sub/lib/release.sh # - ./ci/sub/lib/release.sh
# #
@ -22,7 +23,7 @@ pick() {
seed="$1" seed="$1"
shift shift
seed_file="$(mktemp)" seed_file="$(mktempd)/pickseed"
# We add 32 more bytes to the seed file for sufficient entropy. Otherwise both Cygwin's # 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 # and MinGW's sort for example complains about the lack of entropy on stderr and writes
@ -40,6 +41,44 @@ pick() {
| head -n1 | head -n1
} }
#!/bin/sh #!/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
trap temp_exittrap EXIT
}
temp_exittrap() {
if [ -n "${_TMPDIR-}" ]; then
rm -r "$_TMPDIR"
fi
}
temppath() {
ensure_tmpdir
while true; do
temppath=$_TMPDIR/$(</dev/urandom head -c8 | base64)
if [ ! -e "$temppath" ]; then
echo "$temppath"
return
fi
done
}
mktempd() {
tp=$(temppath)
mkdir -p "$tp"
echo "$tp"
}
#!/bin/sh
if [ "${LIB_LOG-}" ]; then if [ "${LIB_LOG-}" ]; then
return 0 return 0
fi fi
@ -220,7 +259,7 @@ sudo_sh_c() {
sh_c "su root -c '$*'" sh_c "su root -c '$*'"
else else
caterr <<EOF caterr <<EOF
This script needs to run the following command as root: Unable to run the following command as root:
$* $*
Please install doas, sudo, or su. Please install doas, sudo, or su.
EOF EOF
@ -250,11 +289,8 @@ humanpath() {
} }
hide() { hide() {
out="$(mktemp)" out="$(mktempd)/hideout"
set +e capcode "$@" >"$out" 2>&1
"$@" >"$out" 2>&1
code="$?"
set -e
if [ "$code" -eq 0 ]; then if [ "$code" -eq 0 ]; then
return return
fi fi
@ -272,7 +308,7 @@ echo_dur() {
sponge() { sponge() {
dst="$1" dst="$1"
tmp="$(mktemp)" tmp="$(mktempd)/sponge"
cat > "$tmp" cat > "$tmp"
cat "$tmp" > "$dst" cat "$tmp" > "$dst"
} }
@ -324,7 +360,7 @@ ensure_goos() {
ensure_os ensure_os
case "$OS" in case "$OS" in
macos) export GOOS=darwin;; macos) export GOOS=darwin;;
*) export GOOS=$1;; *) export GOOS=$OS;;
esac esac
} }
@ -334,7 +370,7 @@ ensure_goarch() {
fi fi
ensure_arch ensure_arch
case "$ARCH" in case "$ARCH" in
*) export GOARCH=$1;; *) export GOARCH=$ARCH;;
esac esac
} }

2
ci/sub

@ -1 +1 @@
Subproject commit 153c562ec591b7d32e265fabbd426a6b69e4fde8 Subproject commit ce1b7341c09338260fe2799aa5e1400f369f8d7e

View file

@ -7,6 +7,7 @@ set -eu
# install.sh was bundled together from # install.sh was bundled together from
# #
# - ./ci/sub/lib/rand.sh # - ./ci/sub/lib/rand.sh
# - ./ci/sub/lib/temp.sh
# - ./ci/sub/lib/log.sh # - ./ci/sub/lib/log.sh
# - ./ci/sub/lib/flag.sh # - ./ci/sub/lib/flag.sh
# - ./ci/sub/lib/release.sh # - ./ci/sub/lib/release.sh
@ -27,7 +28,7 @@ pick() {
seed="$1" seed="$1"
shift shift
seed_file="$(mktemp)" seed_file="$(mktempd)/pickseed"
# We add 32 more bytes to the seed file for sufficient entropy. Otherwise both Cygwin's # 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 # and MinGW's sort for example complains about the lack of entropy on stderr and writes
@ -45,6 +46,44 @@ pick() {
| head -n1 | head -n1
} }
#!/bin/sh #!/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
trap temp_exittrap EXIT
}
temp_exittrap() {
if [ -n "${_TMPDIR-}" ]; then
rm -r "$_TMPDIR"
fi
}
temppath() {
ensure_tmpdir
while true; do
temppath=$_TMPDIR/$(</dev/urandom head -c8 | base64)
if [ ! -e "$temppath" ]; then
echo "$temppath"
return
fi
done
}
mktempd() {
tp=$(temppath)
mkdir -p "$tp"
echo "$tp"
}
#!/bin/sh
if [ "${LIB_LOG-}" ]; then if [ "${LIB_LOG-}" ]; then
return 0 return 0
fi fi
@ -225,7 +264,7 @@ sudo_sh_c() {
sh_c "su root -c '$*'" sh_c "su root -c '$*'"
else else
caterr <<EOF caterr <<EOF
This script needs to run the following command as root: Unable to run the following command as root:
$* $*
Please install doas, sudo, or su. Please install doas, sudo, or su.
EOF EOF
@ -255,11 +294,8 @@ humanpath() {
} }
hide() { hide() {
out="$(mktemp)" out="$(mktempd)/hideout"
set +e capcode "$@" >"$out" 2>&1
"$@" >"$out" 2>&1
code="$?"
set -e
if [ "$code" -eq 0 ]; then if [ "$code" -eq 0 ]; then
return return
fi fi
@ -277,7 +313,7 @@ echo_dur() {
sponge() { sponge() {
dst="$1" dst="$1"
tmp="$(mktemp)" tmp="$(mktempd)/sponge"
cat > "$tmp" cat > "$tmp"
cat "$tmp" > "$dst" cat "$tmp" > "$dst"
} }
@ -460,7 +496,7 @@ ensure_goos() {
ensure_os ensure_os
case "$OS" in case "$OS" in
macos) export GOOS=darwin;; macos) export GOOS=darwin;;
*) export GOOS=$1;; *) export GOOS=$OS;;
esac esac
} }
@ -470,7 +506,7 @@ ensure_goarch() {
fi fi
ensure_arch ensure_arch
case "$ARCH" in case "$ARCH" in
*) export GOARCH=$1;; *) export GOARCH=$ARCH;;
esac esac
} }