From 37d87455894a997822f871f51db92e6e77098952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Fri, 24 Jan 2025 17:23:22 +0100 Subject: [PATCH] image/list: Add opt-in to --tree by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new `image-tree` feature in `~/.docker/config.json` which will make the `docker image list` use the new tree output by default. The tree output is only used if no conflicting flag is passed (--format, --quiet, --digests, --no-trunc). Signed-off-by: Paweł Gronowski --- cli/command/image/list.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/cli/command/image/list.go b/cli/command/image/list.go index cfe73c5adeb3..a79552dfec76 100644 --- a/cli/command/image/list.go +++ b/cli/command/image/list.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "strconv" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -81,6 +82,8 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions filters.Add("reference", options.matchName) } + options = applyListFeatures(dockerCLI, options) + if options.tree { if options.quiet { return errors.New("--quiet is not yet supported with --tree") @@ -167,3 +170,31 @@ func printAmbiguousHint(stdErr io.Writer, matchName string) { _, _ = fmt.Fprintf(stdErr, "\nNo images found matching %q: did you mean \"docker image %[1]s\"?\n", matchName) } } + +// applyListFeatures checks for feature flags in the config file +// and adjusts the options accordingly. +// Supported features: +// image-tree => --tree option is implied when possible +func applyListFeatures(dockerCLI command.Cli, options imagesOptions) imagesOptions { + cfg := dockerCLI.ConfigFile() + var treeOptIn bool + if v, ok := cfg.Features["image-tree"]; ok { + enabled, err := strconv.ParseBool(v) + if err != nil { + return options + } + if enabled { + treeOptIn = true + } + } + + if !options.tree && treeOptIn { + treeOptIn = !options.quiet + treeOptIn = treeOptIn && !options.noTrunc + treeOptIn = treeOptIn && !options.showDigests + treeOptIn = treeOptIn && options.format == "" + options.tree = treeOptIn + } + + return options +}