Skip to content

Commit e9a9410

Browse files
committed
image/list: Hide untagged images without --all
The `--tree` implementation already does this. Make the behavior consistent for the legacy image list implementation. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent f74cd14 commit e9a9410

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

cli/command/image/list.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
//go:build go1.23
2+
13
package image
24

35
import (
46
"context"
57
"errors"
68
"fmt"
79
"io"
10+
"slices"
811

912
"github.com/docker/cli/cli"
1013
"github.com/docker/cli/cli/command"
@@ -79,6 +82,7 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command {
7982
return &cmd
8083
}
8184

85+
//nolint:gocyclo
8286
func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions) error {
8387
filters := options.filter.Value()
8488
if options.matchName != "" {
@@ -98,20 +102,30 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
98102
if options.format != "" {
99103
return errors.New("--format is not yet supported with --tree")
100104
}
105+
}
101106

102-
return runTree(ctx, dockerCLI, treeOptions{
103-
all: options.all,
104-
filters: filters,
105-
})
107+
listOpts := client.ImageListOptions{
108+
All: options.all,
109+
Filters: filters,
110+
Manifests: options.tree,
106111
}
107112

108-
images, err := dockerCLI.Client().ImageList(ctx, client.ImageListOptions{
109-
All: options.all,
110-
Filters: filters,
111-
})
113+
res, err := dockerCLI.Client().ImageList(ctx, listOpts)
112114
if err != nil {
113115
return err
114116
}
117+
images := res.Items
118+
if !options.all {
119+
images = slices.DeleteFunc(images, isDangling)
120+
}
121+
122+
if options.tree {
123+
return runTree(ctx, dockerCLI, treeOptions{
124+
images: images,
125+
all: options.all,
126+
filters: filters,
127+
})
128+
}
115129

116130
format := options.format
117131
if len(format) == 0 {
@@ -130,10 +144,10 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
130144
},
131145
Digest: options.showDigests,
132146
}
133-
if err := formatter.ImageWrite(imageCtx, images.Items); err != nil {
147+
if err := formatter.ImageWrite(imageCtx, images); err != nil {
134148
return err
135149
}
136-
if options.matchName != "" && len(images.Items) == 0 && options.calledAs == "images" {
150+
if options.matchName != "" && len(images) == 0 && options.calledAs == "images" {
137151
printAmbiguousHint(dockerCLI.Err(), options.matchName)
138152
}
139153
return nil

cli/command/image/tree.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"os"
10-
"slices"
1110
"sort"
1211
"strings"
1312

@@ -24,6 +23,7 @@ import (
2423
)
2524

2625
type treeOptions struct {
26+
images []imagetypes.Summary
2727
all bool
2828
filters client.Filters
2929
}
@@ -36,24 +36,17 @@ type treeView struct {
3636
}
3737

3838
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error {
39-
res, err := dockerCLI.Client().ImageList(ctx, client.ImageListOptions{
40-
All: opts.all,
41-
Filters: opts.filters,
42-
Manifests: true,
43-
})
44-
if err != nil {
45-
return err
46-
}
47-
if !opts.all {
48-
res.Items = slices.DeleteFunc(res.Items, isDangling)
49-
}
39+
images := opts.images
5040

5141
view := treeView{
52-
images: make([]topImage, 0, len(res.Items)),
42+
images: make([]topImage, 0, len(images)),
5343
}
5444
attested := make(map[digest.Digest]bool)
5545

56-
for _, img := range res.Items {
46+
for _, img := range images {
47+
if ctx.Err() != nil {
48+
return ctx.Err()
49+
}
5750
details := imageDetails{
5851
ID: img.ID,
5952
DiskUsage: units.HumanSizeWithPrecision(float64(img.Size), 3),

0 commit comments

Comments
 (0)