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>
This commit is contained in:
Sebastiaan van Stijn 2023-04-10 01:14:21 +02:00
parent f522905595
commit 37e02ff211
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 3 additions and 0 deletions

View File

@ -120,6 +120,8 @@ func runPs(dockerCli command.Cli, options *psOptions) error {
if len(options.format) == 0 { if len(options.format) == 0 {
// load custom psFormat from CLI config (if any) // load custom psFormat from CLI config (if any)
options.format = dockerCli.ConfigFile().PsFormat 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) listOptions, err := buildContainerListOptions(options)

View File

@ -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("format", "{{ .Names }} {{ .Image }} {{ .Labels }}"))
assert.Check(t, cmd.Flags().Set("quiet", "true")) assert.Check(t, cmd.Flags().Set("quiet", "true"))
assert.NilError(t, cmd.Execute()) 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") golden.Assert(t, cli.OutBuffer().String(), "container-list-quiet.golden")
}) })
} }