-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
45 lines (41 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
package render
import (
"github.com/Masterminds/sprig/v3"
"github.com/zcloud-ws/secure-templates/pkg/config"
"github.com/zcloud-ws/secure-templates/pkg/connectors"
"io"
"os"
"path/filepath"
"text/template"
)
func funcMap(cfgOptions config.SecureTemplateConfigOptions, connector connectors.Connector) template.FuncMap {
funcMaps := template.FuncMap{}
for k, v := range sprig.FuncMap() {
funcMaps[k] = v
}
if connector.ConnectorType() == config.SecretEnginePrintKeys {
funcMaps["env"] = func(name string) any {
return connector.Secret(name, "")
}
} else {
funcMaps["env"] = RegisterEnvVar(cfgOptions)
}
funcMaps["secret"] = RegisterSecret(connector)
return funcMaps
}
func ParseFile(cfgOptions config.SecureTemplateConfigOptions, file *os.File, connector connectors.Connector, output io.Writer) error {
tpl, err := template.New(filepath.Base(file.Name())).
Funcs(funcMap(cfgOptions, connector)).
ParseFiles(file.Name())
if err != nil {
return err
}
return processTemplate(tpl, output)
}
func processTemplate(tpl *template.Template, output io.Writer) error {
err := tpl.Execute(output, nil)
if err != nil {
return err
}
return nil
}