summaryrefslogtreecommitdiff
path: root/cmd/podman/tree.go
diff options
context:
space:
mode:
authorSascha Grunert <sgrunert@suse.com>2020-02-05 12:07:44 +0100
committerSascha Grunert <sgrunert@suse.com>2020-02-17 11:12:35 +0100
commit93358ef915f639e52088b0f6aec52e77d3da0af7 (patch)
tree797e7b98b6efe2b0bdb74e93146b53c798367308 /cmd/podman/tree.go
parentff0f8388138f7b66c4312db0e984f0bedcac2558 (diff)
downloadpodman-93358ef915f639e52088b0f6aec52e77d3da0af7.tar.gz
podman-93358ef915f639e52088b0f6aec52e77d3da0af7.tar.bz2
podman-93358ef915f639e52088b0f6aec52e77d3da0af7.zip
Refactor image tree for API usage
Signed-off-by: Sascha Grunert <sgrunert@suse.com>
Diffstat (limited to 'cmd/podman/tree.go')
-rw-r--r--cmd/podman/tree.go97
1 files changed, 2 insertions, 95 deletions
diff --git a/cmd/podman/tree.go b/cmd/podman/tree.go
index 69b42639d..28c770f0c 100644
--- a/cmd/podman/tree.go
+++ b/cmd/podman/tree.go
@@ -1,23 +1,14 @@
package main
import (
- "context"
"fmt"
"github.com/containers/libpod/cmd/podman/cliconfig"
- "github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/adapter"
- "github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
-const (
- middleItem = "├── "
- continueItem = "│ "
- lastItem = "└── "
-)
-
var (
treeCommand cliconfig.TreeValues
@@ -56,95 +47,11 @@ func treeCmd(c *cliconfig.TreeValues) error {
return errors.Wrapf(err, "error creating libpod runtime")
}
defer runtime.DeferredShutdown(false)
- imageInfo, layerInfoMap, img, err := runtime.Tree(c.InputArgs[0])
- if err != nil {
- return err
- }
- return printTree(imageInfo, layerInfoMap, img, c.WhatRequires)
-}
-func printTree(imageInfo *image.InfoImage, layerInfoMap map[string]*image.LayerInfo, img *adapter.ContainerImage, whatRequires bool) error {
- size, err := img.Size(context.Background())
+ tree, err := runtime.ImageTree(c.InputArgs[0], c.WhatRequires)
if err != nil {
return err
}
-
- fmt.Printf("Image ID: %s\n", imageInfo.ID[:12])
- fmt.Printf("Tags:\t %s\n", imageInfo.Tags)
- fmt.Printf("Size:\t %v\n", units.HumanSizeWithPrecision(float64(*size), 4))
- if img.TopLayer() != "" {
- fmt.Printf("Image Layers\n")
- } else {
- fmt.Printf("No Image Layers\n")
- }
-
- if !whatRequires {
- // fill imageInfo with layers associated with image.
- // the layers will be filled such that
- // (Start)RootLayer->...intermediate Parent Layer(s)-> TopLayer(End)
- // Build output from imageInfo into buffer
- printImageHierarchy(imageInfo)
-
- } else {
- // fill imageInfo with layers associated with image.
- // the layers will be filled such that
- // (Start)TopLayer->...intermediate Child Layer(s)-> Child TopLayer(End)
- // (Forks)... intermediate Child Layer(s) -> Child Top Layer(End)
- return printImageChildren(layerInfoMap, img.TopLayer(), "", true)
- }
- return nil
-}
-
-// Stores all children layers which are created using given Image.
-// Layers are stored as follows
-// (Start)TopLayer->...intermediate Child Layer(s)-> Child TopLayer(End)
-// (Forks)... intermediate Child Layer(s) -> Child Top Layer(End)
-func printImageChildren(layerMap map[string]*image.LayerInfo, layerID string, prefix string, last bool) error {
- if layerID == "" {
- return nil
- }
- ll, ok := layerMap[layerID]
- if !ok {
- return fmt.Errorf("lookup error: layerid %s, not found", layerID)
- }
- fmt.Print(prefix)
-
- //initialize intend with middleItem to reduce middleItem checks.
- intend := middleItem
- if !last {
- // add continueItem i.e. '|' for next iteration prefix
- prefix += continueItem
- } else if len(ll.ChildID) > 1 || len(ll.ChildID) == 0 {
- // The above condition ensure, alignment happens for node, which has more then 1 children.
- // If node is last in printing hierarchy, it should not be printed as middleItem i.e. ├──
- intend = lastItem
- prefix += " "
- }
-
- var tags string
- if len(ll.RepoTags) > 0 {
- tags = fmt.Sprintf(" Top Layer of: %s", ll.RepoTags)
- }
- fmt.Printf("%sID: %s Size: %7v%s\n", intend, ll.ID[:12], units.HumanSizeWithPrecision(float64(ll.Size), 4), tags)
- for count, childID := range ll.ChildID {
- if err := printImageChildren(layerMap, childID, prefix, count == len(ll.ChildID)-1); err != nil {
- return err
- }
- }
+ fmt.Print(tree)
return nil
}
-
-// prints the layers info of image
-func printImageHierarchy(imageInfo *image.InfoImage) {
- for count, l := range imageInfo.Layers {
- var tags string
- intend := middleItem
- if len(l.RepoTags) > 0 {
- tags = fmt.Sprintf(" Top Layer of: %s", l.RepoTags)
- }
- if count == len(imageInfo.Layers)-1 {
- intend = lastItem
- }
- fmt.Printf("%s ID: %s Size: %7v%s\n", intend, l.ID[:12], units.HumanSizeWithPrecision(float64(l.Size), 4), tags)
- }
-}