forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomplate.go
87 lines (72 loc) · 1.92 KB
/
gomplate.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
package main
import (
"io"
"path/filepath"
"text/template"
"github.com/hairyhenderson/gomplate/data"
)
func (g *Gomplate) createTemplate() *template.Template {
return template.New("template").Funcs(g.funcMap).Option("missingkey=error")
}
// Gomplate -
type Gomplate struct {
funcMap template.FuncMap
leftDelim string
rightDelim string
}
// RunTemplate -
func (g *Gomplate) RunTemplate(text string, out io.Writer) error {
context := &Context{}
tmpl, err := g.createTemplate().Delims(g.leftDelim, g.rightDelim).Parse(text)
if err != nil {
return err
}
err = tmpl.Execute(out, context)
return err
}
// NewGomplate -
func NewGomplate(d *data.Data, leftDelim, rightDelim string) *Gomplate {
return &Gomplate{
leftDelim: leftDelim,
rightDelim: rightDelim,
funcMap: initFuncs(d),
}
}
func runTemplate(o *GomplateOpts) error {
defer runCleanupHooks()
d := data.NewData(o.dataSources, o.dataSourceHeaders)
addCleanupHook(d.Cleanup)
g := NewGomplate(d, o.lDelim, o.rDelim)
excludeList, err := executeCombinedGlob(o.excludeGlob)
if err != nil {
return err
}
if o.inputDir != "" {
return processInputDir(o.inputDir, o.outputDir, excludeList, g)
}
return processInputFiles(o.input, o.inputFiles, o.outputFiles, excludeList, g)
}
// Called from process.go ...
func renderTemplate(g *Gomplate, inString string, outPath string) error {
outFile, err := openOutFile(outPath)
if err != nil {
return err
}
// nolint: errcheck
defer outFile.Close()
err = g.RunTemplate(inString, outFile)
return err
}
// takes an array of glob strings and executes it as a whole,
// returning a merged list of globbed files
func executeCombinedGlob(globArray []string) ([]string, error) {
var combinedExcludes []string
for _, glob := range globArray {
excludeList, err := filepath.Glob(glob)
if err != nil {
return nil, err
}
combinedExcludes = append(combinedExcludes, excludeList...)
}
return combinedExcludes, nil
}