aboutsummaryrefslogtreecommitdiff
path: root/lib/util.bash
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2023-03-26 11:13:34 +0200
committerPatrick Spek <p.spek@tyil.nl>2023-03-26 11:13:34 +0200
commit221548f141b703c644a2be354a18c338b42b1a5e (patch)
tree554efd140242e7f9da68a31ad825f4abde0a52a1 /lib/util.bash
parent20ff53212b13ac53a95b0a021ac326aee1bb8bab (diff)
Add bpt for file templating
Diffstat (limited to 'lib/util.bash')
-rw-r--r--lib/util.bash50
1 files changed, 44 insertions, 6 deletions
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() {