fix: virtualbox rename fixes overrides [GH-1828]

This commit is contained in:
Mitchell Hashimoto 2015-06-13 16:39:12 -04:00
parent 7eff6b117d
commit c3f54ba5a9
2 changed files with 78 additions and 4 deletions

View File

@ -8,14 +8,14 @@ import (
type FixerVirtualBoxRename struct{} type FixerVirtualBoxRename struct{}
func (FixerVirtualBoxRename) Fix(input map[string]interface{}) (map[string]interface{}, error) { func (FixerVirtualBoxRename) Fix(input map[string]interface{}) (map[string]interface{}, error) {
// The type we'll decode into; we only care about builders
type template struct { type template struct {
Builders []map[string]interface{} Builders []map[string]interface{}
Provisioners []interface{}
} }
// Decode the input into our structure, if we can // Decode the input into our structure, if we can
var tpl template var tpl template
if err := mapstructure.Decode(input, &tpl); err != nil { if err := mapstructure.WeakDecode(input, &tpl); err != nil {
return nil, err return nil, err
} }
@ -37,7 +37,39 @@ func (FixerVirtualBoxRename) Fix(input map[string]interface{}) (map[string]inter
builder["type"] = "virtualbox-iso" builder["type"] = "virtualbox-iso"
} }
for i, raw := range tpl.Provisioners {
var m map[string]interface{}
if err := mapstructure.WeakDecode(raw, &m); err != nil {
// Ignore errors, could be a non-map
continue
}
raw, ok := m["override"]
if !ok {
continue
}
var override map[string]interface{}
if err := mapstructure.WeakDecode(raw, &override); err != nil {
return nil, err
}
if raw, ok := override["virtualbox"]; ok {
override["virtualbox-iso"] = raw
delete(override, "virtualbox")
// Set the change
m["override"] = override
tpl.Provisioners[i] = m
}
}
if len(tpl.Builders) > 0 {
input["builders"] = tpl.Builders input["builders"] = tpl.Builders
}
if len(tpl.Provisioners) > 0 {
input["provisioners"] = tpl.Provisioners
}
return input, nil return input, nil
} }

View File

@ -46,3 +46,45 @@ func TestFixerVirtualBoxRename_Fix(t *testing.T) {
} }
} }
} }
func TestFixerVirtualBoxRenameFix_provisionerOverride(t *testing.T) {
cases := []struct {
Input map[string]interface{}
Expected map[string]interface{}
}{
{
Input: map[string]interface{}{
"provisioners": []interface{}{
map[string]interface{}{
"override": map[string]interface{}{
"virtualbox": map[string]interface{}{},
},
},
},
},
Expected: map[string]interface{}{
"provisioners": []interface{}{
map[string]interface{}{
"override": map[string]interface{}{
"virtualbox-iso": map[string]interface{}{},
},
},
},
},
},
}
for _, tc := range cases {
var f FixerVirtualBoxRename
output, err := f.Fix(tc.Input)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(output, tc.Expected) {
t.Fatalf("unexpected:\n\n%#v\nexpected:\n\n%#v\n", output, tc.Expected)
}
}
}