From b194252d80344d245e1912bbb478b4c542805a21 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Thu, 19 Jan 2017 10:17:56 -0800 Subject: [PATCH] Fix failure in `docker ps --format` when `.Label` has args This fix tries to fix the issue in 30279 where `docker ps --format` fails if `.Label` has args. For example: ``` docker ps --format '{{.ID}}\t{{.Names}}\t{{.Label "some.label"}}' ``` The reason for the failure is that during the preprocessing phase to detect the existance of `.Size`, the `listOptionsProcessor` does not has a method of `Label(name string) string`. This results in the failure of ``` template: :1:24: executing "" at <.Label>: Label is not a method but has arguments ``` This fix fixes the issue by adding needed method of `Label(name string) string`. This fix fixes 30279. Signed-off-by: Yong Tang (cherry picked from commit 2cd4ba1e56222a938b2edaa7ad7981c9a2bc248f) Signed-off-by: Victor Vieux --- cli/command/container/list.go | 6 ++++++ integration-cli/docker_cli_ps_test.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/cli/command/container/list.go b/cli/command/container/list.go index 60c2462986..5bbf41966d 100644 --- a/cli/command/container/list.go +++ b/cli/command/container/list.go @@ -74,6 +74,12 @@ func (o listOptionsProcessor) Size() bool { return true } +// Label is needed here as it allows the correct pre-processing +// because Label() is a method with arguments +func (o listOptionsProcessor) Label(name string) string { + return "" +} + func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) { options := &types.ContainerListOptions{ All: opts.all, diff --git a/integration-cli/docker_cli_ps_test.go b/integration-cli/docker_cli_ps_test.go index f3bdb39d00..19ede90d5a 100644 --- a/integration-cli/docker_cli_ps_test.go +++ b/integration-cli/docker_cli_ps_test.go @@ -943,3 +943,10 @@ func (s *DockerSuite) TestPsFilterMissingArgErrorCode(c *check.C) { _, errCode, _ := dockerCmdWithError("ps", "--filter") c.Assert(errCode, checker.Equals, 125) } + +// Test case for 30291 +func (s *DockerSuite) TestPsFormatTemplateWithArg(c *check.C) { + runSleepingContainer(c, "-d", "--name", "top", "--label", "some.label=label.foo-bar") + out, _ := dockerCmd(c, "ps", "--format", `{{.Names}} {{.Label "some.label"}}`) + c.Assert(strings.TrimSpace(out), checker.Equals, "top label.foo-bar") +}