diff --git a/config/variable.go b/config/variable.go index 0fd0ebd841..841981a39b 100644 --- a/config/variable.go +++ b/config/variable.go @@ -16,14 +16,18 @@ func init() { varRegexp = regexp.MustCompile(`(?i)(\$+)\{([-.a-z0-9_]+)\}`) } -// replaceVariables takes a configuration and a mapping of variables +// ReplaceVariables takes a configuration and a mapping of variables // and performs the structure walking necessary to properly replace // all the variables. -func replaceVariables( - c map[string]interface{}, - vs map[string]string) error { +func ReplaceVariables( + c interface{}, + vs map[string]string) ([]string, error) { w := &variableReplaceWalker{Values: vs} - return reflectwalk.Walk(c, w) + if err := reflectwalk.Walk(c, w); err != nil { + return nil, err + } + + return w.UnknownKeys, nil } // variableDetectWalker implements interfaces for the reflectwalk package diff --git a/config/variable_test.go b/config/variable_test.go index bd3a6a96e6..88fdb23e10 100644 --- a/config/variable_test.go +++ b/config/variable_test.go @@ -36,6 +36,25 @@ func BenchmarkVariableReplaceWalker(b *testing.B) { } } +func TestReplaceVariables(t *testing.T) { + input := "foo-${var.bar}" + expected := "foo-bar" + + unk, err := ReplaceVariables(&input, map[string]string{ + "var.bar": "bar", + }) + if err != nil { + t.Fatalf("err: %s", err) + } + if len(unk) > 0 { + t.Fatal("bad: %#v", unk) + } + + if input != expected { + t.Fatalf("bad: %#v", input) + } +} + func TestVariableDetectWalker(t *testing.T) { w := new(variableDetectWalker)