builder/vmware: functions for parsing VMX

This commit is contained in:
Mitchell Hashimoto 2013-06-05 21:01:22 -07:00
parent 60dba3f8ef
commit 323647e3a6
2 changed files with 48 additions and 0 deletions

25
builder/vmware/vmx.go Normal file
View File

@ -0,0 +1,25 @@
package vmware
import (
"regexp"
"strings"
)
// ParseVMX parses the keys and values from a VMX file and returns
// them as a Go map.
func ParseVMX(contents string) map[string]string {
results := make(map[string]string)
lineRe := regexp.MustCompile(`^(.+?)\s*=\s*"(.*?)"\s*$`)
for _, line := range strings.Split(contents, "\n") {
matches := lineRe.FindStringSubmatch(line)
if matches == nil {
continue
}
results[matches[1]] = matches[2]
}
return results
}

View File

@ -0,0 +1,23 @@
package vmware
import "testing"
func TestParseVMX(t *testing.T) {
contents := `
.encoding = "UTF-8"
config.version = "8"
`
results := ParseVMX(contents)
if len(results) != 2 {
t.Fatalf("not correct number of results: %d", len(results))
}
if results[".encoding"] != "UTF-8" {
t.Errorf("invalid .encoding: %s", results[".encoding"])
}
if results["config.version"] != "8" {
t.Errorf("invalid config.version: %s", results["config.version"])
}
}