Re-introduce case sensitive vmx key functionality (#2707)

Add case sensitive vmx key functionality
This commit is contained in:
mahcsig 2016-08-19 04:23:07 -07:00 committed by Rickard von Essen
parent 4ac6e2a08c
commit 832d45023b
2 changed files with 36 additions and 3 deletions

View File

@ -17,7 +17,7 @@ import (
func ParseVMX(contents string) map[string]string { func ParseVMX(contents string) map[string]string {
results := make(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") { for _, line := range strings.Split(contents, "\n") {
matches := lineRe.FindStringSubmatch(line) matches := lineRe.FindStringSubmatch(line)
@ -43,9 +43,35 @@ func EncodeVMX(contents map[string]string) string {
i++ 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) sort.Strings(keys)
for _, k := range 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() return buf.String()

9
builder/vmware/common/vmx_test.go Normal file → Executable file
View File

@ -6,10 +6,11 @@ func TestParseVMX(t *testing.T) {
contents := ` contents := `
.encoding = "UTF-8" .encoding = "UTF-8"
config.version = "8" config.version = "8"
scsi0:0.virtualSSD = 1
` `
results := ParseVMX(contents) results := ParseVMX(contents)
if len(results) != 2 { if len(results) != 3 {
t.Fatalf("not correct number of results: %d", len(results)) t.Fatalf("not correct number of results: %d", len(results))
} }
@ -20,16 +21,22 @@ config.version = "8"
if results["config.version"] != "8" { if results["config.version"] != "8" {
t.Errorf("invalid config.version: %s", results["config.version"]) 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) { func TestEncodeVMX(t *testing.T) {
contents := map[string]string{ contents := map[string]string{
".encoding": "UTF-8", ".encoding": "UTF-8",
"config.version": "8", "config.version": "8",
"scsi0:0.virtualssd": "1",
} }
expected := `.encoding = "UTF-8" expected := `.encoding = "UTF-8"
config.version = "8" config.version = "8"
scsi0:0.virtualSSD = 1
` `
result := EncodeVMX(contents) result := EncodeVMX(contents)