Skip to content

Commit

Permalink
Naming template after input filename
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed Dec 29, 2017
1 parent 63a99be commit b98fbcd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
18 changes: 12 additions & 6 deletions gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion gomplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
37 changes: 22 additions & 15 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand All @@ -89,44 +89,51 @@ 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 == "-" {
inFile = os.Stdin
} 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()
}
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) {
Expand Down
6 changes: 3 additions & 3 deletions process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions test/integration/input-dir.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"* ]]
}

0 comments on commit b98fbcd

Please sign in to comment.