Skip to content

Commit c212da6

Browse files
committed
images/list: Add print ambiguous warning for tree
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent 44648be commit c212da6

2 files changed

Lines changed: 18 additions & 21 deletions

File tree

cli/command/image/list.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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

cli/command/image/tree.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ 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 := opts.images
4141

4242
view := treeView{
@@ -46,7 +46,7 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
4646

4747
for _, img := range images {
4848
if ctx.Err() != nil {
49-
return ctx.Err()
49+
return 0, ctx.Err()
5050
}
5151
topDetails := imageDetails{
5252
ID: img.ID,
@@ -108,7 +108,8 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
108108
return view.images[i].created > view.images[j].created
109109
})
110110

111-
return printImageTree(dockerCLI, view)
111+
printImageTree(dockerCLI, view)
112+
return len(view.images), nil
112113
}
113114

114115
type imageDetails struct {
@@ -191,7 +192,7 @@ func getPossibleChips(view treeView) (chips []imageChip) {
191192
return possible
192193
}
193194

194-
func printImageTree(dockerCLI command.Cli, view treeView) error {
195+
func printImageTree(dockerCLI command.Cli, view treeView) {
195196
if streamRedirected(dockerCLI.Out()) {
196197
_, _ = fmt.Fprintln(dockerCLI.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
197198
}
@@ -298,8 +299,6 @@ func printImageTree(dockerCLI command.Cli, view treeView) error {
298299
printChildren(out, columns, img, normalColor)
299300
_, _ = fmt.Fprintln(out)
300301
}
301-
302-
return nil
303302
}
304303

305304
// adjustColumns adjusts the width of the first column to maximize the space
@@ -341,7 +340,6 @@ func generateLegend(out tui.Output, width uint) string {
341340
legend += " |"
342341
}
343342
}
344-
legend += " "
345343

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

0 commit comments

Comments
 (0)