1+ //go:build go1.23
2+
13package image
24
35import (
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
8286func 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
0 commit comments