summaryrefslogtreecommitdiff
path: root/pkg/templates.go
diff options
context:
space:
mode:
authorJulien Dessaux2024-03-07 00:54:35 +0100
committerJulien Dessaux2024-03-07 01:02:23 +0100
commit560becfd32dd7355547938f3c6229060dd395aab (patch)
tree42329f4e47c64349ae8a0205f867632ac52bcdc6 /pkg/templates.go
parentfeat(stdlib): began adding systemd services support (diff)
downloadgonf-560becfd32dd7355547938f3c6229060dd395aab.tar.gz
gonf-560becfd32dd7355547938f3c6229060dd395aab.tar.bz2
gonf-560becfd32dd7355547938f3c6229060dd395aab.zip
chore(repo): renamed gonf subfolder to something more traditional in go land
Diffstat (limited to 'pkg/templates.go')
-rw-r--r--pkg/templates.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/pkg/templates.go b/pkg/templates.go
new file mode 100644
index 0000000..0b7e11b
--- /dev/null
+++ b/pkg/templates.go
@@ -0,0 +1,44 @@
+package gonf
+
+import (
+ "bytes"
+ "log/slog"
+ "text/template"
+)
+
+// ----- Globals ---------------------------------------------------------------
+var templates *template.Template
+
+// ----- Init ------------------------------------------------------------------
+func init() {
+ templates = template.New("")
+ templates.Option("missingkey=error")
+ templates.Funcs(builtinTemplateFunctions)
+}
+
+// ----- Public ----------------------------------------------------------------
+type TemplateValue struct {
+ contents []byte
+ data string
+}
+
+func (t *TemplateValue) Bytes() []byte {
+ if t.contents == nil {
+ tpl := templates.New("")
+ if _, err := tpl.Parse(t.data); err != nil {
+ slog.Error("template", "step", "Parse", "data", t.data, "error", err)
+ return nil
+ }
+ var buff bytes.Buffer
+ if err := tpl.Execute(&buff, nil /* no data needed */); err != nil {
+ slog.Error("template", "step", "Execute", "data", t.data, "error", err)
+ return nil
+ }
+ t.contents = buff.Bytes()
+ }
+ return t.contents
+}
+
+func (t TemplateValue) String() string {
+ return string(t.Bytes()[:])
+}