@@ -41,16 +41,20 @@ func portCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Backe
4141 ProjectOptions : p ,
4242 }
4343 cmd := & cobra.Command {
44- Use : "port [OPTIONS] SERVICE PRIVATE_PORT" ,
45- Short : "Print the public port for a port binding " ,
46- Args : cobra .MinimumNArgs ( 2 ),
44+ Use : "port [OPTIONS] SERVICE [ PRIVATE_PORT] " ,
45+ Short : "List port mappings or print the public port of a specific mapping for the service " ,
46+ Args : cobra .RangeArgs ( 1 , 2 ),
4747 PreRunE : Adapt (func (ctx context.Context , args []string ) error {
48- port , err := strconv .ParseUint (args [1 ], 10 , 16 )
49- if err != nil {
50- return err
51- }
52- opts .port = uint16 (port )
5348 opts .protocol = strings .ToLower (opts .protocol )
49+ if len (args ) > 1 {
50+ port , err := strconv .ParseUint (args [1 ], 10 , 16 )
51+ if err != nil {
52+ return err
53+ }
54+ opts .port = uint16 (port )
55+ } else {
56+ opts .protocol = ""
57+ }
5458 return nil
5559 }),
5660 RunE : Adapt (func (ctx context.Context , args []string ) error {
@@ -73,14 +77,21 @@ func runPort(ctx context.Context, dockerCli command.Cli, backendOptions *Backend
7377 if err != nil {
7478 return err
7579 }
76- ip , port , err := backend .Port (ctx , projectName , service , opts .port , api.PortOptions {
80+ publishers , err := backend .Ports (ctx , projectName , service , opts .port , api.PortOptions {
7781 Protocol : opts .protocol ,
7882 Index : opts .index ,
7983 })
8084 if err != nil {
8185 return err
8286 }
8387
84- _ , _ = fmt .Fprintf (dockerCli .Out (), "%s:%d\n " , ip , port )
88+ if opts .port != 0 && len (publishers ) > 0 {
89+ p := publishers [0 ]
90+ _ , _ = fmt .Fprintf (dockerCli .Out (), "%s:%d\n " , p .URL , p .PublishedPort )
91+ return nil
92+ }
93+ for _ , p := range publishers {
94+ _ , _ = fmt .Fprintln (dockerCli .Out (), p .String ())
95+ }
8596 return nil
8697}
0 commit comments