Skip to content

Commit dbb1b42

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

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

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",
@@ -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

149148
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
@@ -37,7 +37,7 @@ type treeView struct {
3737
imageSpacing bool
3838
}
3939

40-
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error {
40+
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) (int, error) {
4141
images := opts.images
4242

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

4848
for _, img := range images {
4949
if ctx.Err() != nil {
50-
return ctx.Err()
50+
return 0, ctx.Err()
5151
}
5252
topDetails := imageDetails{
5353
ID: img.ID,
@@ -141,7 +141,8 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
141141
return strings.Compare(nameA, nameB)
142142
})
143143

144-
return printImageTree(dockerCLI, view)
144+
printImageTree(dockerCLI, view)
145+
return len(view.images), nil
145146
}
146147

147148
type imageDetails struct {
@@ -224,7 +225,7 @@ func getPossibleChips(view treeView) (chips []imageChip) {
224225
return possible
225226
}
226227

227-
func printImageTree(dockerCLI command.Cli, view treeView) error {
228+
func printImageTree(dockerCLI command.Cli, view treeView) {
228229
if streamRedirected(dockerCLI.Out()) {
229230
_, _ = fmt.Fprintln(dockerCLI.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
230231
}
@@ -331,8 +332,6 @@ func printImageTree(dockerCLI command.Cli, view treeView) error {
331332
printChildren(out, columns, img, normalColor)
332333
_, _ = fmt.Fprintln(out)
333334
}
334-
335-
return nil
336335
}
337336

338337
// adjustColumns adjusts the width of the first column to maximize the space
@@ -374,7 +373,6 @@ func generateLegend(out tui.Output, width uint) string {
374373
legend += " |"
375374
}
376375
}
377-
legend += " "
378376

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

0 commit comments

Comments
 (0)