Skip to content

Commit 9dde80a

Browse files
authored
Merge pull request #6574 from vvoland/img-dangling
image/list: Hide untagged images without `--all`
2 parents fcac1d5 + 64805c2 commit 9dde80a

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

cli/command/image/list.go

Lines changed: 26 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,32 @@ 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+
if _, ok := filters["dangling"]; ok {
120+
images = slices.DeleteFunc(images, isDangling)
121+
}
122+
}
123+
124+
if options.tree {
125+
return runTree(ctx, dockerCLI, treeOptions{
126+
images: images,
127+
all: options.all,
128+
filters: filters,
129+
})
130+
}
115131

116132
format := options.format
117133
if len(format) == 0 {
@@ -130,10 +146,10 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
130146
},
131147
Digest: options.showDigests,
132148
}
133-
if err := formatter.ImageWrite(imageCtx, images.Items); err != nil {
149+
if err := formatter.ImageWrite(imageCtx, images); err != nil {
134150
return err
135151
}
136-
if options.matchName != "" && len(images.Items) == 0 && options.calledAs == "images" {
152+
if options.matchName != "" && len(images) == 0 && options.calledAs == "images" {
137153
printAmbiguousHint(dockerCLI.Err(), options.matchName)
138154
}
139155
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)