Skip to content

Commit

Permalink
fixes #37 only replace env var if variable exists in environment
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-miracl committed Mar 6, 2018
1 parent f1a163a commit 4c9cccb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions conflate_test.go
Expand Up @@ -51,14 +51,15 @@ func TestAddData_Expand(t *testing.T) {
c.Expand(true)
os.Setenv("X", "123")
os.Setenv("Y", "str")
inJSON := []byte(`{ "x": $X, "y": "$Y"}`)
inJSON := []byte(`{ "x": $X, "y": "$Y", "z": "$Z"}`)
err := c.AddData(inJSON)
assert.Nil(t, err)
outJSON, err := c.MarshalJSON()
assert.Nil(t, err)
assert.Equal(t, `{
"x": 123,
"y": "str"
"y": "str",
"z": "$Z"
}
`, string(outJSON))
}
Expand Down
9 changes: 8 additions & 1 deletion filedata.go
Expand Up @@ -47,7 +47,14 @@ func newFiledata(bytes []byte, url pkgurl.URL) (filedata, error) {
}

func newExpandedFiledata(bytes []byte, url pkgurl.URL) (filedata, error) {
expBytes := []byte(os.ExpandEnv(string(bytes)))
expBytes := []byte(os.Expand(string(bytes),
func(name string) string {
val, ok := os.LookupEnv(name)
if ok {
return val
}
return "$" + name
}))
return newFiledata(expBytes, url)
}

Expand Down

0 comments on commit 4c9cccb

Please sign in to comment.