From 221548f141b703c644a2be354a18c338b42b1a5e Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 26 Mar 2023 11:13:34 +0200 Subject: Add bpt for file templating --- lib/util.bash | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) (limited to 'lib/util.bash') diff --git a/lib/util.bash b/lib/util.bash index 54ec562..0961c7e 100644 --- a/lib/util.bash +++ b/lib/util.bash @@ -10,6 +10,8 @@ . "$BASHTARD_LIBDIR/util/pkg.bash" # shellcheck source=lib/util/svc.bash . "$BASHTARD_LIBDIR/util/svc.bash" +# shellcheck source=lib/vendor/bpt.bash +. "$BASHTARD_LIBDIR/vendor/bpt.bash" # Change the working directory. In usage, this is the same as using cd, # however, it will make additional checks to ensure everything is going fine. @@ -113,17 +115,44 @@ fetch_http_wget() { file_template() { local file - local sedfile + local system - file="$(playbook_path "base")/share/$1" ; shift - sedfile="$(tmpfile)" + file="$1" ; shift - if [[ ! -f $file ]] + ext="${file##*.}" + path="$(playbook_path "base")/share/$file" + system="bashtard/file_template" + + if [[ -z "$ext" || "$ext" == "$file" ]] then - crit "bashtard/template" "Tried to render template from $file, but it doesn't exist" - return + debug "$system" "Missing extension for template $file, assuming satpl" + ext="satpl" + fi + + if [[ ! -f "$path" ]] + then + crit "$system" "No template found at $path" + return 1 fi + if [[ "$(type -t "file_template_$ext")" != "function" ]] + then + crit "$system" "Invalid extension for template $file ($ext)" + return 1 + fi + + "file_template_$ext" "$path" "$@" +} + +file_template_satpl() +{ + local file + local sedfile + + file="$1" ; shift + + sedfile="$(tmpfile)" + for kv in "$@" do debug "bashtard/template" "Adding $kv to sedfile at $sedfile" @@ -149,6 +178,15 @@ file_template() sed -f "$sedfile" "$file" } +file_template_bpt() +{ + local file + + file="$1" ; shift + + eval "$@ bpt.main ge \"$file\"" +} + # Check if the first argument given appears in the list of all following # arguments. in_args() { -- cgit v1.1