forked from kylemcc/kube-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
38 lines (33 loc) · 972 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
35
36
37
38
package kubegen
import (
"bytes"
"path/filepath"
"text/template"
)
func newTemplate(name string) *template.Template {
return template.New(name).Funcs(Funcs)
}
// Executes a template located at path with the specified data
func execTemplateFile(path string, data interface{}) ([]byte, error) {
tmpl, err := newTemplate(filepath.Base(path)).ParseFiles(path)
if err != nil {
return nil, err
}
return execTemplate(tmpl, data)
}
// Executes a template string with the specified data
func execTemplateString(text string, data interface{}) ([]byte, error) {
tmpl, err := newTemplate("stdin").Parse(text)
if err != nil {
return nil, err
}
return execTemplate(tmpl, data)
}
// Helper for execTemplateFile and execTemplateString - actually executes the template
func execTemplate(tmpl *template.Template, data interface{}) ([]byte, error) {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, err
}
return buf.Bytes(), nil
}