...
1
9
10
11
12
13
14
15
16
17
18
19
20 package config
21
22 import (
23 "encoding/json"
24 "os"
25
26 "github.com/noirbizarre/gonja"
27 "github.com/noirbizarre/gonja/exec"
28 "gitlab.hexacode.org/go-libs/hctypes"
29 )
30
31
32 type Template struct {
33 template *exec.Template
34 }
35
36
37
38
39
40 func NewTemplate(template string) (*Template, error) {
41 tmpl, err := gonja.FromString(template)
42 return &Template{
43 template: tmpl,
44 }, err
45 }
46
47
48
49
50
51 func NewTemplateFromFile(template_file string) (*Template, error) {
52 data, err := os.ReadFile(template_file)
53 if err != nil {
54 return nil, err
55 }
56
57 tmpl, err := gonja.FromBytes(data)
58 return &Template{
59 template: tmpl,
60 }, err
61 }
62
63
64
65
66
67 func (t *Template) Render(data hctypes.Dict) (string, error) {
68 raw := map[string]interface{}{}
69 err := json.Unmarshal([]byte(data.ToJson()), &raw)
70 if err != nil {
71 return "", err
72 }
73 return t.template.Execute(raw)
74 }
75
View as plain text