Skip to content

Commit 77e6bda

Browse files
author
Steven Wobser
committed
Fixed parsing for property values containing '='
1 parent 983fd93 commit 77e6bda

File tree

3 files changed

+115
-16
lines changed

3 files changed

+115
-16
lines changed

decode.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"log"
1212
"reflect"
13+
"regexp"
1314
"strconv"
1415
"strings"
1516
)
@@ -46,27 +47,21 @@ func (d *Decoder) loadVMXMap() error {
4647
for d.scanner.Scan() {
4748
line := d.scanner.Text()
4849

49-
// Ignore comments and empty lines
50-
if strings.HasPrefix(line, "#") || strings.TrimSpace(line) == "" {
50+
re := regexp.MustCompile(`^(?P<key>[^#]\S+) ?= ?"(?P<value>.+)"$`)
51+
match := re.FindStringSubmatch(line)
52+
if match == nil {
5153
continue
5254
}
5355

54-
parts := strings.Split(line, "=")
55-
if len(parts) != 2 {
56-
errors = appendErrors(errors, fmt.Errorf("Invalid line: %s ", line))
57-
}
58-
59-
key := strings.TrimSpace(parts[0])
60-
value := strings.TrimSpace(parts[1])
61-
62-
unquotedVal, err := strconv.Unquote(value)
63-
if err != nil {
64-
unquotedVal = value
65-
//errors = appendErrors(errors, fmt.Errorf("Error unquoting vmx value %s: %v.", value, err))
56+
groups := make(map[string]string)
57+
for i, name := range re.SubexpNames() {
58+
if i != 0 && name != "" {
59+
groups[name] = match[i]
60+
}
6661
}
6762

68-
key = strings.ToLower(key)
69-
d.vmx[key] = unquotedVal
63+
key := strings.ToLower(groups["key"])
64+
d.vmx[key] = groups["value"]
7065
}
7166

7267
if err := d.scanner.Err(); err != nil {

decode_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ func TestUnmarshal(t *testing.T) {
3636
assert(t, len(vm2.SCSIDevices) == 2, fmt.Sprintf("%d != %d", len(vm2.SCSIDevices), 2))
3737
assert(t, vm2.SCSIDevices[0].VMXID != "", fmt.Sprintf("VMXID should not be empty: ->%s<-", vm2.SCSIDevices[0].VMXID))
3838
ok(t, err)
39+
40+
data, err = ioutil.ReadFile(filepath.Join(".", "fixtures", "c.vmx"))
41+
vm3 := new(VirtualMachine)
42+
err = Unmarshal([]byte(data), vm3)
43+
ok(t, err)
3944
}

fixtures/c.vmx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
.encoding = "UTF-8"
2+
bios.bootorder = "hdd,cdrom"
3+
checkpoint.vmstate = ""
4+
cleanshutdown = "TRUE"
5+
config.version = "8"
6+
displayname = "generic-ubuntu1804-vmware"
7+
ehci.pcislotnumber = "-1"
8+
ehci.present = "FALSE"
9+
extendedconfigfile = "generic-ubuntu1804-vmware.vmxf"
10+
floppy0.present = "FALSE"
11+
guestos = "ubuntu-64"
12+
guestos.detailed.data = "bitness='64' distroName='Ubuntu' distroVersion='18.04' familyName='Linux' kernelVersion='4.15.0-76-generic' prettyName='Ubuntu 18.04.4 LTS'"
13+
gui.fullscreenatpoweron = "FALSE"
14+
gui.viewmodeatpoweron = "windowed"
15+
hgfs.linkrootshare = "TRUE"
16+
hgfs.maprootshare = "TRUE"
17+
ide0:0.clientdevice = "TRUE"
18+
ide0:0.devicetype = "cdrom-raw"
19+
ide0:0.filename = "auto detect"
20+
ide0:0.present = "TRUE"
21+
isolation.tools.hgfs.disable = "FALSE"
22+
memsize = "2048"
23+
monitor.phys_bits_used = "42"
24+
msg.autoanswer = "true"
25+
numa.autosize.cookie = "20001"
26+
numa.autosize.vcpu.maxpervirtualnode = "2"
27+
numvcpus = "2"
28+
nvme0.present = "FALSE"
29+
nvram = "generic-ubuntu1804-vmware.nvram"
30+
parallel0.autodetect = "FALSE"
31+
parallel0.bidirectional = ""
32+
parallel0.filename = ""
33+
parallel0.present = "FALSE"
34+
parallel0.startconnected = "FALSE"
35+
pcibridge0.pcislotnumber = "17"
36+
pcibridge0.present = "TRUE"
37+
pcibridge4.functions = "8"
38+
pcibridge4.pcislotnumber = "21"
39+
pcibridge4.present = "TRUE"
40+
pcibridge4.virtualdev = "pcieRootPort"
41+
pcibridge5.functions = "8"
42+
pcibridge5.pcislotnumber = "22"
43+
pcibridge5.present = "TRUE"
44+
pcibridge5.virtualdev = "pcieRootPort"
45+
pcibridge6.functions = "8"
46+
pcibridge6.pcislotnumber = "23"
47+
pcibridge6.present = "TRUE"
48+
pcibridge6.virtualdev = "pcieRootPort"
49+
pcibridge7.functions = "8"
50+
pcibridge7.pcislotnumber = "24"
51+
pcibridge7.present = "TRUE"
52+
pcibridge7.virtualdev = "pcieRootPort"
53+
powertype.poweroff = "soft"
54+
powertype.poweron = "soft"
55+
powertype.reset = "soft"
56+
powertype.suspend = "soft"
57+
proxyapps.publishtohost = "FALSE"
58+
remotedisplay.vnc.enabled = "FALSE"
59+
remotedisplay.vnc.ip = "127.0.0.1"
60+
remotedisplay.vnc.port = "5904"
61+
replay.filename = ""
62+
replay.supported = "FALSE"
63+
sata0.present = "FALSE"
64+
scsi0.pcislotnumber = "16"
65+
scsi0.present = "TRUE"
66+
scsi0.virtualdev = "lsilogic"
67+
scsi0:0.filename = "generic-ubuntu1804-vmware.vmdk"
68+
scsi0:0.present = "TRUE"
69+
scsi0:0.redo = ""
70+
serial0.autodetect = "FALSE"
71+
serial0.filename = ""
72+
serial0.filetype = ""
73+
serial0.pipe.endpoint = ""
74+
serial0.present = "FALSE"
75+
serial0.startconnected = "FALSE"
76+
serial0.trynorxloss = ""
77+
serial0.yieldonmsrread = ""
78+
softpoweroff = "FALSE"
79+
sound.autodetect = "TRUE"
80+
sound.filename = "-1"
81+
sound.present = "FALSE"
82+
sound.startconnected = "FALSE"
83+
svga.guestbackedprimaryaware = "TRUE"
84+
svga.vramsize = "134217728"
85+
tools.synctime = "TRUE"
86+
tools.upgrade.policy = "upgradeAtPowerCycle"
87+
toolsinstallmanager.updatecounter = "1"
88+
usb.pcislotnumber = "-1"
89+
usb.present = "FALSE"
90+
uuid.action = "create"
91+
uuid.bios = "56 4d fc 04 54 ab 8e 06-3c c5 a6 4d f9 c8 bc 5a"
92+
uuid.location = "56 4d fc 04 54 ab 8e 06-3c c5 a6 4d f9 c8 bc 5a"
93+
virtualhw.productcompatibility = "hosted"
94+
virtualhw.version = "12"
95+
vmci0.id = "1861462628"
96+
vmci0.pcislotnumber = "35"
97+
vmci0.present = "TRUE"
98+
vmotion.checkpointfbsize = "134217728"
99+
vmotion.checkpointsvgaprimarysize = "134217728"

0 commit comments

Comments
 (0)