chore(repo): renamed gonf subfolder to something more traditional in go land
This commit is contained in:
parent
3b9af43738
commit
560becfd32
14 changed files with 2 additions and 2 deletions
44
pkg/templates.go
Normal file
44
pkg/templates.go
Normal file
|
@ -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()[:])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue