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

Commit 8b38874

Browse files
committed
introduce port command for parity with docker-compose
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 3366131 commit 8b38874

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
@@ -233,3 +233,7 @@ func (cs *aciComposeService) Top(ctx context.Context, projectName string, servic
233233
func (cs *aciComposeService) Events(ctx context.Context, project string, options compose.EventsOptions) error {
234234
return errdefs.ErrNotImplemented
235235
}
236+
237+
func (cs *aciComposeService) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
238+
return "", 0, errdefs.ErrNotImplemented
239+
}

api/client/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,7 @@ func (c *composeService) Top(ctx context.Context, projectName string, services [
107107
func (c *composeService) Events(ctx context.Context, project string, options compose.EventsOptions) error {
108108
return errdefs.ErrNotImplemented
109109
}
110+
111+
func (c *composeService) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
112+
return "", 0, errdefs.ErrNotImplemented
113+
}

api/compose/api.go

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

7375
// BuildOptions group options of the Build API
@@ -199,6 +201,12 @@ type Event struct {
199201
Attributes map[string]string
200202
}
201203

204+
// PortOptions group options of the Port API
205+
type PortOptions struct {
206+
Protocol string
207+
Index int
208+
}
209+
202210
func (e Event) String() string {
203211
t := e.Timestamp.Format("2006-01-02 15:04:05.000000")
204212
var attr []string

cli/cmd/compose/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func Command(contextType string) *cobra.Command {
140140
unpauseCommand(&opts),
141141
topCommand(&opts),
142142
eventsCommand(&opts),
143+
portCommand(&opts),
143144
)
144145

145146
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
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/docker/compose-cli/api/client"
2525
"github.com/docker/compose-cli/api/progress"
26+
"github.com/docker/compose-cli/utils"
2627
)
2728

2829
type pullOptions struct {
@@ -65,7 +66,7 @@ func runPull(ctx context.Context, opts pullOptions, services []string) error {
6566
return err
6667
}
6768
for _, s := range project.Services {
68-
if !contains(services, s.Name) {
69+
if !utils.StringContains(services, s.Name) {
6970
project.DisabledServices = append(project.DisabledServices, s)
7071
}
7172
}

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
@@ -199,3 +199,7 @@ func (e ecsLocalSimulation) Top(ctx context.Context, projectName string, service
199199
func (e ecsLocalSimulation) Events(ctx context.Context, project string, options compose.EventsOptions) error {
200200
return e.compose.Events(ctx, project, options)
201201
}
202+
203+
func (e ecsLocalSimulation) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
204+
return "", 0, errdefs.ErrNotImplemented
205+
}

ecs/up.go

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

70+
func (b *ecsAPIService) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
71+
return "", 0, errdefs.ErrNotImplemented
72+
}
73+
7074
func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
7175
logrus.Debugf("deploying on AWS with region=%q", b.Region)
7276
err := b.aws.CheckRequirements(ctx, b.Region)

0 commit comments

Comments
 (0)