Skip to content

Commit

Permalink
cmd/ctr: add tree subcommand in snapshot
Browse files Browse the repository at this point in the history
Signed-off-by: Sunny Gogoi <me@darkowlzz.space>
  • Loading branch information
darkowlzz committed Jul 18, 2017
1 parent 81b893a commit 659e3d7
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions cmd/ctr/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var snapshotCommand = cli.Command{
usageSnapshotCommand,
removeSnapshotCommand,
prepareSnapshotCommand,
treeSnapshotCommand,
},
}

Expand Down Expand Up @@ -231,3 +232,72 @@ var prepareSnapshotCommand = cli.Command{
return nil
},
}

var treeSnapshotCommand = cli.Command{
Name: "tree",
Usage: "Display tree view of snapshot branches",
Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext)
defer cancel()

snapshotter, err := getSnapshotter(clicontext)
if err != nil {
return err
}

tree := make(map[string]*snapshotTreeNode)

if err := snapshotter.Walk(ctx, func(ctx context.Context, info snapshot.Info) error {
// Get or create node and add node details
node := getOrCreateTreeNode(info.Name, tree)
if info.Parent != "" {
node.Parent = info.Parent
p := getOrCreateTreeNode(info.Parent, tree)
p.Children = append(p.Children, info.Name)
}

return nil
}); err != nil {
return err
}

printTree(tree)

return nil
},
}

type snapshotTreeNode struct {
Name string
Parent string
Children []string
}

func getOrCreateTreeNode(name string, tree map[string]*snapshotTreeNode) *snapshotTreeNode {
if node, ok := tree[name]; ok {
return node
}
node := &snapshotTreeNode{
Name: name,
}
tree[name] = node
return node
}

func printTree(tree map[string]*snapshotTreeNode) {
for _, node := range tree {
// Print for root(parent-less) nodes only
if node.Parent == "" {
printNode(node.Name, tree, 0)
}
}
}

func printNode(name string, tree map[string]*snapshotTreeNode, level int) {
node := tree[name]
fmt.Printf("%s\\_ %s\n", strings.Repeat(" ", level), node.Name)
level++
for _, child := range node.Children {
printNode(child, tree, level)
}
}

0 comments on commit 659e3d7

Please sign in to comment.