/
template.go
50 lines (43 loc) · 1.23 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
package template
import (
"bytes"
"encoding/base64"
"encoding/xml"
"strconv"
"strings"
"text/template"
"github.com/ghodss/yaml"
"github.com/openshift/openshift-azure/pkg/util/tls"
)
func Template(name, tmpl string, f template.FuncMap, data interface{}) ([]byte, error) {
t, err := template.New(name).Funcs(template.FuncMap{
"CertAsBytes": tls.CertAsBytes,
"CertChainAsBytes": tls.CertChainAsBytes,
"PrivateKeyAsBytes": tls.PrivateKeyAsBytes,
"PublicKeyAsBytes": tls.PublicKeyAsBytes,
"YamlMarshal": yaml.Marshal,
"Base64Encode": base64.StdEncoding.EncodeToString,
"String": func(b []byte) string { return string(b) },
"quote": strconv.Quote,
"ImageOnly": func(s string) string { return strings.Split(s, ":")[0] },
"escape": func(b string) string {
replacer := strings.NewReplacer("$", "\\$")
return replacer.Replace(b)
},
"StringsJoin": strings.Join,
"XMLEscape": func(s string) (string, error) {
var b bytes.Buffer
err := xml.EscapeText(&b, []byte(s))
return b.String(), err
},
}).Funcs(f).Parse(tmpl)
if err != nil {
return nil, err
}
b := &bytes.Buffer{}
err = t.Execute(b, data)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}