2022-11-12 22:19:09 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
2022-11-16 18:48:39 +00:00
|
|
|
# *************
|
|
|
|
|
# DO NOT EDIT
|
|
|
|
|
#
|
|
|
|
|
# lib.sh was bundled together from
|
|
|
|
|
#
|
|
|
|
|
# - ./ci/sub/lib/rand.sh
|
|
|
|
|
# - ./ci/sub/lib/log.sh
|
2022-12-01 20:26:32 +00:00
|
|
|
# - ./ci/sub/lib/release.sh
|
2022-11-16 18:48:39 +00:00
|
|
|
#
|
|
|
|
|
# Generated by ./ci/release/gen_template_lib.sh.
|
|
|
|
|
# *************
|
|
|
|
|
|
|
|
|
|
#!/bin/sh
|
|
|
|
|
if [ "${LIB_RAND-}" ]; then
|
|
|
|
|
return 0
|
2022-11-16 11:16:57 +00:00
|
|
|
fi
|
2022-11-16 18:48:39 +00:00
|
|
|
LIB_RAND=1
|
2022-11-16 11:16:57 +00:00
|
|
|
|
2022-12-02 01:29:40 +00:00
|
|
|
pick() {
|
2022-11-12 22:19:09 +00:00
|
|
|
seed="$1"
|
2022-12-02 01:29:40 +00:00
|
|
|
shift
|
2022-11-12 22:19:09 +00:00
|
|
|
|
|
|
|
|
seed_file="$(mktemp)"
|
2022-12-02 20:10:11 +00:00
|
|
|
echo "$seed" >"$seed_file"
|
2022-12-04 22:56:50 +00:00
|
|
|
# We add 16 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.
|
|
|
|
|
echo "================" >"$seed_file"
|
2022-11-16 18:48:39 +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-12 22:19:09 +00:00
|
|
|
}
|
2022-11-16 18:48:39 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
if [ "${LIB_LOG-}" ]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
LIB_LOG=1
|
|
|
|
|
|
|
|
|
|
if [ -n "${DEBUG-}" ]; then
|
|
|
|
|
set -x
|
|
|
|
|
fi
|
|
|
|
|
|
2022-11-12 22:19:09 +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-12 22:19:09 +00:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setaf() {
|
|
|
|
|
tput setaf "$1"
|
|
|
|
|
shift
|
|
|
|
|
printf '%s' "$*"
|
|
|
|
|
tput sgr0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_echo() {
|
|
|
|
|
printf '%s\n' "$*"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 1-6 are regular and 9-14 are bright.
|
2022-12-02 20:10:11 +00:00
|
|
|
get_rand_color() {
|
|
|
|
|
colors=""
|
|
|
|
|
ncolors=$(command tput colors)
|
|
|
|
|
if [ "$ncolors" -ge 8 ]; then
|
|
|
|
|
colors="$colors 1 2 3 4 5 6"
|
|
|
|
|
elif [ "$ncolors" -ge 16 ]; then
|
|
|
|
|
colors="$colors 9 10 11 12 13 14"
|
|
|
|
|
fi
|
|
|
|
|
pick "$*" $colors
|
2022-11-12 22:19:09 +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-11-16 18:48:39 +00:00
|
|
|
if [ -z "${FGCOLOR-}" ]; then
|
|
|
|
|
FGCOLOR="$(get_rand_color "$prefix")"
|
2022-11-12 22:19:09 +00:00
|
|
|
fi
|
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-01 14:49:48 +00:00
|
|
|
printf '%s' "$(COLOR=$__COLOR setaf "$FGCOLOR" "$prefix")"
|
2022-11-20 11:19:40 +00:00
|
|
|
else
|
2022-12-01 14:49:48 +00:00
|
|
|
printf '%s: %s\n' "$(COLOR=$__COLOR setaf "$FGCOLOR" "$prefix")" "$(printf "$@")"
|
2022-11-12 22:19:09 +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-12 22:19:09 +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-12 22:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
caterr() {
|
2022-11-29 23:25:26 +00:00
|
|
|
FGCOLOR=1 logpcat err "$@"
|
2022-11-12 22:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printferr() {
|
2022-11-29 23:25:26 +00:00
|
|
|
FGCOLOR=1 logfp err "$@"
|
2022-11-12 22:19:09 +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-12 22:19:09 +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-12 22:19:09 +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-12 22:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=5 logp log "$@"
|
2022-11-12 22:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logf() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=5 logfp log "$@"
|
2022-11-12 22:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logcat() {
|
2022-11-16 18:48:39 +00:00
|
|
|
FGCOLOR=5 logpcat log "$@"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
warn() {
|
|
|
|
|
FGCOLOR=3 logp warn "$@"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
warnf() {
|
|
|
|
|
FGCOLOR=3 logfp warn "$@"
|
2022-11-12 22:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-20 10:09:32 +00:00
|
|
|
warncat() {
|
|
|
|
|
FGCOLOR=3 logpcat warn "$@"
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-12 22:19:09 +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-16 18:48:39 +00:00
|
|
|
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
|
2022-11-12 22:19:09 +00:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header() {
|
2022-12-01 23:08:19 +00:00
|
|
|
FGCOLOR=${FGCOLOR:-4} logp "/* $1 */"
|
2022-11-12 22:19:09 +00:00
|
|
|
}
|
2022-11-16 18:48:39 +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-01 23:08:19 +00:00
|
|
|
FGCOLOR=${FGCOLOR:-3} logp "/**
|
2022-11-29 22:18:52 +00:00
|
|
|
$*
|
2022-11-20 13:19:39 +00:00
|
|
|
**/"
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 18:48:39 +00:00
|
|
|
# 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
|
|
|
|
|
}
|
2022-12-01 14:49:48 +00:00
|
|
|
|
|
|
|
|
capcode() {
|
|
|
|
|
set +e
|
|
|
|
|
"$@"
|
|
|
|
|
code=$?
|
|
|
|
|
set -e
|
|
|
|
|
}
|
2022-12-01 20:26:32 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
if [ "${LIB_RELEASE-}" ]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
LIB_RELEASE=1
|
|
|
|
|
|
|
|
|
|
goos() {
|
|
|
|
|
case $1 in
|
2022-12-02 20:10:11 +00:00
|
|
|
macos) echo darwin;;
|
|
|
|
|
*) echo $1;;
|
2022-12-01 20:26:32 +00:00
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os() {
|
|
|
|
|
uname=$(uname)
|
|
|
|
|
case $uname in
|
2022-12-02 20:10:11 +00:00
|
|
|
Linux) echo linux;;
|
|
|
|
|
Darwin) echo macos;;
|
|
|
|
|
FreeBSD) echo freebsd;;
|
|
|
|
|
CYGWIN_NT*|MINGW32_NT*) echo windows;;
|
|
|
|
|
*) echo "$uname";;
|
2022-12-01 20:26:32 +00:00
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arch() {
|
|
|
|
|
uname_m=$(uname -m)
|
|
|
|
|
case $uname_m in
|
2022-12-02 20:10:11 +00:00
|
|
|
aarch64) echo arm64;;
|
|
|
|
|
x86_64) echo amd64;;
|
|
|
|
|
*) echo "$uname_m";;
|
2022-12-01 20:26:32 +00:00
|
|
|
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() {
|
|
|
|
|
# The path has to exist for -w to succeed.
|
|
|
|
|
sh_c "mkdir -p '$1' 2>/dev/null" || true
|
|
|
|
|
if [ ! -w "$1" ]; then
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
}
|