Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker ps: add State field to formatting #2000

Merged
merged 1 commit into from
Jul 15, 2019
Merged
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
6 changes: 6 additions & 0 deletions cli/command/formatter/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewContainerFormat(source string, quiet bool, size bool) Format {
image: {{.Image}}
command: {{.Command}}
created_at: {{.CreatedAt}}
state: {{- pad .State 1 0}}
status: {{- pad .Status 1 0}}
names: {{.Names}}
labels: {{- pad .Labels 1 0}}
Expand Down Expand Up @@ -87,6 +88,7 @@ func newContainerContext() *containerContext {
"CreatedAt": CreatedAtHeader,
"RunningFor": runningForHeader,
"Ports": PortsHeader,
"State": StateHeader,
"Status": StatusHeader,
"Size": SizeHeader,
"Labels": LabelsHeader,
Expand Down Expand Up @@ -169,6 +171,10 @@ func (c *containerContext) Ports() string {
return DisplayablePorts(c.c.Ports)
}

func (c *containerContext) State() string {
return c.c.State
}

func (c *containerContext) Status() string {
return c.c.Status
}
Expand Down
18 changes: 14 additions & 4 deletions cli/command/formatter/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,18 @@ containerID2 ubuntu "" 24 hours ago
Context{Format: NewContainerFormat("table", true, false)},
"containerID1\ncontainerID2\n",
},
{
Context{Format: NewContainerFormat("table {{.State}}", false, true)},
"STATE\nrunning\nrunning\n",
},
// Raw Format
{
Context{Format: NewContainerFormat("raw", false, false)},
fmt.Sprintf(`container_id: containerID1
image: ubuntu
command: ""
created_at: %s
state: running
status:
names: foobar_baz
labels:
Expand All @@ -184,6 +189,7 @@ container_id: containerID2
image: ubuntu
command: ""
created_at: %s
state: running
status:
names: foobar_bar
labels:
Expand All @@ -197,6 +203,7 @@ ports:
image: ubuntu
command: ""
created_at: %s
state: running
status:
names: foobar_baz
labels:
Expand All @@ -207,6 +214,7 @@ container_id: containerID2
image: ubuntu
command: ""
created_at: %s
state: running
status:
names: foobar_bar
labels:
Expand Down Expand Up @@ -237,8 +245,8 @@ size: 0B

for _, testcase := range cases {
containers := []types.Container{
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unixTime},
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unixTime},
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unixTime, State: "running"},
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unixTime, State: "running"},
}
out := bytes.NewBufferString("")
testcase.context.Output = out
Expand Down Expand Up @@ -314,8 +322,8 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) {
func TestContainerContextWriteJSON(t *testing.T) {
unix := time.Now().Add(-65 * time.Second).Unix()
containers := []types.Container{
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix},
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix},
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix, State: "running"},
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix, State: "running"},
}
expectedCreated := time.Unix(unix, 0).String()
expectedJSONs := []map[string]interface{}{
Expand All @@ -332,6 +340,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
"Ports": "",
"RunningFor": "About a minute ago",
"Size": "0B",
"State": "running",
"Status": "",
},
{
Expand All @@ -347,6 +356,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
"Ports": "",
"RunningFor": "About a minute ago",
"Size": "0B",
"State": "running",
"Status": "",
},
}
Expand Down
1 change: 1 addition & 0 deletions cli/command/formatter/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
DescriptionHeader = "DESCRIPTION"
DriverHeader = "DRIVER"
ScopeHeader = "SCOPE"
StateHeader = "STATE"
StatusHeader = "STATUS"
PortsHeader = "PORTS"
ImageHeader = "IMAGE"
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/commandline/ps.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ Valid placeholders for the Go template are listed below:
| `.CreatedAt` | Time when the container was created. |
| `.RunningFor` | Elapsed time since the container was started. |
| `.Ports` | Exposed ports. |
| `.Status` | Container status. |
| `.State` | Container status (for example; "created", "running", "exited"). |
| `.Status` | Container status with details about duration and health-status. |
| `.Size` | Container disk size. |
| `.Names` | Container names. |
| `.Labels` | All labels assigned to the container. |
Expand Down