-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.go
60 lines (48 loc) · 1.15 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
58
59
60
package main
import (
"bytes"
"os"
"strings"
"text/template"
sprig "github.com/Masterminds/sprig"
)
type templateData struct {
Arg map[string]string
Env map[string]string
}
func createTemplate() *template.Template {
tmpl := template.New("base")
tmpl.Funcs(sprig.TxtFuncMap())
tmpl.Option("missingkey=zero")
return tmpl
}
func parseContentAsTemplate(templateContent string, changesets []changeset) bytes.Buffer {
var content bytes.Buffer
data := generateTemplateData(changesets)
tmpl, err := createTemplate().Parse(templateContent)
if err != nil {
logFatalErrorAndExit(err, 1)
}
err = tmpl.Execute(&content, &data)
if err != nil {
logFatalErrorAndExit(err, 1)
}
return content
}
func generateTemplateData(changesets []changeset) templateData {
// init
var ret templateData
ret.Arg = make(map[string]string)
ret.Env = make(map[string]string)
// add changesets
for _, changeset := range changesets {
ret.Arg[changeset.SearchPlain] = changeset.Replace
}
// add env variables
for _, e := range os.Environ() {
split := strings.SplitN(e, "=", 2)
envKey, envValue := split[0], split[1]
ret.Env[envKey] = envValue
}
return ret
}