mirror of https://github.com/docker/cli.git
cli/command/container: runPort: sort ports before printing
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
c5613ac032
commit
1768240bcd
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -11,6 +12,7 @@ import (
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/cli/cli/command/completion"
|
"github.com/docker/cli/cli/command/completion"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
|
"github.com/fvbommel/sortorder"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -58,6 +60,7 @@ func runPort(dockerCli command.Cli, opts *portOptions) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var out []string
|
||||||
if opts.port != "" {
|
if opts.port != "" {
|
||||||
port, proto, _ := strings.Cut(opts.port, "/")
|
port, proto, _ := strings.Cut(opts.port, "/")
|
||||||
if proto == "" {
|
if proto == "" {
|
||||||
|
@ -71,15 +74,21 @@ func runPort(dockerCli command.Cli, opts *portOptions) error {
|
||||||
return errors.Errorf("Error: No public port '%s' published for %s", opts.port, opts.container)
|
return errors.Errorf("Error: No public port '%s' published for %s", opts.port, opts.container)
|
||||||
}
|
}
|
||||||
for _, frontend := range frontends {
|
for _, frontend := range frontends {
|
||||||
_, _ = fmt.Fprintln(dockerCli.Out(), net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
out = append(out, net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for from, frontends := range c.NetworkSettings.Ports {
|
||||||
|
for _, frontend := range frontends {
|
||||||
|
out = append(out, fmt.Sprintf("%s -> %s", from, net.JoinHostPort(frontend.HostIP, frontend.HostPort)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for from, frontends := range c.NetworkSettings.Ports {
|
if len(out) > 0 {
|
||||||
for _, frontend := range frontends {
|
sort.Slice(out, func(i, j int) bool {
|
||||||
_, _ = fmt.Fprintf(dockerCli.Out(), "%s -> %s\n", from, net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
return sortorder.NaturalLess(out[i], out[j])
|
||||||
}
|
})
|
||||||
|
_, _ = fmt.Fprintln(dockerCli.Out(), strings.Join(out, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -37,6 +37,10 @@ func TestNewPortCommandOutput(t *testing.T) {
|
||||||
ips: []string{"::", "0.0.0.0"},
|
ips: []string{"::", "0.0.0.0"},
|
||||||
port: "443/udp",
|
port: "443/udp",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "container-port-all-ports",
|
||||||
|
ips: []string{"::", "0.0.0.0"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
tc := tc
|
tc := tc
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
80/tcp -> 0.0.0.0:3456
|
||||||
|
80/tcp -> [::]:3456
|
||||||
|
443/tcp -> 0.0.0.0:4567
|
||||||
|
443/tcp -> [::]:4567
|
||||||
|
443/udp -> 0.0.0.0:5678
|
||||||
|
443/udp -> [::]:5678
|
|
@ -1,2 +1,2 @@
|
||||||
[::]:5678
|
|
||||||
0.0.0.0:5678
|
0.0.0.0:5678
|
||||||
|
[::]:5678
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
[::]:3456
|
|
||||||
0.0.0.0:3456
|
0.0.0.0:3456
|
||||||
|
[::]:3456
|
||||||
|
|
Loading…
Reference in New Issue