-
Notifications
You must be signed in to change notification settings - Fork 0
/
templ.go
146 lines (122 loc) · 3.14 KB
/
templ.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package templ
import (
"bytes"
"io"
"io/fs"
"path/filepath"
"slices"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-billy/v5/util"
"github.com/graytonio/flagops/lib/config"
"github.com/graytonio/flagops/lib/destinations"
"github.com/open-feature/go-sdk/openfeature"
"github.com/sirupsen/logrus"
)
type TemplateEngine struct {
SourceFilesystem billy.Filesystem
Output destinations.Output
FlagProvider *openfeature.Client
files []string
path config.Path
funcMap map[string]any
}
// TODO Move to config
var ignoreFileExtentions = []string{
".tar",
".tar.gz",
".tgz",
".zip",
".gz",
".7z",
}
// Parses configured paths and available providers to create list of engine tasks to be executed
func CreateEngines(paths []config.Path, providers map[string]*openfeature.Client) ([]*TemplateEngine, error) {
engines := []*TemplateEngine{}
for _, path := range paths {
engine, err := NewTemplateEngine(path, providers[path.Env])
if err != nil {
return nil, err
}
engines = append(engines, engine)
}
return engines, nil
}
// Creates a new template engine with a source path, a destination path, and a feature flag provider
func NewTemplateEngine(path config.Path, provider *openfeature.Client) (*TemplateEngine, error) {
engine := &TemplateEngine{
SourceFilesystem: osfs.New("."),
FlagProvider: provider,
path: path,
}
err := engine.ScanFiles()
if err != nil {
return nil, err
}
output, err := destinations.NewOutput(path)
if err != nil {
return nil, err
}
engine.Output = output
funcMap := sprig.GenericFuncMap()
funcMap["env"] = engine.env
funcMap["toYaml"] = engine.toYAML
funcMap["fromYaml"] = engine.fromYAML
funcMap["fromYamlArray"] = engine.fromYAMLArray
engine.funcMap = funcMap
return engine, nil
}
// Can input path and create list of files to template
func (te *TemplateEngine) ScanFiles() error {
return util.Walk(te.SourceFilesystem, te.path.Path, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
logrus.WithField("file", path).Debug("Adding file to template engine")
te.files = append(te.files, path)
return nil
})
}
// Iterates each file in the engine and writes it to the destination
func (te *TemplateEngine) Execute() error {
err := te.Output.Init()
if err != nil {
return err
}
for _, file := range te.files {
err := te.executeFileTemplate(file)
if err != nil {
return err
}
}
return te.Output.Finalize()
}
func (te *TemplateEngine) executeFileTemplate(path string) error {
file, err := te.SourceFilesystem.Open(path)
if err != nil {
return err
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
return err
}
if slices.Contains(ignoreFileExtentions, filepath.Ext(path)) {
return te.Output.ExecuteFile(path, data)
}
templ, err := template.New(path).
Delims("[{", "}]").
Funcs(te.funcMap).
Parse(string(data))
if err != nil {
return err
}
buf := bytes.NewBuffer([]byte{})
err = templ.Execute(buf, nil)
if err != nil {
return err
}
return te.Output.ExecuteFile(path, buf.Bytes())
}