images/list: Add print ambiguous warning for tree

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-10-15 15:03:45 +02:00
parent c41815f17a
commit 631f32ee9d
2 changed files with 18 additions and 21 deletions

View File

@ -29,7 +29,6 @@ type imagesOptions struct {
showDigests bool
format string
filter opts.FilterOpt
calledAs string
tree bool
}
@ -45,11 +44,14 @@ func newImagesCommand(dockerCLI command.Cli) *cobra.Command {
if len(args) > 0 {
options.matchName = args[0]
}
// Pass through how the command was invoked. We use this to print
// warnings when an ambiguous argument was passed when using the
// legacy (top-level) "docker images" subcommand.
options.calledAs = cmd.CalledAs()
return runImages(cmd.Context(), dockerCLI, options)
numImages, err := runImages(cmd.Context(), dockerCLI, options)
if err != nil {
return err
}
if numImages == 0 && options.matchName != "" && cmd.CalledAs() == "images" {
printAmbiguousHint(dockerCLI.Err(), options.matchName)
}
return nil
},
Annotations: map[string]string{
"category-top": "7",
@ -82,7 +84,7 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command {
return &cmd
}
func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions) error {
func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions) (int, error) {
filters := options.filter.Value()
if options.matchName != "" {
filters.Add("reference", options.matchName)
@ -90,7 +92,7 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
useTree, err := shouldUseTree(options)
if err != nil {
return err
return 0, err
}
listOpts := client.ImageListOptions{
@ -101,7 +103,7 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
res, err := dockerCLI.Client().ImageList(ctx, listOpts)
if err != nil {
return err
return 0, err
}
images := res.Items
@ -138,12 +140,9 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
Digest: options.showDigests,
}
if err := formatter.ImageWrite(imageCtx, images); err != nil {
return err
return 0, err
}
if options.matchName != "" && len(images) == 0 && options.calledAs == "images" {
printAmbiguousHint(dockerCLI.Err(), options.matchName)
}
return nil
return len(images), nil
}
func shouldUseTree(options imagesOptions) (bool, error) {

View File

@ -37,7 +37,7 @@ type treeView struct {
imageSpacing bool
}
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error {
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) (int, error) {
images := opts.images
view := treeView{
@ -47,7 +47,7 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
for _, img := range images {
if ctx.Err() != nil {
return ctx.Err()
return 0, ctx.Err()
}
topDetails := imageDetails{
ID: img.ID,
@ -141,7 +141,8 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
return strings.Compare(nameA, nameB)
})
return printImageTree(dockerCLI, view)
printImageTree(dockerCLI, view)
return len(view.images), nil
}
type imageDetails struct {
@ -224,7 +225,7 @@ func getPossibleChips(view treeView) (chips []imageChip) {
return possible
}
func printImageTree(dockerCLI command.Cli, view treeView) error {
func printImageTree(dockerCLI command.Cli, view treeView) {
if streamRedirected(dockerCLI.Out()) {
_, _ = fmt.Fprintln(dockerCLI.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
}
@ -331,8 +332,6 @@ func printImageTree(dockerCLI command.Cli, view treeView) error {
printChildren(out, columns, img, normalColor)
_, _ = fmt.Fprintln(out)
}
return nil
}
// adjustColumns adjusts the width of the first column to maximize the space
@ -374,7 +373,6 @@ func generateLegend(out tui.Output, width uint) string {
legend += " |"
}
}
legend += " "
r := int(width) - tui.Width(legend)
if r < 0 {