builder/vmware: functions for parsing VMX
This commit is contained in:
parent
60dba3f8ef
commit
323647e3a6
|
@ -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
|
||||
}
|
|
@ -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"])
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue