Merge pull request #8336 from hashicorp/simplify_vb_parse_snapshot_data
virtualbox: simplify ParseSnapshotData
This commit is contained in:
commit
3117d4f0a2
|
@ -29,43 +29,47 @@ func ParseSnapshotData(snapshotData string) (*VBoxSnapshot, error) {
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
txt := scanner.Text()
|
txt := scanner.Text()
|
||||||
idx := strings.Index(txt, "=")
|
if !strings.Contains(txt, "=") {
|
||||||
if idx > 0 {
|
log.Printf("Invalid key,value pair [%s]", txt)
|
||||||
if strings.HasPrefix(txt, "Current") {
|
continue
|
||||||
node.IsCurrent = true
|
}
|
||||||
|
if strings.HasPrefix(txt, "Current") {
|
||||||
|
node.IsCurrent = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
matches := SnapshotNamePartsRe.FindStringSubmatch(txt)
|
||||||
|
if len(matches) < 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch matches[1] {
|
||||||
|
case "UUID":
|
||||||
|
node.UUID = matches[4]
|
||||||
|
case "Name":
|
||||||
|
if nil == rootNode {
|
||||||
|
node = new(VBoxSnapshot)
|
||||||
|
rootNode = node
|
||||||
|
currentIndicator = matches[2]
|
||||||
} else {
|
} else {
|
||||||
matches := SnapshotNamePartsRe.FindStringSubmatch(txt)
|
pathLenCur := strings.Count(currentIndicator, "-")
|
||||||
if len(matches) >= 2 && matches[1] == "Name" {
|
pathLen := strings.Count(matches[2], "-")
|
||||||
if nil == rootNode {
|
if pathLen > pathLenCur {
|
||||||
node = new(VBoxSnapshot)
|
currentIndicator = matches[2]
|
||||||
rootNode = node
|
parentStack.Push(node)
|
||||||
currentIndicator = matches[2]
|
} else if pathLen < pathLenCur {
|
||||||
} else {
|
currentIndicator = matches[2]
|
||||||
pathLenCur := strings.Count(currentIndicator, "-")
|
for i := 0; i < pathLenCur-pathLen; i++ {
|
||||||
pathLen := strings.Count(matches[2], "-")
|
parentStack.Pop()
|
||||||
if pathLen > pathLenCur {
|
|
||||||
currentIndicator = matches[2]
|
|
||||||
parentStack.Push(node)
|
|
||||||
} else if pathLen < pathLenCur {
|
|
||||||
currentIndicator = matches[2]
|
|
||||||
for i := 0; i < pathLenCur-pathLen; i++ {
|
|
||||||
parentStack.Pop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
node = new(VBoxSnapshot)
|
|
||||||
parent := parentStack.Peek().(*VBoxSnapshot)
|
|
||||||
if nil != parent {
|
|
||||||
node.Parent = parent
|
|
||||||
parent.Children = append(parent.Children, node)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
node.Name = matches[4]
|
}
|
||||||
} else if matches[1] == "UUID" {
|
node = new(VBoxSnapshot)
|
||||||
node.UUID = matches[4]
|
parent := parentStack.Peek().(*VBoxSnapshot)
|
||||||
|
if nil != parent {
|
||||||
|
node.Parent = parent
|
||||||
|
parent.Children = append(parent.Children, node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
node.Name = matches[4]
|
||||||
log.Printf("Invalid key,value pair [%s]", txt)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rootNode, nil
|
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