From e3acd19cb715be30281f5c26856cbb4333511172 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Mon, 21 Nov 2016 15:34:50 -0800 Subject: [PATCH] add fixer for manifest filename->output --- fix/fixer.go | 2 + fix/fixer_pp_manifest_filename.go | 60 ++++++++++++++++++++++++++ fix/fixer_pp_manifest_filename_test.go | 41 ++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 fix/fixer_pp_manifest_filename.go create mode 100644 fix/fixer_pp_manifest_filename_test.go diff --git a/fix/fixer.go b/fix/fixer.go index 78f58f546..382cd25b8 100644 --- a/fix/fixer.go +++ b/fix/fixer.go @@ -29,6 +29,7 @@ func init() { "parallels-headless": new(FixerParallelsHeadless), "parallels-deprecations": new(FixerParallelsDeprecations), "sshkeypath": new(FixerSSHKeyPath), + "manifest-filename": new(FixerManifestFilename), } FixerOrder = []string{ @@ -41,5 +42,6 @@ func init() { "parallels-headless", "parallels-deprecations", "sshkeypath", + "manifest-filename", } } diff --git a/fix/fixer_pp_manifest_filename.go b/fix/fixer_pp_manifest_filename.go new file mode 100644 index 000000000..923f63c93 --- /dev/null +++ b/fix/fixer_pp_manifest_filename.go @@ -0,0 +1,60 @@ +package fix + +import ( + "fmt" + "github.com/mitchellh/mapstructure" +) + +// FixerManifestFilename renames any Filename to Output +type FixerManifestFilename struct{} + +func (FixerManifestFilename) Fix(input map[string]interface{}) (map[string]interface{}, error) { + + // Our template type we'll use for this fixer only + type template struct { + PostProcessors []map[string]interface{} `mapstructure:"post-processors"` + } + + // Decode the input into our structure, if we can + fmt.Println("Got 0") + var tpl template + if err := mapstructure.Decode(input, &tpl); err != nil { + fmt.Println("Got 1") + return nil, err + } + for _, pp := range tpl.PostProcessors { + ppTypeRaw, ok := pp["type"] + if !ok { + continue + } + + ppType, ok := ppTypeRaw.(string) + if !ok { + continue + } + + if ppType != "manifest" { + continue + } + + filenameRaw, ok := pp["filename"] + if !ok { + continue + } + + filename, ok := filenameRaw.(string) + if !ok { + continue + } + + delete(pp, "filename") + pp["output"] = filename + } + + input["post-processors"] = tpl.PostProcessors + return input, nil +} + +func (FixerManifestFilename) Synopsis() string { + return `Updates "manifest" post-processor so any "filename" field is renamed to "output".` +} diff --git a/fix/fixer_pp_manifest_filename_test.go b/fix/fixer_pp_manifest_filename_test.go new file mode 100644 index 000000000..ed726dfd6 --- /dev/null +++ b/fix/fixer_pp_manifest_filename_test.go @@ -0,0 +1,41 @@ +package fix + +import ( + "reflect" + "testing" +) + +func TestFixerManifestPPFilename_Impl(t *testing.T) { + var _ Fixer = new(FixerVagrantPPOverride) +} + +func TestFixerManifestPPFilename_Fix(t *testing.T) { + var f FixerManifestFilename + + input := map[string]interface{}{ + "post-processors": []map[string]interface{}{ + { + "type": "manifest", + "filename": "foo", + }, + }, + } + + expected := map[string]interface{}{ + "post-processors": []map[string]interface{}{ + { + "type": "manifest", + "output": "foo", + }, + }, + } + + output, err := f.Fix(input) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !reflect.DeepEqual(output, expected) { + t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected) + } +}