diff --git a/cli/command/container/list_test.go b/cli/command/container/list_test.go index 2f49e9721e68..d967557f8bf2 100644 --- a/cli/command/container/list_test.go +++ b/cli/command/container/list_test.go @@ -3,6 +3,7 @@ package container import ( "errors" "io" + "strings" "testing" "github.com/docker/cli/cli/config/configfile" @@ -344,3 +345,28 @@ func TestContainerListWithFormat(t *testing.T) { golden.Assert(t, cli.OutBuffer().String(), "container-list-quiet.golden") }) } + +func TestContainerListWithCompactFormat(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{ + containerListFunc: func(_ client.ContainerListOptions) (client.ContainerListResult, error) { + return client.ContainerListResult{ + Items: []container.Summary{ + *builders.Container("c1"), + }, + }, nil + }, + }) + cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + assert.Check(t, cmd.Flags().Set("format", "compact")) + assert.NilError(t, cmd.Execute()) + + output := cli.OutBuffer().String() + assert.Assert(t, strings.Contains(output, "CONTAINER ID")) + assert.Assert(t, strings.Contains(output, "NAMES")) + assert.Assert(t, strings.Contains(output, "IMAGE")) + assert.Assert(t, strings.Contains(output, "STATE")) + assert.Assert(t, strings.Contains(output, "CREATED")) +} diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index 7b77a8a75375..76402eec66ce 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -21,6 +21,7 @@ import ( const ( defaultContainerTableFormat = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}" + compactContainerTableFormat = "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.State}}\t{{.RunningFor}}" namesHeader = "NAMES" commandHeader = "COMMAND" @@ -53,6 +54,15 @@ func NewContainerFormat(source string, quiet bool, size bool) Format { format += `\t{{.Size}}` } return Format(format) + case CompactFormatKey: + if quiet { + return DefaultQuietFormat + } + format := compactContainerTableFormat + if size { + format += `\t{{.Size}}` + } + return Format(format) case RawFormatKey: if quiet { return `container_id: {{.ID}}` diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index 07df93d9aeed..1dce6ef28c57 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -309,6 +309,17 @@ containerID2 ubuntu "" 24 hours ago foobar_bar context: Context{Format: NewContainerFormat("table {{.State}}", false, true)}, expected: "STATE\nrunning\nrunning\n", }, + { + context: Context{Format: NewContainerFormat(CompactFormatKey, false, false)}, + expected: `CONTAINER ID NAMES IMAGE STATE CREATED +containerID1 foobar_baz ubuntu running 24 hours ago +containerID2 foobar_bar ubuntu running 24 hours ago +`, + }, + { + context: Context{Format: NewContainerFormat(CompactFormatKey, true, false)}, + expected: "containerID1\ncontainerID2\n", + }, // Raw Format { context: Context{Format: NewContainerFormat("raw", false, false)}, diff --git a/cli/command/formatter/formatter.go b/cli/command/formatter/formatter.go index 4beb7f56b256..272b23500fb4 100644 --- a/cli/command/formatter/formatter.go +++ b/cli/command/formatter/formatter.go @@ -16,10 +16,11 @@ import ( // Format keys used to specify certain kinds of output formats const ( - TableFormatKey = "table" - RawFormatKey = "raw" - PrettyFormatKey = "pretty" - JSONFormatKey = "json" + TableFormatKey = "table" + CompactFormatKey = "compact" + RawFormatKey = "raw" + PrettyFormatKey = "pretty" + JSONFormatKey = "json" DefaultQuietFormat = "{{.ID}}" JSONFormat = "{{json .}}"