Skip to content

Commit

Permalink
Merge pull request #241 from hairyhenderson/refactor-template-processing
Browse files Browse the repository at this point in the history
Refactoring template processing
  • Loading branch information
hairyhenderson authored Jan 29, 2018
2 parents 272c06d + 6aec329 commit 76c18b6
Show file tree
Hide file tree
Showing 41 changed files with 7,084 additions and 266 deletions.
87 changes: 79 additions & 8 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 14 additions & 47 deletions gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -39,55 +40,21 @@ 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)
addCleanupHook(d.Cleanup)

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
}
4 changes: 2 additions & 2 deletions gomplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"github.com/stretchr/testify/assert"
)

func testTemplate(g *Gomplate, template string) string {
func testTemplate(g *Gomplate, tmpl string) string {
var out bytes.Buffer
g.RunTemplate(&input{"testtemplate", template}, &out)
g.RunTemplate(&tplate{name: "testtemplate", contents: tmpl, target: &out})
return out.String()
}

Expand Down
Loading

0 comments on commit 76c18b6

Please sign in to comment.