Use net.JoinHostPort() to fix formatting with IPv6 addresses
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@ -3,6 +3,7 @@ package container
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/cli/cli"
|
||||
@ -61,7 +62,7 @@ func runPort(dockerCli command.Cli, opts *portOptions) error {
|
||||
}
|
||||
if frontends, exists := c.NetworkSettings.Ports[newP]; exists && frontends != nil {
|
||||
for _, frontend := range frontends {
|
||||
fmt.Fprintf(dockerCli.Out(), "%s:%s\n", frontend.HostIP, frontend.HostPort)
|
||||
fmt.Fprintln(dockerCli.Out(), net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -70,7 +71,7 @@ func runPort(dockerCli command.Cli, opts *portOptions) error {
|
||||
|
||||
for from, frontends := range c.NetworkSettings.Ports {
|
||||
for _, frontend := range frontends {
|
||||
fmt.Fprintf(dockerCli.Out(), "%s -> %s:%s\n", from, frontend.HostIP, frontend.HostPort)
|
||||
fmt.Fprintf(dockerCli.Out(), "%s -> %s\n", from, net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
57
cli/command/container/port_test.go
Normal file
57
cli/command/container/port_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/cli/internal/test"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"gotest.tools/v3/assert"
|
||||
"gotest.tools/v3/golden"
|
||||
)
|
||||
|
||||
func TestNewPortCommandOutput(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
ips []string
|
||||
}{
|
||||
{
|
||||
name: "container-port-ipv4",
|
||||
ips: []string{"0.0.0.0"},
|
||||
},
|
||||
{
|
||||
name: "container-port-ipv6",
|
||||
ips: []string{"::"},
|
||||
},
|
||||
{
|
||||
name: "container-port-ipv6-and-ipv4",
|
||||
ips: []string{"::", "0.0.0.0"},
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
inspectFunc: func(string) (types.ContainerJSON, error) {
|
||||
ci := types.ContainerJSON{NetworkSettings: &types.NetworkSettings{}}
|
||||
ci.NetworkSettings.Ports = nat.PortMap{
|
||||
"80/tcp": make([]nat.PortBinding, len(tc.ips)),
|
||||
}
|
||||
for i, ip := range tc.ips {
|
||||
ci.NetworkSettings.Ports["80/tcp"][i] = nat.PortBinding{
|
||||
HostIP: ip, HostPort: "3456",
|
||||
}
|
||||
}
|
||||
return ci, nil
|
||||
},
|
||||
}, test.EnableContentTrust)
|
||||
cmd := NewPortCommand(cli)
|
||||
cmd.SetErr(ioutil.Discard)
|
||||
cmd.SetArgs([]string{"some_container", "80"})
|
||||
err := cmd.Execute()
|
||||
assert.NilError(t, err)
|
||||
golden.Assert(t, cli.OutBuffer().String(), tc.name+".golden")
|
||||
})
|
||||
}
|
||||
}
|
||||
1
cli/command/container/testdata/container-port-ipv4.golden
vendored
Normal file
1
cli/command/container/testdata/container-port-ipv4.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
0.0.0.0:3456
|
||||
2
cli/command/container/testdata/container-port-ipv6-and-ipv4.golden
vendored
Normal file
2
cli/command/container/testdata/container-port-ipv6-and-ipv4.golden
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
[::]:3456
|
||||
0.0.0.0:3456
|
||||
1
cli/command/container/testdata/container-port-ipv6.golden
vendored
Normal file
1
cli/command/container/testdata/container-port-ipv6.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
[::]:3456
|
||||
Reference in New Issue
Block a user