Skip to content

Commit f5116c6

Browse files
authored
Merge pull request moby#30776 from WeiZhang555/stats-all-format-name-panic-cli
Fix panic of "docker stats --format {{.Name}} --all"
2 parents 846b787 + 5a7b3c7 commit f5116c6

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

cli/command/formatter/stats.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ func (c *containerStatsContext) Container() string {
149149

150150
func (c *containerStatsContext) Name() string {
151151
c.AddHeader(nameHeader)
152-
name := c.s.Name[1:]
153-
return name
152+
if len(c.s.Name) > 1 {
153+
return c.s.Name[1:]
154+
}
155+
return "--"
154156
}
155157

156158
func (c *containerStatsContext) ID() string {

cli/command/formatter/stats_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ func TestContainerStatsContextWrite(t *testing.T) {
7070
`MEM USAGE / LIMIT
7171
20B / 20B
7272
-- / --
73+
`,
74+
},
75+
{
76+
Context{Format: "{{.Container}} {{.ID}} {{.Name}}"},
77+
`container1 abcdef foo
78+
container2 --
7379
`,
7480
},
7581
{
@@ -84,6 +90,8 @@ container2 --
8490
stats := []StatsEntry{
8591
{
8692
Container: "container1",
93+
ID: "abcdef",
94+
Name: "/foo",
8795
CPUPercentage: 20,
8896
Memory: 20,
8997
MemoryLimit: 20,

0 commit comments

Comments
 (0)