Skip to content

Commit 37e02ff

Browse files
committed
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 <github@gone.nl>
1 parent f522905 commit 37e02ff

2 files changed

Lines changed: 3 additions & 0 deletions

File tree

cli/command/container/list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ func runPs(dockerCli command.Cli, options *psOptions) error {
120120
if len(options.format) == 0 {
121121
// load custom psFormat from CLI config (if any)
122122
options.format = dockerCli.ConfigFile().PsFormat
123+
} else if options.quiet {
124+
_, _ = dockerCli.Err().Write([]byte("WARNING: Ignoring custom format, because both --format and --quiet are set.\n"))
123125
}
124126

125127
listOptions, err := buildContainerListOptions(options)

cli/command/container/list_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ func TestContainerListWithFormat(t *testing.T) {
324324
assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}"))
325325
assert.Check(t, cmd.Flags().Set("quiet", "true"))
326326
assert.NilError(t, cmd.Execute())
327+
assert.Equal(t, cli.ErrBuffer().String(), "WARNING: Ignoring custom format, because both --format and --quiet are set.\n")
327328
golden.Assert(t, cli.OutBuffer().String(), "container-list-quiet.golden")
328329
})
329330
}

0 commit comments

Comments
 (0)