From 224b1cef4af73c42375b9c9a768429bd9cde0d8d Mon Sep 17 00:00:00 2001 From: Marcel Ludwig Date: Fri, 8 Sep 2017 20:45:09 +0200 Subject: [PATCH] fixed missing output if strings have no placeholders added related tests --- README.md | 3 +++ cli/cli.go | 15 ++++++++++++--- cli/cli_test.go | 33 +++++++++++++++++++++++++++++++++ temgo/temgo.go | 6 +++++- 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 cli/cli_test.go diff --git a/README.md b/README.md index b09ce23..e45625d 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ TESTVAR=foo tg -i /dest/config/file1.ext However, using the inline option on your templates will overwrite them. It is recommended to use this option on resettable files. +Placeholders which have no corresponding environment variable gets not replaced. +`echo 'foo {{ NOT_SET }} bar' | ./tg` results in `foo {{ NOT_SET }} bar` + ### TODO * flags for file in/out * prefix flag for env var e.g. SERVICE_XXX diff --git a/cli/cli.go b/cli/cli.go index 77f9150..e3b4038 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -37,17 +37,26 @@ func main() { rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)) } + replace(rw, file) +} + +func replace(rw *bufio.ReadWriter, file *os.File) { bytes, err := ioutil.ReadAll(rw) must(err) + write := func(b []byte) { + _, err := rw.Write(b) + must(err) + must(rw.Flush()) + } if temgo.ContainsVariable(bytes) { str := envVars.ReplaceVariables(bytes) if file != nil { truncate(file) } - _, err := rw.Write(str) - must(err) - must(rw.Flush()) + write(str) + } else if file == nil { + write(bytes) } } diff --git a/cli/cli_test.go b/cli/cli_test.go new file mode 100644 index 0000000..f191fd2 --- /dev/null +++ b/cli/cli_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "os" + "testing" +) + +func TestReplace(t *testing.T) { + wd, _ := os.Getwd() + testCases := []struct { + in []byte + expected string + }{ + {[]byte("foo bar"), "foo bar"}, + {[]byte("foo {{ NOT_SET }} bar"), "foo {{ NOT_SET }} bar"}, + {[]byte("foo {{invalid }} bar"), "foo {{invalid }} bar"}, + {[]byte("foo {{ PWD }}!"), fmt.Sprintf("foo %s!", wd)}, + } + + for _, testCase := range testCases { + r := bytes.NewBuffer(testCase.in) + w := bytes.NewBuffer(make([]byte, 0)) + rw := bufio.NewReadWriter(bufio.NewReader(r), bufio.NewWriter(w)) + replace(rw, nil) + out := w.Bytes() + if string(out) != testCase.expected { + t.Errorf("\nWant: \t%q\nGot: \t%q", string(testCase.expected), string(out)) + } + } +} diff --git a/temgo/temgo.go b/temgo/temgo.go index 7ae811c..7799765 100644 --- a/temgo/temgo.go +++ b/temgo/temgo.go @@ -17,7 +17,11 @@ func ContainsVariable(str []byte) bool { func replace(e *EnvVars, bytes []byte) []byte { list := templatePattern.FindAllStringSubmatch(string(bytes), -1) for _, match := range list { - bytes = []byte(strings.Replace(string(bytes), match[0], (*e)[match[1]], -1)) + str, ok := (*e)[match[1]] + if !ok { + continue // variable not set + } + bytes = []byte(strings.Replace(string(bytes), match[0], str, -1)) } return bytes }