Skip to content

Commit 63e3c5a

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

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

cli/command/formatter/container.go

Lines changed: 29 additions & 6 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"
@@ -40,6 +42,16 @@ func (p Platform) String() string {
4042
return platforms.FormatAll(p.Platform)
4143
}
4244

45+
// NetworkIP describes an IP-address and the network it's associated with.
46+
type NetworkIP struct {
47+
Network string
48+
IP string
49+
}
50+
51+
func (p NetworkIP) String() string {
52+
return p.Network + "/" + p.IP
53+
}
54+
4355
// NewContainerFormat returns a Format for rendering using a Context
4456
func NewContainerFormat(source string, quiet bool, size bool) Format {
4557
switch source {
@@ -340,19 +352,30 @@ func (c *ContainerContext) Networks() string {
340352
// IPAddresses returns the list of IP-addresses assigned to the container
341353
// IP-addresses are prefixed with the name of the network, separated with a colon.
342354
// 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
355+
func (c *ContainerContext) IPAddresses() []NetworkIP {
356+
if c.c.NetworkSettings == nil || len(c.c.NetworkSettings.Networks) == 0 {
357+
return []NetworkIP{}
347358
}
359+
ipAddresses := make([]NetworkIP, 0, len(c.c.NetworkSettings.Networks))
348360
for name, nw := range c.c.NetworkSettings.Networks {
349361
if nw.IPAddress != "" {
350-
ipAddresses = append(ipAddresses, name+":"+nw.IPAddress)
362+
ipAddresses = append(ipAddresses, NetworkIP{
363+
Network: name,
364+
IP: nw.IPAddress,
365+
})
351366
}
352367
if nw.GlobalIPv6Address != "" {
353-
ipAddresses = append(ipAddresses, name+":"+nw.GlobalIPv6Address)
368+
ipAddresses = append(ipAddresses, NetworkIP{
369+
Network: name,
370+
IP: nw.GlobalIPv6Address,
371+
})
354372
}
355373
}
374+
375+
slices.SortFunc(ipAddresses, func(a, b NetworkIP) int {
376+
return cmp.Compare(a.String(), b.String())
377+
})
378+
356379
return ipAddresses
357380
}
358381

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)