forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
41 lines (34 loc) · 1.06 KB
/
generator.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 generator
import (
"bytes"
"os"
"text/template"
)
var tmplCache = template.New("template")
func init() {
tmplCache = tmplCache.Funcs(template.FuncMap{"escapeString": escapeString})
tmplCache = template.Must(tmplCache.Parse(FilterCustomTagTemplate))
tmplCache = template.Must(tmplCache.Parse(clusterFilterSyslogTemplate))
tmplCache = template.Must(tmplCache.Parse(clusterOutputTemplate))
tmplCache = template.Must(tmplCache.Parse(projectFilterSyslogTemplate))
tmplCache = template.Must(tmplCache.Parse(projectOutputTemplate))
}
func GenerateConfigFile(outputPath, templateM, tempalteName string, conf map[string]interface{}) error {
tp, err := tmplCache.Parse(templateM)
if err != nil {
return err
}
output, err := os.Create(outputPath)
if err != nil {
return err
}
defer output.Close()
return tp.Execute(output, conf)
}
func GenerateConfig(tempalteName string, conf interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
if err := tmplCache.ExecuteTemplate(buf, tempalteName, conf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}