Files
dotfiles/render-template
T
steveandgitadmin 02a5e0df28 init: new dotfile configuration
Remove all org files using a new script to more config over, The old org
file configuaration were great but required org. This new version only
requires the scripts that is included in the repo.

The configuration are all done but have not ported the theming system
yet.

Signed-off-by: Steve Beaulac <steve@sagacity.ca>
Change-Id: I14cd7fe058a71c8ba9d6d35397dc8d7e6a6a6964
2026-03-02 13:26:21 -05:00

79 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage:
render_template.sh TEMPLATE --set key=value ... [-o output]
USAGE
}
if [[ $# -lt 1 ]]; then
usage >&2
exit 2
fi
template="$1"
shift
output=""
declare -A values
while [[ $# -gt 0 ]]; do
case "$1" in
--set)
shift
if [[ $# -eq 0 ]]; then
echo "--set requires key=value" >&2
exit 2
fi
if [[ "$1" != *=* ]]; then
echo "Invalid --set value (expected key=value): $1" >&2
exit 2
fi
key="${1%%=*}"
val="${1#*=}"
if [[ -z "$key" ]]; then
echo "Invalid --set value (empty key): $1" >&2
exit 2
fi
values["$key"]="$val"
shift
;;
-o|--output)
shift
if [[ $# -eq 0 ]]; then
echo "-o/--output requires a path" >&2
exit 2
fi
output="$1"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 2
;;
esac
done
if [[ ${#values[@]} -eq 0 ]]; then
echo "No values provided. Use a JSON file or --set key=value." >&2
exit 2
fi
rendered="$(cat "$template")"
for key in "${!values[@]}"; do
val="${values[$key]}"
rendered="$(printf '%s' "$rendered" | sed -E "s/\{\{[[:space:]]*$key[[:space:]]*\}\}/$val/g")"
done
if [[ -n "$output" ]]; then
printf '%s' "$rendered" > "$output"
else
printf '%s' "$rendered"
fi