@@ -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" ,
@@ -83,15 +85,15 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command {
8385}
8486
8587//nolint:gocyclo
86- func runImages (ctx context.Context , dockerCLI command.Cli , options imagesOptions ) error {
88+ func runImages (ctx context.Context , dockerCLI command.Cli , options imagesOptions ) ( int , error ) {
8789 filters := options .filter .Value ()
8890 if options .matchName != "" {
8991 filters .Add ("reference" , options .matchName )
9092 }
9193
9294 useTree , err := shouldUseTree (options )
9395 if err != nil {
94- return err
96+ return 0 , err
9597 }
9698
9799 listOpts := client.ImageListOptions {
@@ -102,7 +104,7 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
102104
103105 res , err := dockerCLI .Client ().ImageList (ctx , listOpts )
104106 if err != nil {
105- return err
107+ return 0 , err
106108 }
107109
108110 images := res .Items
@@ -139,12 +141,9 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
139141 Digest : options .showDigests ,
140142 }
141143 if err := formatter .ImageWrite (imageCtx , images ); err != nil {
142- return err
143- }
144- if options .matchName != "" && len (images ) == 0 && options .calledAs == "images" {
145- printAmbiguousHint (dockerCLI .Err (), options .matchName )
144+ return 0 , err
146145 }
147- return nil
146+ return len ( images ), nil
148147}
149148
150149func shouldUseTree (options imagesOptions ) (bool , error ) {
0 commit comments