Skip to content

Commit e301d95

Browse files
LK4D4thaJeztah
authored andcommitted
Merge pull request moby#30776 from WeiZhang555/stats-all-format-name-panic-cli
Fix panic of "docker stats --format {{.Name}} --all" (cherry picked from commit f5116c6) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 1c47c89 commit e301d95

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
@@ -145,8 +145,10 @@ func (c *containerStatsContext) Container() string {
145145

146146
func (c *containerStatsContext) Name() string {
147147
c.AddHeader(nameHeader)
148-
name := c.s.Name[1:]
149-
return name
148+
if len(c.s.Name) > 1 {
149+
return c.s.Name[1:]
150+
}
151+
return "--"
150152
}
151153

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

cli/command/formatter/stats_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func TestContainerStatsContextWrite(t *testing.T) {
6969
`MEM USAGE / LIMIT
7070
20 B / 20 B
7171
-- / --
72+
`,
73+
},
74+
{
75+
Context{Format: "{{.Container}} {{.ID}} {{.Name}}"},
76+
`container1 abcdef foo
77+
container2 --
7278
`,
7379
},
7480
{
@@ -83,6 +89,8 @@ container2 --
8389
stats := []StatsEntry{
8490
{
8591
Container: "container1",
92+
ID: "abcdef",
93+
Name: "/foo",
8694
CPUPercentage: 20,
8795
Memory: 20,
8896
MemoryLimit: 20,

0 commit comments

Comments
 (0)