/
execute.go
41 lines (33 loc) 路 1007 Bytes
/
execute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package templates
import (
"bytes"
"text/template"
"github.com/Masterminds/sprig"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/pkg/errors"
)
var (
ErrTemplateParse = "failed to parse text/template during template.Execute"
ErrTemplateExec = "failed to execute template during exec()"
)
// Execute renders the template string into a buffer and includes
// some useful helper functions
func Execute(templateString string, data interface{}) (*bytes.Buffer, error) {
t, err := template.New("template").Funcs(sprig.TxtFuncMap()).Parse(templateString)
if err != nil {
return nil, errors.Wrap(err, ErrTemplateParse)
}
buffer := bytes.NewBuffer(nil)
if err := t.Execute(buffer, data); err != nil {
return nil, errors.Wrap(err, ErrTemplateExec)
}
return fmt(buffer)
}
func fmt(b *bytes.Buffer) (*bytes.Buffer, error) {
fmt := hclwrite.Format(b.Bytes())
if fmt == nil {
return nil, errors.New("formatted template returned empty")
}
buf := bytes.NewBuffer(fmt)
return buf, nil
}