@@ -29,7 +29,6 @@ type imagesOptions struct {
2929 showDigests bool
3030 format string
3131 filter opts.FilterOpt
32- calledAs string
3332 tree bool
3433}
3534
@@ -45,11 +44,14 @@ func newImagesCommand(dockerCLI command.Cli) *cobra.Command {
4544 if len (args ) > 0 {
4645 options .matchName = args [0 ]
4746 }
48- // Pass through how the command was invoked. We use this to print
49- // warnings when an ambiguous argument was passed when using the
50- // legacy (top-level) "docker images" subcommand.
51- options .calledAs = cmd .CalledAs ()
52- return runImages (cmd .Context (), dockerCLI , options )
47+ numImages , err := runImages (cmd .Context (), dockerCLI , options )
48+ if err != nil {
49+ return err
50+ }
51+ if numImages == 0 && options .matchName != "" && cmd .CalledAs () == "images" {
52+ printAmbiguousHint (dockerCLI .Err (), options .matchName )
53+ }
54+ return nil
5355 },
5456 Annotations : map [string ]string {
5557 "category-top" : "7" ,
@@ -82,15 +84,15 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command {
8284 return & cmd
8385}
8486
85- func runImages (ctx context.Context , dockerCLI command.Cli , options imagesOptions ) error {
87+ func runImages (ctx context.Context , dockerCLI command.Cli , options imagesOptions ) ( int , error ) {
8688 filters := options .filter .Value ()
8789 if options .matchName != "" {
8890 filters .Add ("reference" , options .matchName )
8991 }
9092
9193 useTree , err := shouldUseTree (options )
9294 if err != nil {
93- return err
95+ return 0 , err
9496 }
9597
9698 listOpts := client.ImageListOptions {
@@ -101,7 +103,7 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
101103
102104 res , err := dockerCLI .Client ().ImageList (ctx , listOpts )
103105 if err != nil {
104- return err
106+ return 0 , err
105107 }
106108
107109 images := res .Items
@@ -138,12 +140,9 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
138140 Digest : options .showDigests ,
139141 }
140142 if err := formatter .ImageWrite (imageCtx , images ); err != nil {
141- return err
142- }
143- if options .matchName != "" && len (images ) == 0 && options .calledAs == "images" {
144- printAmbiguousHint (dockerCLI .Err (), options .matchName )
143+ return 0 , err
145144 }
146- return nil
145+ return len ( images ), nil
147146}
148147
149148func shouldUseTree (options imagesOptions ) (bool , error ) {
0 commit comments