Browse code

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 <github@gone.nl>

Alexander Morozov authored on 2017/02/09 04:40:06
Showing 2 changed files
... ...
@@ -145,8 +145,10 @@ func (c *containerStatsContext) Container() string {
145 145
 
146 146
 func (c *containerStatsContext) Name() string {
147 147
 	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 "--"
150 152
 }
151 153
 
152 154
 func (c *containerStatsContext) ID() string {
... ...
@@ -72,6 +72,12 @@ func TestContainerStatsContextWrite(t *testing.T) {
72 72
 `,
73 73
 		},
74 74
 		{
75
+			Context{Format: "{{.Container}}  {{.ID}}  {{.Name}}"},
76
+			`container1  abcdef  foo
77
+container2    --
78
+`,
79
+		},
80
+		{
75 81
 			Context{Format: "{{.Container}}  {{.CPUPerc}}"},
76 82
 			`container1  20.00%
77 83
 container2  --
... ...
@@ -83,6 +89,8 @@ container2  --
83 83
 		stats := []StatsEntry{
84 84
 			{
85 85
 				Container:        "container1",
86
+				ID:               "abcdef",
87
+				Name:             "/foo",
86 88
 				CPUPercentage:    20,
87 89
 				Memory:           20,
88 90
 				MemoryLimit:      20,