From 2b57b1028457e86c094972ca5ab53a5ef3ad7b87 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 29 Mar 2019 15:18:14 -0700 Subject: [PATCH] write fixer --- fix/fixer.go | 2 + fix/fixer_hyperv_cpu_and_ram_naming.go | 58 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 fix/fixer_hyperv_cpu_and_ram_naming.go diff --git a/fix/fixer.go b/fix/fixer.go index 41dd8e9fd..bf2ccf25b 100644 --- a/fix/fixer.go +++ b/fix/fixer.go @@ -39,6 +39,7 @@ func init() { "powershell-escapes": new(FixerPowerShellEscapes), "hyperv-deprecations": new(FixerHypervDeprecations), "hyperv-vmxc-typo": new(FixerHypervVmxcTypo), + "hyperv-cpu-and-ram": new(FizerHypervCPUandRAM), "vmware-compaction": new(FixerVMwareCompaction), } @@ -61,5 +62,6 @@ func init() { "docker-email", "powershell-escapes", "vmware-compaction", + "hyperv-cpu-and-ram", } } diff --git a/fix/fixer_hyperv_cpu_and_ram_naming.go b/fix/fixer_hyperv_cpu_and_ram_naming.go new file mode 100644 index 000000000..4caecc396 --- /dev/null +++ b/fix/fixer_hyperv_cpu_and_ram_naming.go @@ -0,0 +1,58 @@ +package fix + +import ( + "github.com/mitchellh/mapstructure" +) + +// FizerHypervCPUandRAM fixes the typo in "clone_from_vmxc_path" replacing +// it with "clone_from_vmcx_path" in Hyper-V VMCX builder templates +type FizerHypervCPUandRAM struct{} + +func (FizerHypervCPUandRAM) Fix(input map[string]interface{}) (map[string]interface{}, error) { + // The type we'll decode into; we only care about builders + type template struct { + Builders []map[string]interface{} + } + + // Decode the input into our structure, if we can + var tpl template + if err := mapstructure.Decode(input, &tpl); err != nil { + return nil, err + } + + for _, builder := range tpl.Builders { + builderTypeRaw, ok := builder["type"] + if !ok { + continue + } + + builderType, ok := builderTypeRaw.(string) + if !ok { + continue + } + + if builderType != "hyperv-vmcx" && builderType != "hyperv-iso" { + continue + } + + ncpus, ok := builder["cpu"] + if ok { + delete(builder, "cpu") + builder["cpus"] = ncpus + } + + memory, ok := builder["ram_size"] + if ok { + delete(builder, "ram_size") + builder["memory"] = memory + } + } + + input["builders"] = tpl.Builders + return input, nil +} + +func (FizerHypervCPUandRAM) Synopsis() string { + return `Replaces "cpu" with "cpus" and "ram_size" with "memory"` + + `in Hyper-V VMCX builder templates` +}