-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
template.go
187 lines (162 loc) · 4.52 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package templateutils
import (
"bytes"
"context"
"os"
"strings"
"text/template"
"emperror.dev/errors"
"github.com/Masterminds/sprig/v3"
"github.com/dustin/go-humanize"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/utils/generalutils"
)
const recursionMaxNums = 1000
func LoadAllHelpersContent(
ctx context.Context,
loadS3FileContent func(ctx context.Context, path string) (string, error),
items []*config.TargetHelperConfigItem,
pathList []string,
) (string, error) {
// Initialize template content
tplContent := ""
// Check if there is a list of config items
if len(items) != 0 {
// Loop over items
for _, item := range items {
// Load template content
tpl, err := loadHelperContent(
ctx,
loadS3FileContent,
item,
)
// Check error
if err != nil {
return "", err
}
// Concat
tplContent = tplContent + "\n" + tpl
}
} else {
// Load from local files
// Loop over local path
for _, item := range pathList {
// Load template content
tpl, err := LoadLocalFileContent(item)
// Check error
if err != nil {
return "", err
}
// Concat
tplContent = tplContent + "\n" + tpl
}
}
// Return
return tplContent, nil
}
func loadHelperContent(
ctx context.Context,
loadS3FileContent func(ctx context.Context, path string) (string, error),
item *config.TargetHelperConfigItem,
) (string, error) {
// Check if it is in bucket and if load from S3 function exists
if item.InBucket && loadS3FileContent != nil {
// Try to get file from bucket
return loadS3FileContent(ctx, item.Path)
}
// Not in bucket, need to load from FS
return LoadLocalFileContent(item.Path)
}
func LoadTemplateContent(
ctx context.Context,
loadS3FileContent func(ctx context.Context, path string) (string, error),
item *config.TargetTemplateConfigItem,
) (string, error) {
// Check if it is in bucket and if load from S3 function exists
if item.InBucket && loadS3FileContent != nil {
// Try to get file from bucket
return loadS3FileContent(ctx, item.Path)
}
// Not in bucket, need to load from FS
return LoadLocalFileContent(item.Path)
}
func LoadLocalFileContent(path string) (string, error) {
// Read file from file path
by, err := os.ReadFile(path)
// Check if error exists
if err != nil {
return "", errors.WithStack(err)
}
return string(by), nil
}
func ExecuteTemplate(tplString string, data interface{}) (*bytes.Buffer, error) {
// Create template
tmpl := template.New("template-string-loaded")
// Load template from string
tmpl, err := tmpl.
Funcs(sprig.TxtFuncMap()).
Funcs(s3ProxyFuncMap(tmpl)).
Parse(tplString)
// Check if error exists
if err != nil {
return nil, errors.WithStack(err)
}
// Generate template in buffer
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, data)
// Check if error exists
if err != nil {
return nil, errors.WithStack(err)
}
return buf, nil
}
func s3ProxyFuncMap(t *template.Template) template.FuncMap {
// Initialize includedNames
// That will help detect nested loop references
includedNames := make(map[string]int)
// Result
funcMap := map[string]interface{}{}
// Add human size function
funcMap["humanSize"] = func(fmt int64) string {
return humanize.Bytes(uint64(fmt))
}
// Add request URI function
funcMap["requestURI"] = generalutils.GetRequestURI
// Add request scheme function
funcMap["requestScheme"] = generalutils.GetRequestScheme
// Add request host function
funcMap["requestHost"] = generalutils.GetRequestHost
// Add the 'include' function here so we can close over t.
// Copied from Helm: https://github.com/helm/helm/blob/3d1bc72827e4edef273fb3d8d8ded2a25fa6f39d/pkg/engine/engine.go#L112
funcMap["include"] = func(name string, data interface{}) (string, error) {
// Initialize buffer
var buf strings.Builder
if v, ok := includedNames[name]; ok {
if v > recursionMaxNums {
return "", errors.Wrapf(errors.New("unable to execute template"), "rendering template has a nested reference name: %s", name)
}
includedNames[name]++
} else {
includedNames[name] = 1
}
err := t.ExecuteTemplate(&buf, name, data)
includedNames[name]--
return buf.String(), err
}
// Copied from Helm
funcMap["toYaml"] = toYAML
funcMap["toJson"] = toJSON
// Inspired from helm
funcMap["tpl"] = func(tpl string, data interface{}) (string, error) {
// Execute template
buf, err := ExecuteTemplate(tpl, data)
// Check error
if err != nil {
return "", err
}
// Default
return buf.String(), nil
}
// Return result
return template.FuncMap(funcMap)
}