d2/ci/sub/bin/prefix
Alexander Wang 524c089a74 oss
Co-authored-by: Anmol Sethi <hi@nhooyr.io>
2022-11-03 06:54:49 -07:00

38 lines
962 B
Bash
Executable file

#!/bin/sh
set -eu
. "$(dirname "$0")/../lib.sh"
main() {
prefix="$1"
shift
COLOR="$(get_rand_color "$prefix")"
prefix="$(setaf "$COLOR" "$prefix")"
_echo "$prefix^:" "$*"
# We need to make sure we exit with a non zero exit if the command fails.
# /bin/sh does not support -o pipefail unfortunately.
fifo="$(mktemp -d)/fifo"
mkfifo "$fifo"
# We add the prefix to all lines and remove any lines about recursive make.
# We cannot silence these with -s which is unfortunate.
sed -e "s#^#$prefix: #" -e "/make\[.\]: warning: -j/d" "$fifo" &
exit_trap() {
code="$?"
end="$(awk 'BEGIN{srand(); print srand()}')"
dur="$((end - start))"
if [ "$code" -eq 0 ]; then
_echo "$prefix\$:" "$(setaf 2 success)" "($(echo_dur $dur))"
else
_echo "$prefix\$:" "$(setaf 1 failure)" "($(echo_dur $dur))"
fi
}
trap exit_trap EXIT
start="$(awk 'BEGIN{srand(); print srand()}')"
"$@" >"$fifo" 2>&1
}
main "$@"