-
Notifications
You must be signed in to change notification settings - Fork 4
/
template.go
34 lines (29 loc) · 840 Bytes
/
template.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
package template
import (
"bytes"
"io/ioutil"
"text/template"
"github.com/Masterminds/sprig"
"github.com/pkg/errors"
)
// ParseFile validates and parses passed as argument template file
func ParseFile(filename string, obj interface{}) ([]byte, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return ParseString(string(data), obj)
}
// ParseString validates and parses passed as argument template
func ParseString(strtmpl string, obj interface{}) ([]byte, error) {
var buf bytes.Buffer
tmpl, err := template.New("template").Funcs(sprig.TxtFuncMap()).Parse(strtmpl)
if err != nil {
return nil, errors.Wrap(err, "error when parsing template")
}
err = tmpl.Execute(&buf, obj)
if err != nil {
return nil, errors.Wrap(err, "error when executing template")
}
return buf.Bytes(), nil
}