Merge pull request #97 from docker/feat-port-parsing

Port parsing on the comand line
This commit is contained in:
Djordje Lukic 2020-05-15 17:49:18 +02:00 committed by GitHub
commit 23d2eacf84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 154 additions and 26 deletions

View file

@ -1,9 +1,9 @@
package run
import (
"fmt"
"strconv"
"strings"
"github.com/docker/go-connections/nat"
"github.com/docker/api/containers"
)
@ -14,27 +14,34 @@ type runOpts struct {
}
func toPorts(ports []string) ([]containers.Port, error) {
_, bindings, err := nat.ParsePortSpecs(ports)
if err != nil {
return nil, err
}
var result []containers.Port
for _, port := range ports {
parts := strings.Split(port, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("unable to parse ports %q", port)
}
source, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
destination, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
for port, bind := range bindings {
for _, portbind := range bind {
var hostPort uint32
if portbind.HostPort != "" {
hp, err := strconv.Atoi(portbind.HostPort)
if err != nil {
return nil, err
}
hostPort = uint32(hp)
} else {
hostPort = uint32(port.Int())
}
result = append(result, containers.Port{
Source: uint32(source),
Destination: uint32(destination),
})
result = append(result, containers.Port{
HostPort: hostPort,
ContainerPort: uint32(port.Int()),
Protocol: port.Proto(),
HostIP: portbind.HostIP,
})
}
}
return result, nil
}

99
cli/cmd/run/opts_test.go Normal file
View file

@ -0,0 +1,99 @@
package run
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/docker/api/containers"
)
type RunOptsSuite struct {
suite.Suite
}
func (s *RunOptsSuite) TestPortParse() {
testCases := []struct {
in string
expected []containers.Port
}{
{
in: "80",
expected: []containers.Port{
{
HostPort: 80,
ContainerPort: 80,
Protocol: "tcp",
},
},
},
{
in: "80:80",
expected: []containers.Port{
{
HostPort: 80,
ContainerPort: 80,
Protocol: "tcp",
},
},
},
{
in: "80:80/udp",
expected: []containers.Port{
{
ContainerPort: 80,
HostPort: 80,
Protocol: "udp",
},
},
},
{
in: "8080:80",
expected: []containers.Port{
{
HostPort: 8080,
ContainerPort: 80,
Protocol: "tcp",
},
},
},
{
in: "192.168.0.2:8080:80",
expected: []containers.Port{
{
HostPort: 8080,
ContainerPort: 80,
Protocol: "tcp",
HostIP: "192.168.0.2",
},
},
},
{
in: "80-81:80-81",
expected: []containers.Port{
{
HostPort: 80,
ContainerPort: 80,
Protocol: "tcp",
},
{
HostPort: 81,
ContainerPort: 81,
Protocol: "tcp",
},
},
},
}
for _, testCase := range testCases {
result, err := toPorts([]string{testCase.in})
require.Nil(s.T(), err)
assert.ElementsMatch(s.T(), testCase.expected, result)
}
}
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(RunOptsSuite))
}

View file

@ -50,7 +50,7 @@ func Command() *cobra.Command {
},
}
cmd.Flags().StringArrayVarP(&opts.publish, "publish", "p", []string{}, "Publish a container's port(s)")
cmd.Flags().StringArrayVarP(&opts.publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT")
cmd.Flags().StringVar(&opts.name, "name", getRandomName(), "Assign a name to the container")
return cmd