#!/bin/bash
#
# Run kustomize build and send the result through bat (if present), formatted as yaml
# include stderr output as comments ("# XXX")
#
outfile=$(mktemp)
errfile=$(mktemp)
trap 'rm -f $outfile $errfile' EXIT
kustomize build "${@:-.}" >"$outfile" 2>"$errfile"
text=$( sed -e 's/^/# /' "$errfile"; cat "$outfile" )
rm -f "$outfile" "$errfile"
trap - EXIT
if command -v batcat &>/dev/null; then
    batcat -l yaml <<<"$text"
elif command -v bat &>/dev/null; then
    bat -l yaml <<<"$text"
else
    less <<<"$text"
fi
