virtualbox: simplify ParseSnapshotData
This commit is contained in:
parent
c48978b90e
commit
a36b94320a
|
@ -29,13 +29,23 @@ func ParseSnapshotData(snapshotData string) (*VBoxSnapshot, error) {
|
|||
|
||||
for scanner.Scan() {
|
||||
txt := scanner.Text()
|
||||
idx := strings.Index(txt, "=")
|
||||
if idx > 0 {
|
||||
if !strings.Contains(txt, "=") {
|
||||
log.Printf("Invalid key,value pair [%s]", txt)
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(txt, "Current") {
|
||||
node.IsCurrent = true
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
matches := SnapshotNamePartsRe.FindStringSubmatch(txt)
|
||||
if len(matches) >= 2 && matches[1] == "Name" {
|
||||
if len(matches) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch matches[1] {
|
||||
case "UUID":
|
||||
node.UUID = matches[4]
|
||||
case "Name":
|
||||
if nil == rootNode {
|
||||
node = new(VBoxSnapshot)
|
||||
rootNode = node
|
||||
|
@ -60,12 +70,6 @@ func ParseSnapshotData(snapshotData string) (*VBoxSnapshot, error) {
|
|||
}
|
||||
}
|
||||
node.Name = matches[4]
|
||||
} else if matches[1] == "UUID" {
|
||||
node.UUID = matches[4]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Printf("Invalid key,value pair [%s]", txt)
|
||||
}
|
||||
}
|
||||
return rootNode, nil
|
||||
|
|
|
@ -134,3 +134,10 @@ func TestSnapshot_EnsureParents(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshot_WrongSnapshot(t *testing.T) {
|
||||
rootNode, err := ParseSnapshotData(`Potato=
|
||||
`)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, rootNode)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue