From 08ac5a303969838178d344a457b0de60475cb73c Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Wed, 19 Oct 2016 15:09:42 -0700 Subject: [PATCH] Add Networks placeholder to ps --format Passing {{.Networks}} to the format parameter will prompt ps to display all the networks the container is connected to. Signed-off-by: Kenfe-Mickael Laventure --- command/container/list.go | 7 +++++++ command/formatter/container.go | 16 ++++++++++++++++ command/formatter/container_test.go | 4 ++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/command/container/list.go b/command/container/list.go index 7f10ce8bd1..2d46b6604e 100644 --- a/command/container/list.go +++ b/command/container/list.go @@ -70,6 +70,13 @@ func (p *preProcessor) Size() bool { return true } +// Networks does nothing but return true. +// It is needed to avoid the template check to fail as this field +// doesn't exist in `types.Container` +func (p *preProcessor) Networks() bool { + return true +} + func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) { options := &types.ContainerListOptions{ All: opts.all, diff --git a/command/formatter/container.go b/command/formatter/container.go index 094bc85447..6273453355 100644 --- a/command/formatter/container.go +++ b/command/formatter/container.go @@ -24,6 +24,7 @@ const ( portsHeader = "PORTS" mountsHeader = "MOUNTS" localVolumes = "LOCAL VOLUMES" + networksHeader = "NETWORKS" ) // NewContainerFormat returns a Format for rendering using a Context @@ -217,3 +218,18 @@ func (c *containerContext) LocalVolumes() string { return fmt.Sprintf("%d", count) } + +func (c *containerContext) Networks() string { + c.AddHeader(networksHeader) + + if c.c.NetworkSettings == nil { + return "" + } + + networks := []string{} + for k := range c.c.NetworkSettings.Networks { + networks = append(networks, k) + } + + return strings.Join(networks, ",") +} diff --git a/command/formatter/container_test.go b/command/formatter/container_test.go index 4b520f94ba..0a844efb65 100644 --- a/command/formatter/container_test.go +++ b/command/formatter/container_test.go @@ -333,8 +333,8 @@ func TestContainerContextWriteJSON(t *testing.T) { } expectedCreated := time.Unix(unix, 0).String() expectedJSONs := []map[string]interface{}{ - {"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID1", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_baz", "Ports": "", "RunningFor": "About a minute", "Size": "0 B", "Status": ""}, - {"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID2", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_bar", "Ports": "", "RunningFor": "About a minute", "Size": "0 B", "Status": ""}, + {"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID1", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_baz", "Networks": "", "Ports": "", "RunningFor": "About a minute", "Size": "0 B", "Status": ""}, + {"Command": "\"\"", "CreatedAt": expectedCreated, "ID": "containerID2", "Image": "ubuntu", "Labels": "", "LocalVolumes": "0", "Mounts": "", "Names": "foobar_bar", "Networks": "", "Ports": "", "RunningFor": "About a minute", "Size": "0 B", "Status": ""}, } out := bytes.NewBufferString("") err := ContainerWrite(Context{Format: "{{json .}}", Output: out}, containers)