diff --git a/gomplate.go b/gomplate.go index d1c6bb682..6bb3243a5 100644 --- a/gomplate.go +++ b/gomplate.go @@ -8,8 +8,8 @@ import ( "github.com/hairyhenderson/gomplate/data" ) -func (g *Gomplate) createTemplate() *template.Template { - return template.New("template").Funcs(g.funcMap).Option("missingkey=error") +func (g *Gomplate) createTemplate(name string) *template.Template { + return template.New(name).Funcs(g.funcMap).Option("missingkey=error") } // Gomplate - @@ -20,9 +20,9 @@ type Gomplate struct { } // RunTemplate - -func (g *Gomplate) RunTemplate(text string, out io.Writer) error { +func (g *Gomplate) RunTemplate(in *input, out io.Writer) error { context := &Context{} - tmpl, err := g.createTemplate().Delims(g.leftDelim, g.rightDelim).Parse(text) + tmpl, err := g.createTemplate(in.name).Delims(g.leftDelim, g.rightDelim).Parse(in.contents) if err != nil { return err } @@ -39,6 +39,12 @@ 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) @@ -59,14 +65,14 @@ func runTemplate(o *GomplateOpts) error { } // Called from process.go ... -func renderTemplate(g *Gomplate, inString string, outPath string) error { +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(inString, outFile) + err = g.RunTemplate(in, outFile) return err } diff --git a/gomplate_test.go b/gomplate_test.go index 112f6ae37..d3d23cc69 100644 --- a/gomplate_test.go +++ b/gomplate_test.go @@ -17,7 +17,7 @@ import ( func testTemplate(g *Gomplate, template string) string { var out bytes.Buffer - g.RunTemplate(template, &out) + g.RunTemplate(&input{"testtemplate", template}, &out) return out.String() } diff --git a/net/net_test.go b/net/net_test.go index ff4ffd372..bcd17a9bb 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -14,7 +14,7 @@ func TestLookupIP(t *testing.T) { } func TestLookupIPs(t *testing.T) { - assert.Equal(t, []string{"127.0.0.1"}, LookupIPs("localhost")) + assert.Equal(t, "127.0.0.1", LookupIPs("localhost")[0]) assert.Equal(t, []string{"169.254.255.254"}, LookupIPs("hostlocal.io")) assert.Equal(t, []string{"93.184.216.34"}, LookupIPs("example.com")) } diff --git a/process.go b/process.go index 7df938195..46dd9e63c 100644 --- a/process.go +++ b/process.go @@ -10,7 +10,7 @@ import ( // == Direct input processing ======================================== func processInputFiles(stringTemplate string, input []string, output []string, excludeList []string, g *Gomplate) error { - input, err := readInputs(stringTemplate, input) + ins, err := readInputs(stringTemplate, input) if err != nil { return err } @@ -19,8 +19,8 @@ func processInputFiles(stringTemplate string, input []string, output []string, e output = []string{"-"} } - for n, input := range input { - if err := renderTemplate(g, input, output[n]); err != nil { + for n, in := range ins { + if err := renderTemplate(g, in, output[n]); err != nil { return err } } @@ -65,11 +65,11 @@ func processInputDir(input string, output string, excludeList []string, g *Gompl return err } } else { - inString, err := readInput(nextInPath) + in, err := readInput(nextInPath) if err != nil { return err } - if err := renderTemplate(g, inString, nextOutPath); err != nil { + if err := renderTemplate(g, in, nextOutPath); err != nil { return err } } @@ -89,26 +89,29 @@ func inList(list []string, entry string) bool { // == File handling ================================================ -func readInputs(input string, files []string) ([]string, error) { - if input != "" { - return []string{input}, nil +func readInputs(inString string, files []string) ([]*input, error) { + if inString != "" { + return []*input{{ + name: "", + contents: inString, + }}, nil } if len(files) == 0 { files = []string{"-"} } - ins := make([]string, len(files)) + ins := make([]*input, len(files)) for n, filename := range files { - inString, err := readInput(filename) + in, err := readInput(filename) if err != nil { return nil, err } - ins[n] = inString + ins[n] = in } return ins, nil } -func readInput(filename string) (string, error) { +func readInput(filename string) (*input, error) { var err error var inFile *os.File if filename == "-" { @@ -116,7 +119,7 @@ func readInput(filename string) (string, error) { } else { inFile, err = os.Open(filename) if err != nil { - return "", fmt.Errorf("failed to open %s\n%v", filename, err) + return nil, fmt.Errorf("failed to open %s\n%v", filename, err) } // nolint: errcheck defer inFile.Close() @@ -124,9 +127,13 @@ func readInput(filename string) (string, error) { bytes, err := ioutil.ReadAll(inFile) if err != nil { err = fmt.Errorf("read failed for %s\n%v", filename, err) - return "", err + return nil, err } - return string(bytes), nil + in := &input{ + name: filename, + contents: string(bytes), + } + return in, nil } func openOutFile(filename string) (out *os.File, err error) { diff --git a/process_test.go b/process_test.go index 161518a43..7441724c2 100644 --- a/process_test.go +++ b/process_test.go @@ -18,18 +18,18 @@ import ( func TestReadInput(t *testing.T) { actual, err := readInputs("foo", nil) assert.Nil(t, err) - assert.Equal(t, "foo", actual[0]) + assert.Equal(t, &input{"", "foo"}, actual[0]) // stdin is "" because during tests it's given /dev/null actual, err = readInputs("", []string{"-"}) assert.Nil(t, err) - assert.Equal(t, "", actual[0]) + assert.Equal(t, &input{"-", ""}, actual[0]) actual, err = readInputs("", []string{"process_test.go"}) assert.Nil(t, err) thisFile, _ := os.Open("process_test.go") expected, _ := ioutil.ReadAll(thisFile) - assert.Equal(t, string(expected), actual[0]) + assert.Equal(t, &input{"process_test.go", string(expected)}, actual[0]) } func TestInputDir(t *testing.T) { diff --git a/test/integration/input-dir.bats b/test/integration/input-dir.bats index 30cf9adae..fb222d267 100644 --- a/test/integration/input-dir.bats +++ b/test/integration/input-dir.bats @@ -67,3 +67,11 @@ function teardown () { [ "$status" -eq 1 ] [[ "${output}" == "Error: --output-dir can not be used together with --out"* ]] } + +@test "errors with filename when using input dir and bad input file" { + rm -rf $tmpdir/out || true + echo -n "{{end}}" > $tmpdir/in/bad.tmpl + gomplate --input-dir $tmpdir/in --output-dir $tmpdir/out -d config=$tmpdir/config.yml + [ "$status" -eq 1 ] + [[ "${output}" == "Error: template: $tmpdir/in/bad.tmpl:1: unexpected {{end}}"* ]] +} \ No newline at end of file