From 45e2095ae389ce19df6153bf41cac6e5ecd39c6f Mon Sep 17 00:00:00 2001 From: Thomas Meckel Date: Sat, 23 Mar 2019 22:01:40 +0100 Subject: [PATCH] Moved parsing of tree string representation to new function in snapshot.go --- builder/virtualbox/common/driver_4_2.go | 55 ++----------------------- 1 file changed, 3 insertions(+), 52 deletions(-) diff --git a/builder/virtualbox/common/driver_4_2.go b/builder/virtualbox/common/driver_4_2.go index 0e749f324..b8dc33211 100644 --- a/builder/virtualbox/common/driver_4_2.go +++ b/builder/virtualbox/common/driver_4_2.go @@ -1,7 +1,6 @@ package common import ( - "bufio" "bytes" "context" "fmt" @@ -12,7 +11,6 @@ import ( "strings" "time" - "github.com/golang-collections/collections/stack" versionUtil "github.com/hashicorp/go-version" packer "github.com/hashicorp/packer/common" ) @@ -261,56 +259,9 @@ func (d *VBox42Driver) LoadSnapshots(vmName string) (*VBoxSnapshot, error) { var rootNode *VBoxSnapshot if stdoutString != "This machine does not have any snapshots" { - scanner := bufio.NewScanner(strings.NewReader(stdoutString)) - SnapshotNamePartsRe := regexp.MustCompile("Snapshot(?PName|UUID)(?P(-[1-9]+)*)=\"(?P[^\"]*)\"") - var currentIndicator string - parentStack := stack.New() - var node *VBoxSnapshot - for scanner.Scan() { - txt := scanner.Text() - idx := strings.Index(txt, "=") - if idx > 0 { - if strings.HasPrefix(txt, "Current") { - node.IsCurrent = true - } else { - matches := SnapshotNamePartsRe.FindStringSubmatch(txt) - log.Printf("************ Snapshot %s name parts", txt) - log.Printf("Matches %#v\n", matches) - log.Printf("Node %s\n", matches[0]) - log.Printf("Type %s\n", matches[1]) - log.Printf("Path %s\n", matches[2]) - log.Printf("Leaf %s\n", matches[3]) - log.Printf("Value %s\n", matches[4]) - if matches[1] == "Name" { - if nil == rootNode { - node = new(VBoxSnapshot) - rootNode = node - currentIndicator = matches[2] - } else { - pathLenCur := strings.Count(currentIndicator, "-") - pathLen := strings.Count(matches[2], "-") - if pathLen > pathLenCur { - currentIndicator = matches[2] - parentStack.Push(node) - } else if pathLen < pathLenCur { - for i := 0; i < pathLenCur-1; i++ { - parentStack.Pop() - } - } - node = new(VBoxSnapshot) - parent := parentStack.Peek().(*VBoxSnapshot) - if nil != parent { - parent.Children = append(parent.Children, node) - } - } - node.Name = matches[4] - } else if matches[1] == "UUID" { - node.UUID = matches[4] - } - } - } else { - log.Printf("Invalid key,value pair [%s]", txt) - } + rootNode, err = ParseSnapshotData(stdoutString) + if nil != err { + return nil, err } }