forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
338 lines (298 loc) · 7.7 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package gomplate
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/hairyhenderson/gomplate/tmpl"
"github.com/hairyhenderson/gomplate/conv"
"github.com/hairyhenderson/gomplate/env"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/zealic/xignore"
)
// ignorefile name, like .gitignore
const gomplateignore = ".gomplateignore"
// for overriding in tests
var stdin io.ReadCloser = os.Stdin
var fs = afero.NewOsFs()
// Stdout allows overriding the writer to use when templates are written to stdout ("-").
var Stdout io.WriteCloser = os.Stdout
// tplate - models a gomplate template file...
type tplate struct {
name string
targetPath string
target io.Writer
contents string
mode os.FileMode
modeOverride bool
}
func addTmplFuncs(f template.FuncMap, root *template.Template, ctx interface{}) {
t := tmpl.New(root, ctx)
tns := func() *tmpl.Template { return t }
f["tmpl"] = tns
f["tpl"] = t.Inline
}
func (t *tplate) toGoTemplate(g *gomplate) (tmpl *template.Template, err error) {
if g.rootTemplate != nil {
tmpl = g.rootTemplate.New(t.name)
} else {
tmpl = template.New(t.name)
g.rootTemplate = tmpl
}
tmpl.Option("missingkey=error")
// the "tmpl" funcs get added here because they need access to the root template and context
addTmplFuncs(g.funcMap, g.rootTemplate, g.context)
tmpl.Funcs(g.funcMap)
tmpl.Delims(g.leftDelim, g.rightDelim)
_, err = tmpl.Parse(t.contents)
if err != nil {
return nil, err
}
for alias, path := range g.nestedTemplates {
// nolint: gosec
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
_, err = tmpl.New(alias).Parse(string(b))
if err != nil {
return nil, err
}
}
return tmpl, nil
}
// loadContents - reads the template in _once_ if it hasn't yet been read. Uses the name!
func (t *tplate) loadContents() (err error) {
if t.contents == "" {
t.contents, err = readInput(t.name)
}
return err
}
func (t *tplate) addTarget() (err error) {
if t.name == "<arg>" && t.targetPath == "" {
t.targetPath = "-"
}
if t.target == nil {
t.target, err = openOutFile(t.targetPath, t.mode, t.modeOverride)
}
return err
}
// gatherTemplates - gather and prepare input template(s) and output file(s) for rendering
// nolint: gocyclo
func gatherTemplates(o *Config, outFileNamer func(string) (string, error)) (templates []*tplate, err error) {
o.defaults()
mode, modeOverride, err := o.getMode()
if err != nil {
return nil, err
}
switch {
// the arg-provided input string gets a special name
case o.Input != "":
templates = []*tplate{{
name: "<arg>",
contents: o.Input,
mode: mode,
modeOverride: modeOverride,
targetPath: o.OutputFiles[0],
}}
case o.InputDir != "":
// input dirs presume output dirs are set too
templates, err = walkDir(o.InputDir, outFileNamer, o.ExcludeGlob, mode, modeOverride)
if err != nil {
return nil, err
}
case o.Input == "":
templates = make([]*tplate, len(o.InputFiles))
for i := range o.InputFiles {
templates[i], err = fileToTemplates(o.InputFiles[i], o.OutputFiles[i], mode, modeOverride)
if err != nil {
return nil, err
}
}
}
return processTemplates(templates)
}
func processTemplates(templates []*tplate) ([]*tplate, error) {
for _, t := range templates {
if err := t.loadContents(); err != nil {
return nil, err
}
if err := t.addTarget(); err != nil {
return nil, err
}
}
return templates, nil
}
// walkDir - given an input dir `dir` and an output dir `outDir`, and a list
// of .gomplateignore and exclude globs (if any), walk the input directory and create a list of
// tplate objects, and an error, if any.
func walkDir(dir string, outFileNamer func(string) (string, error), excludeGlob []string, mode os.FileMode, modeOverride bool) ([]*tplate, error) {
dir = filepath.Clean(dir)
dirStat, err := fs.Stat(dir)
if err != nil {
return nil, err
}
dirMode := dirStat.Mode()
templates := make([]*tplate, 0)
matcher := xignore.NewMatcher(fs)
matches, err := matcher.Matches(dir, &xignore.MatchesOptions{
Ignorefile: gomplateignore,
Nested: true, // allow nested ignorefile
AfterPatterns: excludeGlob,
})
if err != nil {
return nil, err
}
// Unmatched ignorefile rules's files
files := matches.UnmatchedFiles
for _, file := range files {
nextInPath := filepath.Join(dir, file)
nextOutPath, err := outFileNamer(file)
if err != nil {
return nil, err
}
fMode := mode
if mode == 0 {
stat, perr := fs.Stat(nextInPath)
if perr == nil {
fMode = stat.Mode()
} else {
fMode = dirMode
}
}
// Ensure file parent dirs
if err = fs.MkdirAll(filepath.Dir(nextOutPath), dirMode); err != nil {
return nil, err
}
templates = append(templates, &tplate{
name: nextInPath,
targetPath: nextOutPath,
mode: fMode,
modeOverride: modeOverride,
})
}
return templates, nil
}
func fileToTemplates(inFile, outFile string, mode os.FileMode, modeOverride bool) (*tplate, error) {
if inFile != "-" {
si, err := fs.Stat(inFile)
if err != nil {
return nil, err
}
if mode == 0 {
mode = si.Mode()
}
}
tmpl := &tplate{
name: inFile,
targetPath: outFile,
mode: mode,
modeOverride: modeOverride,
}
return tmpl, nil
}
func openOutFile(filename string, mode os.FileMode, modeOverride bool) (out io.WriteCloser, err error) {
if conv.ToBool(env.Getenv("GOMPLATE_SUPPRESS_EMPTY", "false")) {
out = newEmptySkipper(func() (io.WriteCloser, error) {
if filename == "-" {
return Stdout, nil
}
return createOutFile(filename, mode, modeOverride)
})
return out, nil
}
if filename == "-" {
return Stdout, nil
}
return createOutFile(filename, mode, modeOverride)
}
func createOutFile(filename string, mode os.FileMode, modeOverride bool) (out io.WriteCloser, err error) {
out, err = fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode.Perm())
if err != nil {
return out, err
}
if modeOverride {
err = fs.Chmod(filename, mode.Perm())
}
return out, err
}
func readInput(filename string) (string, error) {
var err error
var inFile io.ReadCloser
if filename == "-" {
inFile = stdin
} else {
inFile, err = fs.OpenFile(filename, os.O_RDONLY, 0)
if err != nil {
return "", fmt.Errorf("failed to open %s\n%v", filename, err)
}
// nolint: errcheck
defer inFile.Close()
}
bytes, err := ioutil.ReadAll(inFile)
if err != nil {
err = fmt.Errorf("read failed for %s\n%v", filename, err)
return "", err
}
return string(bytes), nil
}
// emptySkipper is a io.WriteCloser wrapper that will only start writing once a
// non-whitespace byte has been encountered. The writer must be provided by the
// `open` func
type emptySkipper struct {
open func() (io.WriteCloser, error)
// internal
w io.WriteCloser
buf *bytes.Buffer
nw bool
}
func newEmptySkipper(open func() (io.WriteCloser, error)) *emptySkipper {
return &emptySkipper{
w: nil,
buf: &bytes.Buffer{},
nw: false,
open: open,
}
}
func (f *emptySkipper) Write(p []byte) (n int, err error) {
if !f.nw {
if allWhitespace(p) {
// buffer the whitespace
return f.buf.Write(p)
}
// first time around, so open the writer
f.nw = true
f.w, err = f.open()
if err != nil {
return 0, err
}
if f.w == nil {
return 0, errors.New("nil writer returned by open")
}
// empty the buffer into the wrapped writer
_, err = f.buf.WriteTo(f.w)
if err != nil {
return 0, err
}
}
return f.w.Write(p)
}
func (f *emptySkipper) Close() error {
if f.w != nil {
return f.w.Close()
}
return nil
}
func allWhitespace(p []byte) bool {
for _, b := range p {
if b == ' ' || b == '\t' || b == '\n' || b == '\r' || b == '\v' {
continue
}
return false
}
return true
}