Skip to content

Commit

Permalink
fixed missing output if strings have no placeholders
Browse files Browse the repository at this point in the history
added related tests
  • Loading branch information
malud committed Sep 8, 2017
1 parent 5c439f8 commit 224b1ce
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
33 changes: 33 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
}
6 changes: 5 additions & 1 deletion temgo/temgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

1 comment on commit 224b1ce

@malud
Copy link
Owner Author

@malud malud commented on 224b1ce Sep 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #2

Please sign in to comment.