From ecd044c260f162e4a9f233aab11e8eefe7949826 Mon Sep 17 00:00:00 2001 From: Jerome Froelich Date: Wed, 23 Oct 2019 17:22:57 -0400 Subject: [PATCH] Ensure replacement values are properly quoted --- expand.go | 7 ++++++- option_test.go | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/expand.go b/expand.go index 2ad692d..f71a72c 100644 --- a/expand.go +++ b/expand.go @@ -27,6 +27,7 @@ import ( "strings" "golang.org/x/text/transform" + yaml "gopkg.in/yaml.v2" ) const ( @@ -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 == "" { diff --git a/option_test.go b/option_test.go index 6623a28..1bfd993 100644 --- a/option_test.go +++ b/option_test.go @@ -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 @@ -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) {