From 6eeb877c5e00ab2b99d46e5c911037c9fdc4fabe Mon Sep 17 00:00:00 2001 From: gkovacs81 Date: Sun, 11 Oct 2015 18:29:48 +0200 Subject: [PATCH] Fix print tree by startimage Print tree nodes if only-labeled --- images.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/images.go b/images.go index 8525840..43be608 100644 --- a/images.go +++ b/images.go @@ -92,37 +92,33 @@ func (x *ImagesCommand) Execute(args []string) error { // attempt to find the start image, which can be specified as an // image ID or a repository name - startImageArg := args[0] startImageRepo := args[0] - // in case a repo name was specified, append ":latest" if it isn't - // already there - if !strings.HasSuffix(startImageRepo, ":latest") { + // if tag is not defined, find by :latest tag + if strings.Index(startImageRepo, ":") == -1 { startImageRepo = fmt.Sprintf("%s:latest", startImageRepo) } IMAGES: for _, image := range *images { - // check if the start image arg matches an image id + // find by image id if strings.Index(image.Id, startImageArg) == 0 { startImage = image.Id break IMAGES } - // check if the start image arg matches an repository name - if image.RepoTags[0] != ":" { - for _, repotag := range image.RepoTags { - if repotag == startImageRepo { - startImage = image.Id - break IMAGES - } + // find by image name (name and tag) + for _, repotag := range image.RepoTags { + if repotag == startImageRepo { + startImage = image.Id + break IMAGES } } } if startImage == "" { - return fmt.Errorf("Unable to find image %s.", startImageArg) + return fmt.Errorf("Unable to find image %s = %s.", startImageArg, startImageRepo) } } @@ -181,7 +177,11 @@ func WalkTree(buffer *bytes.Buffer, noTrunc bool, onlyLabeled bool, images []Ima if length > 1 { for index, image := range images { var nextPrefix string = "" - var visible bool = onlyLabeled && image.RepoTags[0] != ":" || !onlyLabeled + // image is visible + // 1. it has a label + // 2. it is root + // 3. it is a node + var visible bool = onlyLabeled && image.RepoTags[0] != ":" || !onlyLabeled || image.ParentId == "" || len(byParent[image.Id]) > 1 if visible { if index+1 == length { PrintTreeNode(buffer, noTrunc, image, prefix+"└─") @@ -198,7 +198,11 @@ func WalkTree(buffer *bytes.Buffer, noTrunc bool, onlyLabeled bool, images []Ima } else { for _, image := range images { var nextPrefix string = "" - var visible bool = onlyLabeled && image.RepoTags[0] != ":" || !onlyLabeled + // image is visible + // 1. it has a label + // 2. it is root + // 3. it is a node + var visible bool = onlyLabeled && image.RepoTags[0] != ":" || !onlyLabeled || image.ParentId == "" || len(byParent[image.Id]) > 1 if visible { PrintTreeNode(buffer, noTrunc, image, prefix+"└─") nextPrefix = " "