Browse code

Merge pull request #30776 from WeiZhang555/stats-all-format-name-panic-cli

Fix panic of "docker stats --format {{.Name}} --all"

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