Skip to content

Commit 062ef96

Browse files
committed
images/list: Add print ambiguous warning for tree
Signed-off-by: Paweł Gronowski <[email protected]>
1 parent cbbc86a commit 062ef96

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

cli/command/image/list.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type imagesOptions struct {
2626
showDigests bool
2727
format string
2828
filter opts.FilterOpt
29-
calledAs string
3029
tree bool
3130
}
3231

@@ -42,11 +41,14 @@ func newImagesCommand(dockerCLI command.Cli) *cobra.Command {
4241
if len(args) > 0 {
4342
options.matchName = args[0]
4443
}
45-
// Pass through how the command was invoked. We use this to print
46-
// warnings when an ambiguous argument was passed when using the
47-
// legacy (top-level) "docker images" subcommand.
48-
options.calledAs = cmd.CalledAs()
49-
return runImages(cmd.Context(), dockerCLI, options)
44+
numImages, err := runImages(cmd.Context(), dockerCLI, options)
45+
if err != nil {
46+
return err
47+
}
48+
if numImages == 0 && options.matchName != "" && cmd.CalledAs() == "images" {
49+
printAmbiguousHint(dockerCLI.Err(), options.matchName)
50+
}
51+
return nil
5052
},
5153
Annotations: map[string]string{
5254
"category-top": "7",
@@ -79,15 +81,15 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command {
7981
return &cmd
8082
}
8183

82-
func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions) error {
84+
func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions) (int, error) {
8385
filters := options.filter.Value()
8486
if options.matchName != "" {
8587
filters.Add("reference", options.matchName)
8688
}
8789

8890
useTree, err := shouldUseTree(options)
8991
if err != nil {
90-
return err
92+
return 0, err
9193
}
9294

9395
if useTree {
@@ -103,7 +105,7 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
103105
Filters: filters,
104106
})
105107
if err != nil {
106-
return err
108+
return 0, err
107109
}
108110

109111
format := options.format
@@ -124,12 +126,9 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
124126
Digest: options.showDigests,
125127
}
126128
if err := formatter.ImageWrite(imageCtx, images); err != nil {
127-
return err
128-
}
129-
if options.matchName != "" && len(images) == 0 && options.calledAs == "images" {
130-
printAmbiguousHint(dockerCLI.Err(), options.matchName)
129+
return 0, err
131130
}
132-
return nil
131+
return len(images), nil
133132
}
134133

135134
func shouldUseTree(options imagesOptions) (bool, error) {

cli/command/image/tree.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ type treeView struct {
3636
imageSpacing bool
3737
}
3838

39-
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error {
39+
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) (int, error) {
4040
images, err := dockerCLI.Client().ImageList(ctx, client.ImageListOptions{
4141
All: opts.all,
4242
Filters: opts.filters,
4343
Manifests: true,
4444
})
4545
if err != nil {
46-
return err
46+
return 0, err
4747
}
4848
if !opts.all {
4949
images = slices.DeleteFunc(images, isDangling)
@@ -115,7 +115,7 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
115115
return view.images[i].created > view.images[j].created
116116
})
117117

118-
return printImageTree(dockerCLI, view)
118+
return len(view.images), printImageTree(dockerCLI, view)
119119
}
120120

121121
type imageDetails struct {
@@ -348,7 +348,6 @@ func generateLegend(out tui.Output, width uint) string {
348348
legend += " |"
349349
}
350350
}
351-
legend += " "
352351

353352
r := int(width) - tui.Width(legend)
354353
if r < 0 {

0 commit comments

Comments
 (0)