Browse code

Merge pull request #27564 from mlaventure/add-networks-format-to-ps

Add Networks placeholder to ps --format

Victor Vieux authored on 2016/10/21 10:43:46
Showing 4 changed files
... ...
@@ -70,6 +70,13 @@ func (p *preProcessor) Size() bool {
70 70
 	return true
71 71
 }
72 72
 
73
+// Networks does nothing but return true.
74
+// It is needed to avoid the template check to fail as this field
75
+// doesn't exist in `types.Container`
76
+func (p *preProcessor) Networks() bool {
77
+	return true
78
+}
79
+
73 80
 func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) {
74 81
 	options := &types.ContainerListOptions{
75 82
 		All:    opts.all,
... ...
@@ -24,6 +24,7 @@ const (
24 24
 	portsHeader       = "PORTS"
25 25
 	mountsHeader      = "MOUNTS"
26 26
 	localVolumes      = "LOCAL VOLUMES"
27
+	networksHeader    = "NETWORKS"
27 28
 )
28 29
 
29 30
 // NewContainerFormat returns a Format for rendering using a Context
... ...
@@ -217,3 +218,18 @@ func (c *containerContext) LocalVolumes() string {
217 217
 
218 218
 	return fmt.Sprintf("%d", count)
219 219
 }
220
+
221
+func (c *containerContext) Networks() string {
222
+	c.AddHeader(networksHeader)
223
+
224
+	if c.c.NetworkSettings == nil {
225
+		return ""
226
+	}
227
+
228
+	networks := []string{}
229
+	for k := range c.c.NetworkSettings.Networks {
230
+		networks = append(networks, k)
231
+	}
232
+
233
+	return strings.Join(networks, ",")
234
+}
... ...
@@ -333,8 +333,8 @@ func TestContainerContextWriteJSON(t *testing.T) {
333 333
 	}
334 334
 	expectedCreated := time.Unix(unix, 0).String()
335 335
 	expectedJSONs := []map[string]interface{}{
336
-		{"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID1", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_baz", "Ports": "", "RunningFor": "About a minute", "Size": "0 B", "Status": ""},
337
-		{"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID2", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_bar", "Ports": "", "RunningFor": "About a minute", "Size": "0 B", "Status": ""},
336
+		{"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID1", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_baz", "Networks": "", "Ports": "", "RunningFor": "About a minute", "Size": "0 B", "Status": ""},
337
+		{"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID2", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_bar", "Networks": "", "Ports": "", "RunningFor": "About a minute", "Size": "0 B", "Status": ""},
338 338
 	}
339 339
 	out := bytes.NewBufferString("")
340 340
 	err := ContainerWrite(Context{Format: "{{json .}}", Output: out}, containers)
... ...
@@ -347,6 +347,7 @@ Placeholder   | Description
347 347
 `.Labels`     | All labels assigned to the container.
348 348
 `.Label`      | Value of a specific label for this container. For example `'{% raw %}{{.Label "com.docker.swarm.cpu"}}{% endraw %}'`
349 349
 `.Mounts`     | Names of the volumes mounted in this container.
350
+`.Networks`   | Names of the networks attached to this container.
350 351
 
351 352
 When using the `--format` option, the `ps` command will either output the data
352 353
 exactly as the template declares or, when using the `table` directive, includes