From 832d45023be15ac640766792adf672430680ded2 Mon Sep 17 00:00:00 2001 From: mahcsig Date: Fri, 19 Aug 2016 04:23:07 -0700 Subject: [PATCH] Re-introduce case sensitive vmx key functionality (#2707) Add case sensitive vmx key functionality --- builder/vmware/common/vmx.go | 30 ++++++++++++++++++++++++++++-- builder/vmware/common/vmx_test.go | 9 ++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) mode change 100644 => 100755 builder/vmware/common/vmx_test.go diff --git a/builder/vmware/common/vmx.go b/builder/vmware/common/vmx.go index e7cdb662f..e531e4341 100755 --- a/builder/vmware/common/vmx.go +++ b/builder/vmware/common/vmx.go @@ -17,7 +17,7 @@ import ( func ParseVMX(contents string) map[string]string { results := make(map[string]string) - lineRe := regexp.MustCompile(`^(.+?)\s*=\s*"(.*?)"\s*$`) + lineRe := regexp.MustCompile(`^(.+?)\s*=\s*"?(.*?)"?\s*$`) for _, line := range strings.Split(contents, "\n") { matches := lineRe.FindStringSubmatch(line) @@ -43,9 +43,35 @@ func EncodeVMX(contents map[string]string) string { i++ } + // a list of VMX key fragments that the value must not be quoted + // fragments are used to cover multliples (i.e. multiple disks) + // keys are still lowercase at this point, use lower fragments + noQuotes := []string { + ".virtualssd", + } + + // a list of VMX key fragments that are case sensitive + // fragments are used to cover multliples (i.e. multiple disks) + caseSensitive := []string { + ".virtualSSD", + } + sort.Strings(keys) for _, k := range keys { - buf.WriteString(fmt.Sprintf("%s = \"%s\"\n", k, contents[k])) + pat := "%s = \"%s\"\n" + // items with no quotes + for _, q := range noQuotes { + if strings.Contains(k, q) { + pat = "%s = %s\n" + break; + } + } + key := k + // case sensitive key fragments + for _, c := range caseSensitive { + key = strings.Replace(key, strings.ToLower(c), c, 1) + } + buf.WriteString(fmt.Sprintf(pat, key, contents[k])) } return buf.String() diff --git a/builder/vmware/common/vmx_test.go b/builder/vmware/common/vmx_test.go old mode 100644 new mode 100755 index da07b2a10..715a6a6ee --- a/builder/vmware/common/vmx_test.go +++ b/builder/vmware/common/vmx_test.go @@ -6,10 +6,11 @@ func TestParseVMX(t *testing.T) { contents := ` .encoding = "UTF-8" config.version = "8" +scsi0:0.virtualSSD = 1 ` results := ParseVMX(contents) - if len(results) != 2 { + if len(results) != 3 { t.Fatalf("not correct number of results: %d", len(results)) } @@ -20,16 +21,22 @@ config.version = "8" if results["config.version"] != "8" { t.Errorf("invalid config.version: %s", results["config.version"]) } + + if results["scsi0:0.virtualssd"] != "1" { + t.Errorf("invalid scsi0:0.virtualssd: %s", results["scsi0:0.virtualssd"]) + } } func TestEncodeVMX(t *testing.T) { contents := map[string]string{ ".encoding": "UTF-8", "config.version": "8", + "scsi0:0.virtualssd": "1", } expected := `.encoding = "UTF-8" config.version = "8" +scsi0:0.virtualSSD = 1 ` result := EncodeVMX(contents)