mirror of https://github.com/docker/cli.git
cli/command/container: runPort(): slight refactor
- use strings.Cut - don't use nat.NewPort as we don't accept port ranges - use an early return if there's no results Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
f0435fd3f3
commit
58487e088a
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/cli/cli"
|
"github.com/docker/cli/cli"
|
||||||
|
@ -58,31 +59,26 @@ func runPort(dockerCli command.Cli, opts *portOptions) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.port != "" {
|
if opts.port != "" {
|
||||||
port := opts.port
|
port, proto, _ := strings.Cut(opts.port, "/")
|
||||||
proto := "tcp"
|
if proto == "" {
|
||||||
parts := strings.SplitN(port, "/", 2)
|
proto = "tcp"
|
||||||
|
|
||||||
if len(parts) == 2 && len(parts[1]) != 0 {
|
|
||||||
port = parts[0]
|
|
||||||
proto = parts[1]
|
|
||||||
}
|
}
|
||||||
natPort := port + "/" + proto
|
if _, err = strconv.ParseUint(port, 10, 16); err != nil {
|
||||||
newP, err := nat.NewPort(proto, port)
|
return errors.Wrapf(err, "Error: invalid port (%s)", port)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if frontends, exists := c.NetworkSettings.Ports[newP]; exists && frontends != nil {
|
frontends, exists := c.NetworkSettings.Ports[nat.Port(port+"/"+proto)]
|
||||||
for _, frontend := range frontends {
|
if !exists || frontends == nil {
|
||||||
fmt.Fprintln(dockerCli.Out(), net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
return errors.Errorf("Error: No public port '%s' published for %s", opts.port, opts.container)
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
return errors.Errorf("Error: No public port '%s' published for %s", natPort, opts.container)
|
for _, frontend := range frontends {
|
||||||
|
_, _ = fmt.Fprintln(dockerCli.Out(), net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for from, frontends := range c.NetworkSettings.Ports {
|
for from, frontends := range c.NetworkSettings.Ports {
|
||||||
for _, frontend := range frontends {
|
for _, frontend := range frontends {
|
||||||
fmt.Fprintf(dockerCli.Out(), "%s -> %s\n", from, net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
_, _ = fmt.Fprintf(dockerCli.Out(), "%s -> %s\n", from, net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue