diff --git a/CHANGELOG.md b/CHANGELOG.md index 1230bd046..2927455c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ FEATURES: IMPROVEMENTS: * core: RPC transport between plugins switched to MessagePack + * core: Templates array values can now be comma separated strings. + Most importantly, this allows for user variables to fill + array configurations. [GH-950] * builder/amazon: Added `ssh_private_key_file` option [GH-971] * builder/amazon: Added `ami_virtualization_type` option [GH-1021] * builder/googlecompute: Configurable instance name. [GH-1065] diff --git a/common/config.go b/common/config.go index 0919bf714..4c6fbbbef 100644 --- a/common/config.go +++ b/common/config.go @@ -68,7 +68,10 @@ func DecodeConfig(target interface{}, raws ...interface{}) (*mapstructure.Metada var md mapstructure.Metadata decoderConfig := &mapstructure.DecoderConfig{ - DecodeHook: decodeHook, + DecodeHook: mapstructure.ComposeDecodeHookFunc( + decodeHook, + mapstructure.StringToSliceHookFunc(","), + ), Metadata: &md, Result: target, WeaklyTypedInput: true, diff --git a/common/config_test.go b/common/config_test.go index fa7e57e81..3217a2ebd 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -95,6 +95,33 @@ func TestDecodeConfig(t *testing.T) { } } +// This test tests the case that a user var is used for an integer +// configuration. +func TestDecodeConfig_stringToSlice(t *testing.T) { + type Local struct { + Val []string + } + + raw := map[string]interface{}{ + "packer_user_variables": map[string]string{ + "foo": "bar", + }, + + "val": "foo,{{user `foo`}}", + } + + var result Local + _, err := DecodeConfig(&result, raw) + if err != nil { + t.Fatalf("err: %s", err) + } + + expected := []string{"foo", "bar"} + if !reflect.DeepEqual(result.Val, expected) { + t.Fatalf("invalid: %#v", result.Val) + } +} + // This test tests the case that a user var is used for an integer // configuration. func TestDecodeConfig_userVarConversion(t *testing.T) {