From e301d956b7df5eabcec9ea021cb5ebf3fb32184c Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Wed, 8 Feb 2017 11:40:06 -0800 Subject: [PATCH] Merge pull request #30776 from WeiZhang555/stats-all-format-name-panic-cli Fix panic of "docker stats --format {{.Name}} --all" (cherry picked from commit f5116c680207a12ecd9c4fa654a1a5707e7ac89c) Signed-off-by: Sebastiaan van Stijn --- cli/command/formatter/stats.go | 6 ++++-- cli/command/formatter/stats_test.go | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cli/command/formatter/stats.go b/cli/command/formatter/stats.go index 7997f996d8..30c9e30bcf 100644 --- a/cli/command/formatter/stats.go +++ b/cli/command/formatter/stats.go @@ -145,8 +145,10 @@ func (c *containerStatsContext) Container() string { func (c *containerStatsContext) Name() string { c.AddHeader(nameHeader) - name := c.s.Name[1:] - return name + if len(c.s.Name) > 1 { + return c.s.Name[1:] + } + return "--" } func (c *containerStatsContext) ID() string { diff --git a/cli/command/formatter/stats_test.go b/cli/command/formatter/stats_test.go index d5a17cc70e..f5c6cae0c3 100644 --- a/cli/command/formatter/stats_test.go +++ b/cli/command/formatter/stats_test.go @@ -69,6 +69,12 @@ func TestContainerStatsContextWrite(t *testing.T) { `MEM USAGE / LIMIT 20 B / 20 B -- / -- +`, + }, + { + Context{Format: "{{.Container}} {{.ID}} {{.Name}}"}, + `container1 abcdef foo +container2 -- `, }, { @@ -83,6 +89,8 @@ container2 -- stats := []StatsEntry{ { Container: "container1", + ID: "abcdef", + Name: "/foo", CPUPercentage: 20, Memory: 20, MemoryLimit: 20,