Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions cli/command/image/list.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//go:build go1.23

package image

import (
"context"
"errors"
"fmt"
"io"
"slices"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -79,6 +82,7 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command {
return &cmd
}

//nolint:gocyclo
func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions) error {
filters := options.filter.Value()
if options.matchName != "" {
Expand All @@ -98,20 +102,32 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
if options.format != "" {
return errors.New("--format is not yet supported with --tree")
}
}

return runTree(ctx, dockerCLI, treeOptions{
all: options.all,
filters: filters,
})
listOpts := client.ImageListOptions{
All: options.all,
Filters: filters,
Manifests: options.tree,
}

images, err := dockerCLI.Client().ImageList(ctx, client.ImageListOptions{
All: options.all,
Filters: filters,
})
res, err := dockerCLI.Client().ImageList(ctx, listOpts)
if err != nil {
return err
}
images := res.Items
if !options.all {
if _, ok := filters["dangling"]; ok {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure about this, but I think it makes sense to NOT manipulate the dangling images if --all wasn't passed but dangling filter was used explicitly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at my computer to test, but this means that filtering for dangling images won't produce a result unless --all is also passed? Or is this just the optimization to not process them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, without this change docker images --filter dangling=true would always result in empty image list.

images = slices.DeleteFunc(images, isDangling)
}
}

if options.tree {
return runTree(ctx, dockerCLI, treeOptions{
images: images,
all: options.all,
filters: filters,
})
}

format := options.format
if len(format) == 0 {
Expand All @@ -130,10 +146,10 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
},
Digest: options.showDigests,
}
if err := formatter.ImageWrite(imageCtx, images.Items); err != nil {
if err := formatter.ImageWrite(imageCtx, images); err != nil {
return err
}
if options.matchName != "" && len(images.Items) == 0 && options.calledAs == "images" {
if options.matchName != "" && len(images) == 0 && options.calledAs == "images" {
printAmbiguousHint(dockerCLI.Err(), options.matchName)
}
return nil
Expand Down
21 changes: 7 additions & 14 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"os"
"slices"
"sort"
"strings"

Expand All @@ -24,6 +23,7 @@ import (
)

type treeOptions struct {
images []imagetypes.Summary
all bool
filters client.Filters
}
Expand All @@ -36,24 +36,17 @@ type treeView struct {
}

func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error {
res, err := dockerCLI.Client().ImageList(ctx, client.ImageListOptions{
All: opts.all,
Filters: opts.filters,
Manifests: true,
})
if err != nil {
return err
}
if !opts.all {
res.Items = slices.DeleteFunc(res.Items, isDangling)
}
images := opts.images

view := treeView{
images: make([]topImage, 0, len(res.Items)),
images: make([]topImage, 0, len(images)),
}
attested := make(map[digest.Digest]bool)

for _, img := range res.Items {
for _, img := range images {
if ctx.Err() != nil {
return ctx.Err()
}
details := imageDetails{
ID: img.ID,
DiskUsage: units.HumanSizeWithPrecision(float64(img.Size), 3),
Expand Down
Loading