summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2017-12-29 20:56:47 +0100
committerDave Henderson <dhenderson@gmail.com>2017-12-29 21:35:25 +0100
commitb98fbcd880d80a19e3a5b76601a3ba35fadf4c3c (patch)
tree556fd055c0c9063ceb37b08018dc4b9d65f3ad54
parent63a99be4de3e93251a850cb2a8a18b43e6018291 (diff)
Naming template after input filename
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
-rw-r--r--gomplate.go18
-rw-r--r--gomplate_test.go2
-rw-r--r--net/net_test.go2
-rw-r--r--process.go37
-rw-r--r--process_test.go6
-rw-r--r--test/integration/input-dir.bats8
6 files changed, 47 insertions, 26 deletions
diff --git a/gomplate.go b/gomplate.go
index d1c6bb68..6bb3243a 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 112f6ae3..d3d23cc6 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 ff4ffd37..bcd17a9b 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 7df93819..46dd9e63 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: "<arg>",
+ 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 161518a4..7441724c 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{"<arg>", "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 30cf9ada..fb222d26 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