Browse code

Add `.CreatedAt` placeholder for `docker network ls --format`

This fix tries to add a placeholder `.CreatedAt` for Go
format template in `docker network ls --format`.

While working on 29226, I noticed that it is not possible to
display network's creation time in `docker network ls`, with or
without `--format`.

We are able to find the timestamp through `docker network inspect` though.

However, as we allows networks to be pruned based on the timestamp
(see 29226), showing the timestamp in `docker network ls --format`
would be much useful now.

This fix adds the `.CreatedAt` placeholder for `docker network ls --format`.
The default output was not changed for `docker network ls --format`.

A test case for unit tests has been added.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2017/01/05 08:17:54
Showing 3 changed files
... ...
@@ -115,3 +115,8 @@ func (c *networkContext) Label(name string) string {
115 115
 	}
116 116
 	return c.n.Labels[name]
117 117
 }
118
+
119
+func (c *networkContext) CreatedAt() string {
120
+	c.AddHeader(createdAtHeader)
121
+	return c.n.Created.String()
122
+}
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"encoding/json"
6 6
 	"strings"
7 7
 	"testing"
8
+	"time"
8 9
 
9 10
 	"github.com/docker/docker/api/types"
10 11
 	"github.com/docker/docker/pkg/stringid"
... ...
@@ -144,12 +145,22 @@ network_id: networkID2
144 144
 foobar_bar
145 145
 `,
146 146
 		},
147
+		// Custom Format with CreatedAt
148
+		{
149
+			Context{Format: NewNetworkFormat("{{.Name}} {{.CreatedAt}}", false)},
150
+			`foobar_baz 2016-01-01 00:00:00 +0000 UTC
151
+foobar_bar 2017-01-01 00:00:00 +0000 UTC
152
+`,
153
+		},
147 154
 	}
148 155
 
156
+	timestamp1, _ := time.Parse("2006-01-02", "2016-01-01")
157
+	timestamp2, _ := time.Parse("2006-01-02", "2017-01-01")
158
+
149 159
 	for _, testcase := range cases {
150 160
 		networks := []types.NetworkResource{
151
-			{ID: "networkID1", Name: "foobar_baz", Driver: "foo", Scope: "local"},
152
-			{ID: "networkID2", Name: "foobar_bar", Driver: "bar", Scope: "local"},
161
+			{ID: "networkID1", Name: "foobar_baz", Driver: "foo", Scope: "local", Created: timestamp1},
162
+			{ID: "networkID2", Name: "foobar_bar", Driver: "bar", Scope: "local", Created: timestamp2},
153 163
 		}
154 164
 		out := bytes.NewBufferString("")
155 165
 		testcase.context.Output = out
... ...
@@ -168,8 +179,8 @@ func TestNetworkContextWriteJSON(t *testing.T) {
168 168
 		{ID: "networkID2", Name: "foobar_bar"},
169 169
 	}
170 170
 	expectedJSONs := []map[string]interface{}{
171
-		{"Driver": "", "ID": "networkID1", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_baz", "Scope": ""},
172
-		{"Driver": "", "ID": "networkID2", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_bar", "Scope": ""},
171
+		{"Driver": "", "ID": "networkID1", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_baz", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"},
172
+		{"Driver": "", "ID": "networkID2", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_bar", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"},
173 173
 	}
174 174
 
175 175
 	out := bytes.NewBufferString("")
... ...
@@ -182,16 +182,17 @@ using a Go template.
182 182
 
183 183
 Valid placeholders for the Go template are listed below:
184 184
 
185
-Placeholder | Description
186
-`.ID`       | Network ID
187
-`.Name`     | Network name
188
-`.Driver`   | Network driver
189
-`.Scope`    | Network scope (local, global)
190
-`.IPv6`     | Whether IPv6 is enabled on the network or not.
191
-`.Internal` | Whether the network is internal or not.
192
-`.Labels`   | All labels assigned to the network.
193
-`.Label`    | Value of a specific label for this network. For example `{{.Label "project.version"}}`
185
+Placeholder  | Description
186
+-------------|------------------------------------------------------------------------------------------
187
+`.ID`        | Network ID
188
+`.Name`      | Network name
189
+`.Driver`    | Network driver
190
+`.Scope`     | Network scope (local, global)
191
+`.IPv6`      | Whether IPv6 is enabled on the network or not.
192
+`.Internal`  | Whether the network is internal or not.
193
+`.Labels`    | All labels assigned to the network.
194
+`.Label`     | Value of a specific label for this network. For example `{{.Label "project.version"}}`
195
+`.CreatedAt` | Time when the network was created
194 196
 
195 197
 When using the `--format` option, the `network ls` command will either
196 198
 output the data exactly as the template declares or, when using the