From f5229055954117c6d074da733d22350124318368 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 7 Jun 2022 10:14:31 +0200 Subject: [PATCH 1/2] docker ps: always use --quiet, also combined with --format 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: ```console 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: ```console 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 ``` Signed-off-by: Sebastiaan van Stijn --- cli/command/container/list_test.go | 21 +++++++++++++++---- .../testdata/container-list-quiet.golden | 2 ++ cli/command/formatter/container.go | 3 +++ cli/command/formatter/container_test.go | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 cli/command/container/testdata/container-list-quiet.golden diff --git a/cli/command/container/list_test.go b/cli/command/container/list_test.go index f88aeeb56..a071b2e30 100644 --- a/cli/command/container/list_test.go +++ b/cli/command/container/list_test.go @@ -309,8 +309,21 @@ func TestContainerListWithFormat(t *testing.T) { }, nil }, }) - cmd := newListCommand(cli) - cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}") - assert.NilError(t, cmd.Execute()) - golden.Assert(t, cli.OutBuffer().String(), "container-list-with-format.golden") + + t.Run("with format", func(t *testing.T) { + cli.OutBuffer().Reset() + cmd := newListCommand(cli) + assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}")) + assert.NilError(t, cmd.Execute()) + golden.Assert(t, cli.OutBuffer().String(), "container-list-with-format.golden") + }) + + t.Run("with format and quiet", func(t *testing.T) { + cli.OutBuffer().Reset() + cmd := newListCommand(cli) + assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}")) + assert.Check(t, cmd.Flags().Set("quiet", "true")) + assert.NilError(t, cmd.Execute()) + golden.Assert(t, cli.OutBuffer().String(), "container-list-quiet.golden") + }) } diff --git a/cli/command/container/testdata/container-list-quiet.golden b/cli/command/container/testdata/container-list-quiet.golden new file mode 100644 index 000000000..54e8203d2 --- /dev/null +++ b/cli/command/container/testdata/container-list-quiet.golden @@ -0,0 +1,2 @@ +container_id +container_id diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index f3306617f..60ad2fa40 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -55,6 +55,9 @@ ports: {{- pad .Ports 1 0}} } return Format(format) default: // custom format + if quiet { + return DefaultQuietFormat + } return Format(source) } } diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index c076d3e4b..1c09a5779 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -163,7 +163,7 @@ containerID2 ubuntu "" 24 hours ago foobar_bar }, { Context{Format: NewContainerFormat("table {{.Image}}", true, false)}, - "IMAGE\nubuntu\nubuntu\n", + "containerID1\ncontainerID2\n", }, { Context{Format: NewContainerFormat("table", true, false)}, From 37e02ff211aafd0d375d80a24cfccfc558298e81 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 10 Apr 2023 01:14:21 +0200 Subject: [PATCH 2/2] 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") }) }