1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

fix docker images -tree <invalid_image> and docker images -viz <image_name>

This commit is contained in:
Victor Vieux 2013-12-09 10:43:58 -08:00
parent cdc07f7d5c
commit ac1093b83a

View file

@ -1102,33 +1102,9 @@ func (cli *DockerCli) CmdImages(args ...string) error {
return nil
}
if *flViz {
body, _, err := cli.call("GET", "/images/json?all=1", nil)
if err != nil {
return err
}
filter := cmd.Arg(0)
var outs []APIImages
err = json.Unmarshal(body, &outs)
if err != nil {
return err
}
fmt.Fprintf(cli.out, "digraph docker {\n")
for _, image := range outs {
if image.ParentId == "" {
fmt.Fprintf(cli.out, " base -> \"%s\" [style=invis]\n", utils.TruncateID(image.ID))
} else {
fmt.Fprintf(cli.out, " \"%s\" -> \"%s\"\n", utils.TruncateID(image.ParentId), utils.TruncateID(image.ID))
}
if image.RepoTags[0] != "<none>:<none>" {
fmt.Fprintf(cli.out, " \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n", utils.TruncateID(image.ID), utils.TruncateID(image.ID), strings.Join(image.RepoTags, "\\n"))
}
}
fmt.Fprintf(cli.out, " base [style=invisible]\n}\n")
} else if *flTree {
if *flViz || *flTree {
body, _, err := cli.call("GET", "/images/json?all=1", nil)
if err != nil {
return err
@ -1140,8 +1116,8 @@ func (cli *DockerCli) CmdImages(args ...string) error {
}
var (
startImageArg = cmd.Arg(0)
startImage APIImages
printNode func(cli *DockerCli, noTrunc bool, image APIImages, prefix string)
startImage APIImages
roots []APIImages
byParent = make(map[string][]APIImages)
@ -1158,28 +1134,38 @@ func (cli *DockerCli) CmdImages(args ...string) error {
}
}
if startImageArg != "" {
if startImageArg == image.ID || startImageArg == utils.TruncateID(image.ID) {
if filter != "" {
if filter == image.ID || filter == utils.TruncateID(image.ID) {
startImage = image
}
for _, repotag := range image.RepoTags {
if repotag == startImageArg {
if repotag == filter {
startImage = image
}
}
}
}
if startImageArg != "" {
WalkTree(cli, noTrunc, []APIImages{startImage}, byParent, "")
if *flViz {
fmt.Fprintf(cli.out, "digraph docker {\n")
printNode = (*DockerCli).printVizNode
} else {
WalkTree(cli, noTrunc, roots, byParent, "")
printNode = (*DockerCli).printTreeNode
}
if startImage.ID != "" {
cli.WalkTree(*noTrunc, &[]APIImages{startImage}, byParent, "", printNode)
} else if filter == "" {
cli.WalkTree(*noTrunc, &roots, byParent, "", printNode)
}
if *flViz {
fmt.Fprintf(cli.out, " base [style=invisible]\n}\n")
}
} else {
v := url.Values{}
if cmd.NArg() == 1 {
v.Set("filter", cmd.Arg(0))
v.Set("filter", filter)
}
if *all {
v.Set("all", "1")
@ -1225,35 +1211,58 @@ func (cli *DockerCli) CmdImages(args ...string) error {
return nil
}
func WalkTree(cli *DockerCli, noTrunc *bool, images []APIImages, byParent map[string][]APIImages, prefix string) {
if len(images) > 1 {
length := len(images)
for index, image := range images {
func (cli *DockerCli) WalkTree(noTrunc bool, images *[]APIImages, byParent map[string][]APIImages, prefix string, printNode func(cli *DockerCli, noTrunc bool, image APIImages, prefix string)) {
length := len(*images)
if length > 1 {
for index, image := range *images {
if index+1 == length {
PrintTreeNode(cli, noTrunc, image, prefix+"└─")
printNode(cli, noTrunc, image, prefix+"└─")
if subimages, exists := byParent[image.ID]; exists {
WalkTree(cli, noTrunc, subimages, byParent, prefix+" ")
cli.WalkTree(noTrunc, &subimages, byParent, prefix+" ", printNode)
}
} else {
PrintTreeNode(cli, noTrunc, image, prefix+"|─")
printNode(cli, noTrunc, image, prefix+"|─")
if subimages, exists := byParent[image.ID]; exists {
WalkTree(cli, noTrunc, subimages, byParent, prefix+"| ")
cli.WalkTree(noTrunc, &subimages, byParent, prefix+"| ", printNode)
}
}
}
} else {
for _, image := range images {
PrintTreeNode(cli, noTrunc, image, prefix+"└─")
for _, image := range *images {
printNode(cli, noTrunc, image, prefix+"└─")
if subimages, exists := byParent[image.ID]; exists {
WalkTree(cli, noTrunc, subimages, byParent, prefix+" ")
cli.WalkTree(noTrunc, &subimages, byParent, prefix+" ", printNode)
}
}
}
}
func PrintTreeNode(cli *DockerCli, noTrunc *bool, image APIImages, prefix string) {
func (cli *DockerCli) printVizNode(noTrunc bool, image APIImages, prefix string) {
var (
imageID string
parentID string
)
if noTrunc {
imageID = image.ID
parentID = image.ParentId
} else {
imageID = utils.TruncateID(image.ID)
parentID = utils.TruncateID(image.ParentId)
}
if image.ParentId == "" {
fmt.Fprintf(cli.out, " base -> \"%s\" [style=invis]\n", imageID)
} else {
fmt.Fprintf(cli.out, " \"%s\" -> \"%s\"\n", parentID, imageID)
}
if image.RepoTags[0] != "<none>:<none>" {
fmt.Fprintf(cli.out, " \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n",
imageID, imageID, strings.Join(image.RepoTags, "\\n"))
}
}
func (cli *DockerCli) printTreeNode(noTrunc bool, image APIImages, prefix string) {
var imageID string
if *noTrunc {
if noTrunc {
imageID = image.ID
} else {
imageID = utils.TruncateID(image.ID)