Skip to content

Commit 941bfeb

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 <pawel.gronowski@docker.com>
1 parent f74cd14 commit 941bfeb

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

cli/command/image/list.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"slices"
89

910
"github.com/docker/cli/cli"
1011
"github.com/docker/cli/cli/command"
@@ -98,20 +99,30 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
9899
if options.format != "" {
99100
return errors.New("--format is not yet supported with --tree")
100101
}
102+
}
101103

102-
return runTree(ctx, dockerCLI, treeOptions{
103-
all: options.all,
104-
filters: filters,
105-
})
104+
listOpts := client.ImageListOptions{
105+
All: options.all,
106+
Filters: filters,
107+
Manifests: options.tree,
106108
}
107109

108-
images, err := dockerCLI.Client().ImageList(ctx, client.ImageListOptions{
109-
All: options.all,
110-
Filters: filters,
111-
})
110+
res, err := dockerCLI.Client().ImageList(ctx, listOpts)
112111
if err != nil {
113112
return err
114113
}
114+
images := res.Items
115+
if !options.all {
116+
images = slices.DeleteFunc(images, isDangling)
117+
}
118+
119+
if options.tree {
120+
return runTree(ctx, dockerCLI, treeOptions{
121+
images: images,
122+
all: options.all,
123+
filters: filters,
124+
})
125+
}
115126

116127
format := options.format
117128
if len(format) == 0 {
@@ -130,10 +141,10 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
130141
},
131142
Digest: options.showDigests,
132143
}
133-
if err := formatter.ImageWrite(imageCtx, images.Items); err != nil {
144+
if err := formatter.ImageWrite(imageCtx, images); err != nil {
134145
return err
135146
}
136-
if options.matchName != "" && len(images.Items) == 0 && options.calledAs == "images" {
147+
if options.matchName != "" && len(images) == 0 && options.calledAs == "images" {
137148
printAmbiguousHint(dockerCLI.Err(), options.matchName)
138149
}
139150
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)