diff --git a/common/config.go b/common/config.go index eaf9caa02..772cd345d 100644 --- a/common/config.go +++ b/common/config.go @@ -45,6 +45,17 @@ func CheckUnusedConfig(md *mapstructure.Metadata) *packer.MultiError { return nil } +// ChooseString returns the first non-empty value. +func ChooseString(vals ...string) string { + for _, el := range vals { + if el != "" { + return el + } + } + + return "" +} + // DecodeConfig is a helper that handles decoding raw configuration using // mapstructure. It returns the metadata and any errors that may happen. // If you need extra configuration for mapstructure, you should configure @@ -206,14 +217,3 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) { return v, nil }, nil } - -// ChooseString returns the first non-empty value. -func ChooseString(vals ...string) string { - for _, el := range vals { - if el != "" { - return el - } - } - - return "" -} diff --git a/common/config_test.go b/common/config_test.go index e7cfe0627..9c8b42892 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -29,6 +29,33 @@ func TestCheckUnusedConfig(t *testing.T) { } } +func TestChooseString(t *testing.T) { + cases := []struct { + Input []string + Output string + }{ + { + []string{"", "foo", ""}, + "foo", + }, + { + []string{"", "foo", "bar"}, + "foo", + }, + { + []string{"", "", ""}, + "", + }, + } + + for _, tc := range cases { + result := ChooseString(tc.Input...) + if result != tc.Output { + t.Fatalf("bad: %#v", tc.Input) + } + } +} + func TestDecodeConfig(t *testing.T) { type Local struct { Foo string