-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.go
94 lines (73 loc) · 2.12 KB
/
functions.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
htmlTemplate "html/template"
"io"
"log"
"os"
"path"
textTemplate "text/template"
sprig "github.com/Masterminds/sprig/v3"
shellquote "github.com/kballard/go-shellquote"
yaml "gopkg.in/yaml.v2"
)
// FuncMap is the same as implemented in text/template and html/template.
type FuncMap map[string]interface{}
func buildFuncMap(dataFile *os.File) (FuncMap, error) {
var err error
funcMap := make(FuncMap)
funcMap["uid"] = os.Getuid
funcMap["gid"] = os.Getgid
funcMap["euid"] = os.Geteuid
funcMap["egid"] = os.Getegid
funcMap["pwd"] = os.Getwd
funcMap["hostname"] = os.Hostname
funcMap["data"], err = dataFunc(dataFile)
if err != nil {
return nil, err
}
funcMap["shellquote"] = shellquote.Join
return funcMap, nil
}
func dataFunc(dataFile *os.File) (func() FuncMap, error) {
var dataFunctionMap map[string]interface{}
if dataFile != nil {
defer func() {
if err := dataFile.Close(); err != nil {
log.Printf("unable to close data file: %s", err)
}
}()
if err := yaml.NewDecoder(dataFile).Decode(&dataFunctionMap); err != nil {
return nil, fmt.Errorf("unable to parse data file: %w", err)
}
}
return func() FuncMap { return dataFunctionMap }, nil
}
func doTextTemplate(file string, funcMap FuncMap, emitter io.Writer) error {
template := textTemplate.
New(path.Base(file)).
Funcs(sprig.TxtFuncMap()).
Funcs(textTemplate.FuncMap(funcMap)).
Option("missingkey=zero")
if _, err := template.ParseFiles(file); err != nil {
return fmt.Errorf("failed to parse: %w", err)
}
if err := template.Execute(emitter, struct{}{}); err != nil {
return fmt.Errorf("unable to run your template: %w", err)
}
return nil
}
func doHTMLTemplate(file string, funcMap FuncMap, emitter io.Writer) error {
template := htmlTemplate.
New(path.Base(file)).
Funcs(sprig.FuncMap()).
Funcs(htmlTemplate.FuncMap(funcMap)).
Option("missingkey=zero")
if _, err := template.ParseFiles(file); err != nil {
return fmt.Errorf("failed to parse: %w", err)
}
if err := template.Execute(emitter, struct{}{}); err != nil {
return fmt.Errorf("unable to run your template: %w", err)
}
return nil
}