Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cli/command/container/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package container
import (
"errors"
"io"
"strings"
"testing"

"github.com/docker/cli/cli/config/configfile"
Expand Down Expand Up @@ -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"))
}
10 changes: 10 additions & 0 deletions cli/command/formatter/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}}`
Expand Down
11 changes: 11 additions & 0 deletions cli/command/formatter/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
9 changes: 5 additions & 4 deletions cli/command/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 .}}"
Expand Down