-
Notifications
You must be signed in to change notification settings - Fork 9
/
template.go
57 lines (51 loc) · 1.5 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package template
import (
"bytes"
"io"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/pluralsh/plural-cli/pkg/utils"
)
func GetFuncMap() template.FuncMap {
funcs := sprig.TxtFuncMap()
funcs["genAESKey"] = utils.GenAESKey
funcs["repoRoot"] = repoRoot
funcs["repoName"] = repoName
funcs["repoUrl"] = repoUrl
funcs["branchName"] = branchName
funcs["dumpConfig"] = dumpConfig
funcs["dumpAesKey"] = dumpAesKey
funcs["readLine"] = readLine
funcs["readPassword"] = readPassword
funcs["readLineDefault"] = readLineDefault
funcs["readFile"] = readFile
funcs["homeDir"] = homeDir
funcs["knownHosts"] = knownHosts
funcs["dedupe"] = dedupe
funcs["dedupeObj"] = dedupeObj
funcs["secret"] = secret
funcs["probe"] = probe
funcs["importValue"] = importValue
funcs["namespace"] = namespace
funcs["toYaml"] = toYaml
funcs["fileExists"] = fileExists
funcs["pathJoin"] = pathJoin
funcs["eabCredential"] = eabCredential
funcs["chartInstalled"] = chartInstalled
return funcs
}
func MakeTemplate(tmplate string) (*template.Template, error) {
return template.New("gotpl").Funcs(GetFuncMap()).Parse(tmplate)
}
func RenderTemplate(wr io.Writer, tmplate string, ctx map[string]interface{}) error {
tmpl, err := MakeTemplate(tmplate)
if err != nil {
return err
}
return tmpl.Execute(wr, map[string]interface{}{"Values": ctx})
}
func RenderString(tmplate string, ctx map[string]interface{}) (string, error) {
var buffer bytes.Buffer
err := RenderTemplate(&buffer, tmplate, ctx)
return buffer.String(), err
}