mirror of
https://github.com/docker/compose.git
synced 2026-08-30 13:31:54 +00:00
Implement printing published ports
This commit is contained in:
parent
23d2eacf84
commit
d8a38afecc
14 changed files with 240 additions and 48 deletions
|
|
@ -9,6 +9,9 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
|
||||
"github.com/docker/api/cli/formatter"
|
||||
"github.com/docker/api/client"
|
||||
)
|
||||
|
||||
|
|
@ -50,11 +53,11 @@ func runPs(ctx context.Context, opts psOpts) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintf(w, "NAME\tIMAGE\tSTATUS\tCOMMAND\n")
|
||||
format := "%s\t%s\t%s\t%s\n"
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 8, ' ', 0)
|
||||
fmt.Fprintf(w, "CONTAINER ID\tIMAGE\tCOMMAND\tSTATUS\tPORTS\n")
|
||||
format := "%s\t%s\t%s\t%s\t%s\n"
|
||||
for _, c := range containers {
|
||||
fmt.Fprintf(w, format, c.ID, c.Image, c.Status, c.Command)
|
||||
fmt.Fprintf(w, format, stringid.TruncateID(c.ID), c.Image, c.Command, c.Status, formatter.PortsString(c.Ports))
|
||||
}
|
||||
|
||||
return w.Flush()
|
||||
|
|
|
|||
|
|
@ -1,59 +0,0 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/docker/go-connections/nat"
|
||||
|
||||
"github.com/docker/api/containers"
|
||||
)
|
||||
|
||||
type runOpts struct {
|
||||
name string
|
||||
publish []string
|
||||
}
|
||||
|
||||
func toPorts(ports []string) ([]containers.Port, error) {
|
||||
_, bindings, err := nat.ParsePortSpecs(ports)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result []containers.Port
|
||||
|
||||
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{
|
||||
HostPort: hostPort,
|
||||
ContainerPort: uint32(port.Int()),
|
||||
Protocol: port.Proto(),
|
||||
HostIP: portbind.HostIP,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *runOpts) toContainerConfig(image string) (containers.ContainerConfig, error) {
|
||||
publish, err := toPorts(r.publish)
|
||||
if err != nil {
|
||||
return containers.ContainerConfig{}, err
|
||||
}
|
||||
|
||||
return containers.ContainerConfig{
|
||||
ID: r.name,
|
||||
Image: image,
|
||||
Ports: publish,
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -1,99 +0,0 @@
|
|||
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))
|
||||
}
|
||||
|
|
@ -35,12 +35,13 @@ import (
|
|||
"github.com/docker/docker/pkg/namesgenerator"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/api/cli/options/run"
|
||||
"github.com/docker/api/client"
|
||||
)
|
||||
|
||||
// Command runs a container
|
||||
func Command() *cobra.Command {
|
||||
var opts runOpts
|
||||
var opts run.Opts
|
||||
cmd := &cobra.Command{
|
||||
Use: "run",
|
||||
Short: "Run a container",
|
||||
|
|
@ -50,19 +51,19 @@ func Command() *cobra.Command {
|
|||
},
|
||||
}
|
||||
|
||||
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")
|
||||
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
|
||||
}
|
||||
|
||||
func runRun(ctx context.Context, image string, opts runOpts) error {
|
||||
func runRun(ctx context.Context, image string, opts run.Opts) error {
|
||||
c, err := client.New(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
project, err := opts.toContainerConfig(image)
|
||||
project, err := opts.ToContainerConfig(image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -70,7 +71,8 @@ func runRun(ctx context.Context, image string, opts runOpts) error {
|
|||
if err = c.ContainerService().Run(ctx, project); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(opts.name)
|
||||
fmt.Println(opts.Name)
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
|
|
|||
6
cli/cmd/testdata/ps-out.golden
vendored
6
cli/cmd/testdata/ps-out.golden
vendored
|
|
@ -1,3 +1,3 @@
|
|||
NAME IMAGE STATUS COMMAND
|
||||
id nginx
|
||||
1234 alpine
|
||||
CONTAINER ID IMAGE COMMAND STATUS PORTS
|
||||
id nginx
|
||||
1234 alpine
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue