@@ -28,6 +28,7 @@ import (
2828
2929type portOptions struct {
3030 * projectOptions
31+ port int
3132 protocol string
3233 index int
3334}
@@ -40,25 +41,29 @@ func portCommand(p *projectOptions, backend compose.Service) *cobra.Command {
4041 Use : "port [options] [--] SERVICE PRIVATE_PORT" ,
4142 Short : "Print the public port for a port binding." ,
4243 Args : cobra .MinimumNArgs (2 ),
43- RunE : Adapt (func (ctx context.Context , args []string ) error {
44+ PreRunE : Adapt (func (ctx context.Context , args []string ) error {
4445 port , err := strconv .Atoi (args [1 ])
4546 if err != nil {
4647 return err
4748 }
48- return runPort (ctx , backend , opts , args [0 ], port )
49+ opts .port = port
50+ return nil
51+ }),
52+ RunE : Adapt (func (ctx context.Context , args []string ) error {
53+ return runPort (ctx , backend , opts , args [0 ])
4954 }),
5055 }
5156 cmd .Flags ().StringVar (& opts .protocol , "protocol" , "tcp" , "tcp or udp" )
5257 cmd .Flags ().IntVar (& opts .index , "index" , 1 , "index of the container if service has multiple replicas" )
5358 return cmd
5459}
5560
56- func runPort (ctx context.Context , backend compose.Service , opts portOptions , service string , port int ) error {
61+ func runPort (ctx context.Context , backend compose.Service , opts portOptions , service string ) error {
5762 projectName , err := opts .toProjectName ()
5863 if err != nil {
5964 return err
6065 }
61- ip , port , err := backend .Port (ctx , projectName , service , port , compose.PortOptions {
66+ ip , port , err := backend .Port (ctx , projectName , service , opts . port , compose.PortOptions {
6267 Protocol : opts .protocol ,
6368 Index : opts .index ,
6469 })
0 commit comments