From 37e02ff211aafd0d375d80a24cfccfc558298e81 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 10 Apr 2023 01:14:21 +0200 Subject: [PATCH] docker ps: print warning if both --format and --quiet are set Of both "--quiet" and "--format" are set, --quiet takes precedence. This patch adds a warning to inform the user that their custom format is not used: docker ps --format='{{.Image}}' ubuntu:22.04 alpine docker ps --format='{{.Image}}' --quiet WARNING: Ignoring custom format, because both --format and --quiet are set. 40111f61d5c5 482efdf39fac The warning is printed on STDERR, so can be redirected: docker ps --format='{{.Image}}' --quiet 2> /dev/null 40111f61d5c5 482efdf39fac The warning is only shown if the format is set using the "--format" option. No warning is shown if a custom format is set through the CLI configuration file: mkdir -p ~/.docker/ echo '{"psFormat": "{{.Image}}"}' > ~/.docker/config.json docker ps ubuntu:22.04 alpine docker ps --quiet 40111f61d5c5 482efdf39fac Signed-off-by: Sebastiaan van Stijn --- cli/command/container/list.go | 2 ++ cli/command/container/list_test.go | 1 + 2 files changed, 3 insertions(+) diff --git a/cli/command/container/list.go b/cli/command/container/list.go index 3571954b5..471e4e1c4 100644 --- a/cli/command/container/list.go +++ b/cli/command/container/list.go @@ -120,6 +120,8 @@ func runPs(dockerCli command.Cli, options *psOptions) error { if len(options.format) == 0 { // load custom psFormat from CLI config (if any) options.format = dockerCli.ConfigFile().PsFormat + } else if options.quiet { + _, _ = dockerCli.Err().Write([]byte("WARNING: Ignoring custom format, because both --format and --quiet are set.\n")) } listOptions, err := buildContainerListOptions(options) diff --git a/cli/command/container/list_test.go b/cli/command/container/list_test.go index a071b2e30..a00783775 100644 --- a/cli/command/container/list_test.go +++ b/cli/command/container/list_test.go @@ -324,6 +324,7 @@ func TestContainerListWithFormat(t *testing.T) { assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}")) assert.Check(t, cmd.Flags().Set("quiet", "true")) assert.NilError(t, cmd.Execute()) + assert.Equal(t, cli.ErrBuffer().String(), "WARNING: Ignoring custom format, because both --format and --quiet are set.\n") golden.Assert(t, cli.OutBuffer().String(), "container-list-quiet.golden") }) }