Skip to content

Commit f522905

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

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

cli/command/container/list_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,21 @@ func TestContainerListWithFormat(t *testing.T) {
309309
}, nil
310310
},
311311
})
312-
cmd := newListCommand(cli)
313-
cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}")
314-
assert.NilError(t, cmd.Execute())
315-
golden.Assert(t, cli.OutBuffer().String(), "container-list-with-format.golden")
312+
313+
t.Run("with format", func(t *testing.T) {
314+
cli.OutBuffer().Reset()
315+
cmd := newListCommand(cli)
316+
assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}"))
317+
assert.NilError(t, cmd.Execute())
318+
golden.Assert(t, cli.OutBuffer().String(), "container-list-with-format.golden")
319+
})
320+
321+
t.Run("with format and quiet", func(t *testing.T) {
322+
cli.OutBuffer().Reset()
323+
cmd := newListCommand(cli)
324+
assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}"))
325+
assert.Check(t, cmd.Flags().Set("quiet", "true"))
326+
assert.NilError(t, cmd.Execute())
327+
golden.Assert(t, cli.OutBuffer().String(), "container-list-quiet.golden")
328+
})
316329
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
container_id
2+
container_id

cli/command/formatter/container.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ ports: {{- pad .Ports 1 0}}
5555
}
5656
return Format(format)
5757
default: // custom format
58+
if quiet {
59+
return DefaultQuietFormat
60+
}
5861
return Format(source)
5962
}
6063
}

cli/command/formatter/container_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ containerID2 ubuntu "" 24 hours ago foobar_bar
163163
},
164164
{
165165
Context{Format: NewContainerFormat("table {{.Image}}", true, false)},
166-
"IMAGE\nubuntu\nubuntu\n",
166+
"containerID1\ncontainerID2\n",
167167
},
168168
{
169169
Context{Format: NewContainerFormat("table", true, false)},

0 commit comments

Comments
 (0)