diff --git a/cli/command/image/list.go b/cli/command/image/list.go index 2ac5f9e6c4d7..d267c4778bea 100644 --- a/cli/command/image/list.go +++ b/cli/command/image/list.go @@ -1,3 +1,5 @@ +//go:build go1.23 + package image import ( @@ -5,6 +7,7 @@ import ( "errors" "fmt" "io" + "slices" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -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 != "" { @@ -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 { + 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 { @@ -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 diff --git a/cli/command/image/tree.go b/cli/command/image/tree.go index c45dc4a906be..383a26522140 100644 --- a/cli/command/image/tree.go +++ b/cli/command/image/tree.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "os" - "slices" "sort" "strings" @@ -24,6 +23,7 @@ import ( ) type treeOptions struct { + images []imagetypes.Summary all bool filters client.Filters } @@ -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),