Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure replacement values are properly quoted #110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"

"golang.org/x/text/transform"
yaml "gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -68,7 +69,11 @@ func replace(lookUp LookupFunc) func(in string) (string, error) {
}

if envVal, ok := lookUp(key); ok {
return envVal, nil
buf, err := yaml.Marshal(envVal)
if err != nil {
return "", fmt.Errorf("unable to marshal value of %q to YAML: %v", key, err)
}
return string(buf), nil
}

if def == "" {
Expand Down
3 changes: 2 additions & 1 deletion option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestStatic(t *testing.T) {
}

func TestExpand(t *testing.T) {
environment := map[string]string{"FOO": "bar"}
environment := map[string]string{"FOO": "bar", "BAR": "@bar"}
lookup := func(key string) (string, bool) {
s, ok := environment[key]
return s, ok
Expand All @@ -60,6 +60,7 @@ func TestExpand(t *testing.T) {
{"present bracketed", "${FOO:baz}", false, "bar"},
{"absent bracketed", "${NOT_THERE:baz}", false, "baz"},
{"absent bracketed no default", `${NOT_THERE:""}`, false, nil},
{"present bracketed quoted value", "${BAR}", false, "@bar"},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
Expand Down