diff --git a/cli/command/container/list.go b/cli/command/container/list.go index f76a9fa947..80ca727974 100644 --- a/cli/command/container/list.go +++ b/cli/command/container/list.go @@ -77,8 +77,7 @@ func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, er options.Limit = 1 } - options.Size = opts.size - if !options.Size && len(opts.format) > 0 { + if !opts.quiet && !options.Size && len(opts.format) > 0 { // The --size option isn't set, but .Size may be used in the template. // Parse and execute the given template to detect if the .Size field is // used. If it is, then automatically enable the --size option. See #24696 @@ -109,6 +108,11 @@ func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, er func runPs(dockerCli command.Cli, options *psOptions) error { ctx := context.Background() + if len(options.format) == 0 { + // load custom psFormat from CLI config (if any) + options.format = dockerCli.ConfigFile().PsFormat + } + listOptions, err := buildContainerListOptions(options) if err != nil { return err @@ -119,18 +123,9 @@ func runPs(dockerCli command.Cli, options *psOptions) error { return err } - format := options.format - if len(format) == 0 { - if len(dockerCli.ConfigFile().PsFormat) > 0 && !options.quiet { - format = dockerCli.ConfigFile().PsFormat - } else { - format = formatter.TableFormatKey - } - } - containerCtx := formatter.Context{ Output: dockerCli.Out(), - Format: formatter.NewContainerFormat(format, options.quiet, listOptions.Size), + Format: formatter.NewContainerFormat(options.format, options.quiet, listOptions.Size), Trunc: !options.noTrunc, } return formatter.ContainerWrite(containerCtx, containers) diff --git a/cli/command/container/list_test.go b/cli/command/container/list_test.go index 3c5720d60e..d436e39a99 100644 --- a/cli/command/container/list_test.go +++ b/cli/command/container/list_test.go @@ -246,13 +246,13 @@ func TestContainerListWithConfigFormat(t *testing.T) { cli := test.NewFakeCli(&fakeClient{ containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) { return []types.Container{ - *Container("c1", WithLabel("some.label", "value")), - *Container("c2", WithName("foo/bar"), WithLabel("foo", "bar")), + *Container("c1", WithLabel("some.label", "value"), WithSize(10700000)), + *Container("c2", WithName("foo/bar"), WithLabel("foo", "bar"), WithSize(3200000)), }, nil }, }) cli.SetConfigFile(&configfile.ConfigFile{ - PsFormat: "{{ .Names }} {{ .Image }} {{ .Labels }}", + PsFormat: "{{ .Names }} {{ .Image }} {{ .Labels }} {{ .Size}}", }) cmd := newListCommand(cli) assert.NilError(t, cmd.Execute()) diff --git a/cli/command/container/testdata/container-list-with-config-format.golden b/cli/command/container/testdata/container-list-with-config-format.golden index 6333bf57ee..1f6954db04 100644 --- a/cli/command/container/testdata/container-list-with-config-format.golden +++ b/cli/command/container/testdata/container-list-with-config-format.golden @@ -1,2 +1,2 @@ -c1 busybox:latest some.label=value -c2 busybox:latest foo=bar +c1 busybox:latest some.label=value 10.7MB +c2 busybox:latest foo=bar 3.2MB diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index b22b36828d..f3306617f0 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -27,7 +27,7 @@ const ( // NewContainerFormat returns a Format for rendering using a Context func NewContainerFormat(source string, quiet bool, size bool) Format { switch source { - case TableFormatKey: + case TableFormatKey, "": // table formatting is the default if none is set. if quiet { return DefaultQuietFormat } @@ -54,8 +54,9 @@ ports: {{- pad .Ports 1 0}} format += `size: {{.Size}}\n` } return Format(format) + default: // custom format + return Format(source) } - return Format(source) } // ContainerWrite renders the context for a list of containers diff --git a/internal/test/builders/container.go b/internal/test/builders/container.go index 7961f376e8..0f31998a06 100644 --- a/internal/test/builders/container.go +++ b/internal/test/builders/container.go @@ -61,6 +61,15 @@ func WithPort(privateport, publicport uint16, builders ...func(*types.Port)) fun } } +// WithSize adds size in bytes to the container +func WithSize(size int64) func(*types.Container) { + return func(c *types.Container) { + if size >= 0 { + c.SizeRw = size + } + } +} + // IP sets the ip of the port func IP(ip string) func(*types.Port) { return func(p *types.Port) {