summaryrefslogtreecommitdiff
path: root/gomplate.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2017-12-29 22:22:53 +0100
committerDave Henderson <dhenderson@gmail.com>2018-01-28 21:28:37 -0500
commit6aec3291409e3dce80eba8fa9bcab41ceaf826a1 (patch)
treeb74f7b9b908b5670eeeb7d3a44c1612bb33e306d /gomplate.go
parent869528ef5bfff1adc573637923515037288f0adc (diff)
Refactoring template processing
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'gomplate.go')
-rw-r--r--gomplate.go61
1 files changed, 14 insertions, 47 deletions
diff --git a/gomplate.go b/gomplate.go
index 6bb3243a..76b06260 100644
--- a/gomplate.go
+++ b/gomplate.go
@@ -2,16 +2,11 @@ package main
import (
"io"
- "path/filepath"
"text/template"
"github.com/hairyhenderson/gomplate/data"
)
-func (g *Gomplate) createTemplate(name string) *template.Template {
- return template.New(name).Funcs(g.funcMap).Option("missingkey=error")
-}
-
// Gomplate -
type Gomplate struct {
funcMap template.FuncMap
@@ -20,13 +15,19 @@ type Gomplate struct {
}
// RunTemplate -
-func (g *Gomplate) RunTemplate(in *input, out io.Writer) error {
+func (g *Gomplate) RunTemplate(t *tplate) error {
context := &Context{}
- tmpl, err := g.createTemplate(in.name).Delims(g.leftDelim, g.rightDelim).Parse(in.contents)
+ tmpl, err := t.toGoTemplate(g)
if err != nil {
return err
}
- err = tmpl.Execute(out, context)
+
+ switch t.target.(type) {
+ case io.Closer:
+ // nolint: errcheck
+ defer t.target.(io.Closer).Close()
+ }
+ err = tmpl.Execute(t.target, context)
return err
}
@@ -39,12 +40,6 @@ func NewGomplate(d *data.Data, leftDelim, rightDelim string) *Gomplate {
}
}
-// input - models an input file...
-type input struct {
- name string
- contents string
-}
-
func runTemplate(o *GomplateOpts) error {
defer runCleanupHooks()
d := data.NewData(o.dataSources, o.dataSourceHeaders)
@@ -52,42 +47,14 @@ func runTemplate(o *GomplateOpts) error {
g := NewGomplate(d, o.lDelim, o.rDelim)
- excludeList, err := executeCombinedGlob(o.excludeGlob)
+ tmpl, err := gatherTemplates(o)
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, in *input, outPath string) error {
- outFile, err := openOutFile(outPath)
- if err != nil {
- return err
- }
- // nolint: errcheck
- defer outFile.Close()
- err = g.RunTemplate(in, 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
+ for _, t := range tmpl {
+ if err := g.RunTemplate(t); err != nil {
+ return err
}
-
- combinedExcludes = append(combinedExcludes, excludeList...)
}
-
- return combinedExcludes, nil
+ return nil
}