cli icon indicating copy to clipboard operation
cli copied to clipboard

[23.0 backport] docker ps: always use --quiet, also combined with --format

Open thaJeztah opened this issue 3 years ago • 6 comments

  • backport of https://github.com/docker/cli/pull/3666

  • follow-up to https://github.com/docker/cli/pull/3640
  • see the discussion on https://github.com/photra/cli/pull/1#issuecomment-1148364089
  • fixes https://github.com/docker/cli/issues/4093
  • supersedes / closes https://github.com/docker/cli/pull/4040

Previously, the formatter would ignore the quiet option if a custom format was passed; this situation was handled in runPs(), where custom formats would only be applied if the quiet option was not set, but only if the format was set in the CLI's config.

This patch updates NewContainerFormat() to do the same, even if a --format was passed on the command-line.

This is a change in behavior, so may need some discussion; possible alternatives;

  • produce an error if both --format and --quiet are passed
  • print a warning if both are passed (but use the logic from this patch)

Before this patch:

docker ps --format '{{.Image}}'
ubuntu:22.04
alpine

docker ps --format '{{.Image}}' --quiet
ubuntu:22.04
alpine

mkdir -p ~/.docker/
echo '{"psFormat": "{{.Image}}" > ~/.docker/config.json

docker ps
ubuntu:22.04
alpine

docker ps --quiet
ubuntu:22.04
alpine

With this patch applied:

docker ps --format '{{.Image}}'
ubuntu:22.04
alpine

docker ps --format '{{.Image}}' --quiet
40111f61d5c5
482efdf39fac

mkdir -p ~/.docker/
echo '{"psFormat": "{{.Image}}" > ~/.docker/config.json

docker ps
ubuntu:22.04
alpine

docker ps --quiet
40111f61d5c5
482efdf39fac

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

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

thaJeztah avatar Apr 13 '23 11:04 thaJeztah

Codecov Report

Merging #4201 (de7e265) into 23.0 (0b3f540) will increase coverage by 0.00%. The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##             23.0    #4201   +/-   ##
=======================================
  Coverage   58.83%   58.84%           
=======================================
  Files         572      572           
  Lines       49610    49620   +10     
=======================================
+ Hits        29188    29198   +10     
  Misses      18648    18648           
  Partials     1774     1774           

codecov-commenter avatar Apr 13 '23 12:04 codecov-commenter

Discussing with @neersighted whether or not to backport this to v23.0;

👍 fixes docker ps --quiet with a custom format in ~/.docker/config.json 👎 it's a change in behaviour (notably for docker ps --format {''some format"}} --quiet)

/cc @tianon @vvoland @cpuguy83 @AkihiroSuda WDYT?

thaJeztah avatar Apr 13 '23 20:04 thaJeztah

No strong feelings - I don't think it needs a backport, but I think the new behavior is much more correct (and matches the user expectation better), so it's totally reasonable to backport if 23.x is going to continue to live for a while.

tianon avatar Apr 13 '23 22:04 tianon

FWIW; I discussed with @neersighted and he was okay with this change, but wanted to have a quick peek from others

thaJeztah avatar Apr 14 '23 08:04 thaJeztah

For v23, it might be safer to just retain the old behavior but print a warning message

AkihiroSuda avatar Apr 14 '23 08:04 AkihiroSuda

Yeah, I guess the "complicated" one is the case where both as passed through --flags. The case where a default is configured in the ~/.docker/config.json (and --quiet not working) is easier.

I could consider adding a commit that only changes the behaviour for ~/.docker/config.json for 23.0, and we can decide on the other ones later (or keep it)

thaJeztah avatar Apr 14 '23 09:04 thaJeztah