262 lines
4 KiB
Bash
262 lines
4 KiB
Bash
#!/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.
|
|
# *************
|
|
|
|
#!/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() {
|
|
if ! command -v shuf >/dev/null || ! command -v md5sum >/dev/null; then
|
|
eval "_echo \"\$3\""
|
|
return
|
|
fi
|
|
|
|
seed="$1"
|
|
shift
|
|
i="$(rand "$seed" "1-$#")"
|
|
eval "_echo \"\$$i\""
|
|
}
|
|
#!/bin/sh
|
|
if [ "${LIB_LOG-}" ]; then
|
|
return 0
|
|
fi
|
|
LIB_LOG=1
|
|
|
|
if [ -n "${DEBUG-}" ]; then
|
|
set -x
|
|
fi
|
|
|
|
export COLOR
|
|
if ! [ "${COLOR-}" = 0 -o "${COLOR-}" = false ]; then
|
|
if [ "${COLOR-}" = 1 -o "${COLOR-}" = true -o -t 1 ]; then
|
|
export _COLOR=1
|
|
fi
|
|
fi
|
|
|
|
tput() {
|
|
if [ "${COLOR-}" = 0 -o "${COLOR-}" = false ]; then
|
|
return 0
|
|
fi
|
|
|
|
if [ "${COLOR-}" = 1 -o "${COLOR-}" = true -o -t 1 ]; then
|
|
TERM=${TERM:-xterm-256color} 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 "${FGCOLOR-}" ]; then
|
|
FGCOLOR="$(get_rand_color "$prefix")"
|
|
fi
|
|
printf '%s' "$(setaf "$FGCOLOR" "$prefix")"
|
|
|
|
if [ $# -gt 0 ]; then
|
|
printf ': '
|
|
printf "$@"
|
|
fi
|
|
)}
|
|
|
|
catp() {
|
|
prefix="$1"
|
|
shift
|
|
|
|
sed "s/^/$(printfp "$prefix" '')/"
|
|
}
|
|
|
|
repeat() {
|
|
char="$1"
|
|
times="$2"
|
|
seq -s "$char" "$times" | tr -d '[:digit:]'
|
|
}
|
|
|
|
strlen() {
|
|
printf %s "$1" | wc -c
|
|
}
|
|
|
|
echoerr() {
|
|
FGCOLOR=1 echop err "$*" | humanpath>&2
|
|
}
|
|
|
|
caterr() {
|
|
FGCOLOR=1 catp err "$@" | humanpath >&2
|
|
}
|
|
|
|
printferr() {
|
|
FGCOLOR=1 printfp err "$@" | humanpath >&2
|
|
}
|
|
|
|
logp() {
|
|
echop "$@" | humanpath >&2
|
|
}
|
|
|
|
logfp() {
|
|
printfp "$@" | humanpath >&2
|
|
}
|
|
|
|
logpcat() {
|
|
catp "$@" | humanpath >&2
|
|
}
|
|
|
|
log() {
|
|
FGCOLOR=5 logp log "$@"
|
|
}
|
|
|
|
logf() {
|
|
FGCOLOR=5 logfp log "$@"
|
|
}
|
|
|
|
logcat() {
|
|
FGCOLOR=5 logpcat log "$@"
|
|
}
|
|
|
|
warn() {
|
|
FGCOLOR=3 logp warn "$@"
|
|
}
|
|
|
|
warnf() {
|
|
FGCOLOR=3 logfp warn "$@"
|
|
}
|
|
|
|
sh_c() {
|
|
FGCOLOR=3 logp exec "$*"
|
|
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
|
|
}
|
|
|
|
header() {
|
|
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
|
|
}
|