Skip to content

Commit

Permalink
list/tree: Group same tags together
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <[email protected]>
  • Loading branch information
vvoland committed Jul 18, 2024
1 parent 587ef0c commit ef3354d
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
return err
}

var view []topImage
view := make([]topImage, 0, len(images))
for _, img := range images {
details := imageDetails{
ID: img.ID,
DiskUsage: units.HumanSizeWithPrecision(float64(img.Size), 3),
Used: img.Containers > 0,
}

var children []subImage
children := make([]subImage, 0, len(img.Manifests))
for _, im := range img.Manifests {
if im.Kind != imagetypes.ImageManifestKindImage {
continue
Expand All @@ -62,13 +62,11 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
children = append(children, sub)
}

for _, tag := range img.RepoTags {
view = append(view, topImage{
Name: tag,
Details: details,
Children: children,
})
}
view = append(view, topImage{
Names: img.RepoTags,
Details: details,
Children: children,
})
}

return printImageTree(dockerCLI, view)
Expand All @@ -81,7 +79,7 @@ type imageDetails struct {
}

type topImage struct {
Name string
Names []string
Details imageDetails
Children []subImage
}
Expand Down Expand Up @@ -115,8 +113,10 @@ func printImageTree(dockerCLI command.Cli, images []topImage) error {

maxImageName := len(headers[0].Title)
for _, img := range images {
if len(img.Name) > maxImageName {
maxImageName = len(img.Name)
for _, name := range img.Names {
if len(name) > maxImageName {
maxImageName = len(name)
}
}
for _, sub := range img.Children {
if len(sub.Platform) > maxImageName {
Expand Down Expand Up @@ -170,7 +170,12 @@ func printImageTree(dockerCLI command.Cli, images []topImage) error {

// Print images
for _, img := range images {
fmt.Fprint(out, headers[0].Print(topNameColor, img.Name))
for idx, name := range img.Names {
fmt.Fprint(out, headers[0].Print(topNameColor, name))
if idx != len(img.Names)-1 {
fmt.Fprintln(out, "")
}
}
fmt.Fprint(out, strings.Repeat(" ", spacing))

printDetails(normalColor, img.Details)
Expand Down

0 comments on commit ef3354d

Please sign in to comment.