Commit 323647e3 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: functions for parsing VMX

parent 60dba3f8
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
}
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"])
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment