From f1a240fb6a201d05cea5b77033e7fa4892ee1bbf Mon Sep 17 00:00:00 2001 From: Nate Jones Date: Sat, 1 Aug 2015 20:29:29 -0700 Subject: [PATCH] support passing repo name as starting point for -t fixes #7 --- images.go | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/images.go b/images.go index c2bc14b..bfe3289 100644 --- a/images.go +++ b/images.go @@ -82,12 +82,46 @@ func (x *ImagesCommand) Execute(args []string) error { fmt.Printf(jsonToDot(images)) } else if imagesCommand.Tree { - var startImageArg = "" + var startImage = "" if len(args) > 0 { - startImageArg = args[0] + + // 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") { + startImageRepo = fmt.Sprintf("%s:latest", startImageRepo) + } + + IMAGES: + for _, image := range *images { + // check if the start image arg matches an image id + if strings.Index(image.Id, startImageArg) == 0 { + startImage = startImageArg + 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 + } + } + } + } + + if startImage == "" { + return fmt.Errorf("Unable to find image %s.", startImageArg) + } } - fmt.Printf(jsonToTree(images, startImageArg, imagesCommand.NoTruncate)) + fmt.Printf(jsonToTree(images, startImage, imagesCommand.NoTruncate)) } else if imagesCommand.Short { fmt.Printf(jsonToShort(images)) } else {