Skip to content

Commit 654ba31

Browse files
committed
WIP use structured type
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e305b4e commit 654ba31

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

cli/command/formatter/container.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package formatter
55

66
import (
7+
"cmp"
78
"fmt"
89
"net"
10+
"slices"
911
"sort"
1012
"strconv"
1113
"strings"
@@ -19,7 +21,8 @@ import (
1921
)
2022

2123
const (
22-
defaultContainerTableFormat = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}"
24+
// defaultContainerTableFormat = `table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}\t{{join .IPAddresses ", "}}`
25+
defaultContainerTableFormat = `table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}`
2326

2427
namesHeader = "NAMES"
2528
commandHeader = "COMMAND"
@@ -40,6 +43,16 @@ func (p Platform) String() string {
4043
return platforms.FormatAll(p.Platform)
4144
}
4245

46+
// NetworkIP describes an IP-address and the network it's associated with.
47+
type NetworkIP struct {
48+
Network string
49+
IP string
50+
}
51+
52+
func (p NetworkIP) String() string {
53+
return p.Network + "/" + p.IP
54+
}
55+
4356
// NewContainerFormat returns a Format for rendering using a Context
4457
func NewContainerFormat(source string, quiet bool, size bool) Format {
4558
switch source {
@@ -340,19 +353,30 @@ func (c *ContainerContext) Networks() string {
340353
// IPAddresses returns the list of IP-addresses assigned to the container
341354
// IP-addresses are prefixed with the name of the network, separated with a colon.
342355
// For example: "bridge:192.168.1.10"
343-
func (c *ContainerContext) IPAddresses() []string {
344-
ipAddresses := []string{}
345-
if c.c.NetworkSettings == nil {
346-
return ipAddresses
356+
func (c *ContainerContext) IPAddresses() []NetworkIP {
357+
if c.c.NetworkSettings == nil || len(c.c.NetworkSettings.Networks) == 0 {
358+
return []NetworkIP{}
347359
}
360+
ipAddresses := make([]NetworkIP, 0, len(c.c.NetworkSettings.Networks))
348361
for name, nw := range c.c.NetworkSettings.Networks {
349362
if nw.IPAddress != "" {
350-
ipAddresses = append(ipAddresses, name+":"+nw.IPAddress)
363+
ipAddresses = append(ipAddresses, NetworkIP{
364+
Network: name,
365+
IP: nw.IPAddress,
366+
})
351367
}
352368
if nw.GlobalIPv6Address != "" {
353-
ipAddresses = append(ipAddresses, name+":"+nw.GlobalIPv6Address)
369+
ipAddresses = append(ipAddresses, NetworkIP{
370+
Network: name,
371+
IP: nw.GlobalIPv6Address,
372+
})
354373
}
355374
}
375+
376+
slices.SortFunc(ipAddresses, func(a, b NetworkIP) int {
377+
return cmp.Compare(a.String(), b.String())
378+
})
379+
356380
return ipAddresses
357381
}
358382

cli/command/formatter/container_test.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,18 @@ func TestContainerContextWriteJSON(t *testing.T) {
439439
Image: "ubuntu",
440440
Created: unix,
441441
State: container.StateRunning,
442-
442+
NetworkSettings: &container.NetworkSettingsSummary{
443+
Networks: map[string]*network.EndpointSettings{
444+
"bridge": {
445+
IPAddress: "172.17.0.1",
446+
GlobalIPv6Address: "ff02::1",
447+
},
448+
"my-net": {
449+
IPAddress: "172.18.0.1",
450+
GlobalIPv6Address: "ff02::2",
451+
},
452+
},
453+
},
443454
ImageManifestDescriptor: &ocispec.Descriptor{Platform: &ocispec.Platform{Architecture: "amd64", OS: "linux"}},
444455
},
445456
{
@@ -458,6 +469,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
458469
"Command": `""`,
459470
"CreatedAt": expectedCreated,
460471
"ID": "containerID1",
472+
"IPAddresses": []any{},
461473
"Image": "ubuntu",
462474
"Labels": "",
463475
"LocalVolumes": "0",
@@ -472,15 +484,21 @@ func TestContainerContextWriteJSON(t *testing.T) {
472484
"Status": "",
473485
},
474486
{
475-
"Command": `""`,
476-
"CreatedAt": expectedCreated,
477-
"ID": "containerID2",
487+
"Command": `""`,
488+
"CreatedAt": expectedCreated,
489+
"ID": "containerID2",
490+
"IPAddresses": []any{
491+
map[string]any{"IP": "172.17.0.1", "Network": "bridge"},
492+
map[string]any{"IP": "ff02::1", "Network": "bridge"},
493+
map[string]any{"IP": "172.18.0.1", "Network": "my-net"},
494+
map[string]any{"IP": "ff02::2", "Network": "my-net"},
495+
},
478496
"Image": "ubuntu",
479497
"Labels": "",
480498
"LocalVolumes": "0",
481499
"Mounts": "",
482500
"Names": "foobar_bar",
483-
"Networks": "",
501+
"Networks": "bridge,my-net",
484502
"Platform": map[string]any{"architecture": "amd64", "os": "linux"},
485503
"Ports": "",
486504
"RunningFor": "About a minute ago",
@@ -492,6 +510,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
492510
"Command": `""`,
493511
"CreatedAt": expectedCreated,
494512
"ID": "containerID3",
513+
"IPAddresses": []any{},
495514
"Image": "ubuntu",
496515
"Labels": "",
497516
"LocalVolumes": "0",
@@ -564,8 +583,8 @@ func TestContainerContextIPAddresses(t *testing.T) {
564583
out := bytes.NewBufferString("")
565584
err := ContainerWrite(Context{Format: "{{.IPAddresses}}", Output: out}, containers)
566585
assert.NilError(t, err)
567-
assert.Equal(t, out.String(), `[one:192.168.1.2 two:192.168.178.2]
568-
[one:192.168.1.3 two:192.168.178.3]
586+
assert.Equal(t, out.String(), `[one/192.168.1.2 two/192.168.178.2]
587+
[one/192.168.1.3 two/192.168.178.3]
569588
`)
570589
}
571590

docs/reference/commandline/container_ls.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,6 @@ Show the IP-addresses that containers have:
455455
$ docker ps --format "table {{.ID}}\\t{{join .IPAddresses \", \"}}"
456456

457457
CONTAINER ID IP ADDRESSES
458-
c0cf2877da71 bridge:172.17.0.3
459-
17e7d1910fc0 bridge:172.17.0.2, mynetwork:172.19.0.2
458+
c0cf2877da71 bridge/172.17.0.3
459+
17e7d1910fc0 bridge/172.17.0.2, mynetwork/172.19.0.2
460460
```

0 commit comments

Comments
 (0)