Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit a86a473

Browse files
authored
Merge pull request #1438 from docker/port
introduce `port` command for parity with docker-compose
2 parents 2bf3e9d + 8b38874 commit a86a473

File tree

13 files changed

+167
-12
lines changed

13 files changed

+167
-12
lines changed

aci/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,7 @@ func (cs *aciComposeService) Top(ctx context.Context, projectName string, servic
237237
func (cs *aciComposeService) Events(ctx context.Context, project string, options compose.EventsOptions) error {
238238
return errdefs.ErrNotImplemented
239239
}
240+
241+
func (cs *aciComposeService) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
242+
return "", 0, errdefs.ErrNotImplemented
243+
}

api/client/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ func (c *composeService) Top(ctx context.Context, projectName string, services [
111111
func (c *composeService) Events(ctx context.Context, project string, options compose.EventsOptions) error {
112112
return errdefs.ErrNotImplemented
113113
}
114+
115+
func (c *composeService) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
116+
return "", 0, errdefs.ErrNotImplemented
117+
}

api/compose/api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ type Service interface {
7070
Top(ctx context.Context, projectName string, services []string) ([]ContainerProcSummary, error)
7171
// Events executes the equivalent to a `compose events`
7272
Events(ctx context.Context, project string, options EventsOptions) error
73+
// Port executes the equivalent to a `compose port`
74+
Port(ctx context.Context, project string, service string, port int, options PortOptions) (string, int, error)
7375
}
7476

7577
// BuildOptions group options of the Build API
@@ -207,6 +209,12 @@ type Event struct {
207209
Attributes map[string]string
208210
}
209211

212+
// PortOptions group options of the Port API
213+
type PortOptions struct {
214+
Protocol string
215+
Index int
216+
}
217+
210218
func (e Event) String() string {
211219
t := e.Timestamp.Format("2006-01-02 15:04:05.000000")
212220
var attr []string

cli/cmd/compose/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func Command(contextType string) *cobra.Command {
142142
unpauseCommand(&opts),
143143
topCommand(&opts),
144144
eventsCommand(&opts),
145+
portCommand(&opts),
145146
)
146147

147148
if contextType == store.LocalContextType || contextType == store.DefaultContextType {

cli/cmd/compose/port.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"strconv"
23+
24+
"github.com/spf13/cobra"
25+
26+
"github.com/docker/compose-cli/api/client"
27+
"github.com/docker/compose-cli/api/compose"
28+
)
29+
30+
type portOptions struct {
31+
*projectOptions
32+
protocol string
33+
index int
34+
}
35+
36+
func portCommand(p *projectOptions) *cobra.Command {
37+
opts := portOptions{
38+
projectOptions: p,
39+
}
40+
cmd := &cobra.Command{
41+
Use: "port [options] [--] SERVICE PRIVATE_PORT",
42+
Short: "Print the public port for a port binding.",
43+
Args: cobra.MinimumNArgs(2),
44+
RunE: func(cmd *cobra.Command, args []string) error {
45+
port, err := strconv.Atoi(args[1])
46+
if err != nil {
47+
return err
48+
}
49+
return runPort(cmd.Context(), opts, args[0], port)
50+
},
51+
}
52+
cmd.Flags().StringVar(&opts.protocol, "protocol", "tcp", "tcp or udp")
53+
cmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if service has multiple replicas")
54+
return cmd
55+
}
56+
57+
func runPort(ctx context.Context, opts portOptions, service string, port int) error {
58+
c, err := client.New(ctx)
59+
if err != nil {
60+
return err
61+
}
62+
63+
projectName, err := opts.toProjectName()
64+
if err != nil {
65+
return err
66+
}
67+
ip, port, err := c.ComposeService().Port(ctx, projectName, service, port, compose.PortOptions{
68+
Protocol: opts.protocol,
69+
Index: opts.index,
70+
})
71+
if err != nil {
72+
return err
73+
}
74+
75+
fmt.Printf("%s:%d\n", ip, port)
76+
return nil
77+
}

cli/cmd/compose/ps.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/docker/compose-cli/api/client"
3030
"github.com/docker/compose-cli/api/compose"
3131
"github.com/docker/compose-cli/cli/formatter"
32+
"github.com/docker/compose-cli/utils"
3233
)
3334

3435
type psOptions struct {
@@ -77,7 +78,7 @@ func runPs(ctx context.Context, opts psOptions) error {
7778
if opts.Services {
7879
services := []string{}
7980
for _, s := range containers {
80-
if !contains(services, s.Service) {
81+
if !utils.StringContains(services, s.Service) {
8182
services = append(services, s.Service)
8283
}
8384
}
@@ -115,12 +116,3 @@ func runPs(ctx context.Context, opts psOptions) error {
115116
},
116117
"NAME", "SERVICE", "STATUS", "PORTS")
117118
}
118-
119-
func contains(slice []string, item string) bool {
120-
for _, v := range slice {
121-
if v == item {
122-
return true
123-
}
124-
}
125-
return false
126-
}

cli/cmd/compose/pull.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/docker/compose-cli/api/client"
2828
"github.com/docker/compose-cli/api/progress"
29+
"github.com/docker/compose-cli/utils"
2930
)
3031

3132
type pullOptions struct {
@@ -78,7 +79,7 @@ func runPull(ctx context.Context, opts pullOptions, services []string) error {
7879
return err
7980
}
8081
for _, s := range project.Services {
81-
if !contains(services, s.Name) {
82+
if !utils.StringContains(services, s.Name) {
8283
project.DisabledServices = append(project.DisabledServices, s)
8384
}
8485
}

cli/cmd/compose/up.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"github.com/compose-spec/compose-go/types"
31+
"github.com/docker/compose-cli/utils"
3132
"github.com/sirupsen/logrus"
3233
"github.com/spf13/cobra"
3334
"golang.org/x/sync/errgroup"
@@ -106,7 +107,7 @@ func (opts upOptions) apply(project *types.Project, services []string) error {
106107
return err
107108
}
108109
for _, s := range project.Services {
109-
if !contains(services, s.Name) {
110+
if !utils.StringContains(services, s.Name) {
110111
project.DisabledServices = append(project.DisabledServices, s)
111112
}
112113
}

ecs/local/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,7 @@ func (e ecsLocalSimulation) Top(ctx context.Context, projectName string, service
203203
func (e ecsLocalSimulation) Events(ctx context.Context, project string, options compose.EventsOptions) error {
204204
return e.compose.Events(ctx, project, options)
205205
}
206+
207+
func (e ecsLocalSimulation) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
208+
return "", 0, errdefs.ErrNotImplemented
209+
}

ecs/up.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (b *ecsAPIService) Events(ctx context.Context, project string, options comp
7171
return errdefs.ErrNotImplemented
7272
}
7373

74+
func (b *ecsAPIService) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
75+
return "", 0, errdefs.ErrNotImplemented
76+
}
77+
7478
func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
7579
logrus.Debugf("deploying on AWS with region=%q", b.Region)
7680
err := b.aws.CheckRequirements(ctx, b.Region)

0 commit comments

Comments
 (0)