diff --git a/internal/cmd/profile/flags.go b/internal/cmd/profile/flags.go index 2eff5fb..cc0b992 100644 --- a/internal/cmd/profile/flags.go +++ b/internal/cmd/profile/flags.go @@ -2,8 +2,22 @@ package profile import "github.com/urfave/cli/v2" +var bindHost string +var flagBindHost = &cli.StringFlag{ + Name: "bind-host", + Usage: "[Optional] specify the host used for binding the server when logging in through a browser", + Required: false, + Value: "localhost", + Destination: &bindHost, + EnvVars: []string{"SPACECTL_BIND_HOST"}, +} + +var bindPort int var flagBindPort = &cli.IntFlag{ - Name: "bind", - Usage: "[Optional] specify the port used for binding the server when logging in through a browser", - Required: false, + Name: "bind", + Usage: "[Optional] specify the port used for binding the server when logging in through a browser", + Required: false, + Value: 0, + Destination: &bindPort, + EnvVars: []string{"SPACECTL_BIND_PORT"}, } diff --git a/internal/cmd/profile/login_command.go b/internal/cmd/profile/login_command.go index 1c83617..4049611 100644 --- a/internal/cmd/profile/login_command.go +++ b/internal/cmd/profile/login_command.go @@ -38,6 +38,7 @@ func loginCommand() *cli.Command { ArgsUsage: "", Action: loginAction, Flags: []cli.Flag{ + flagBindHost, flagBindPort, }, } @@ -222,13 +223,7 @@ func loginUsingWebBrowser(ctx *cli.Context, creds *session.StoredCredentials) er done <- true } - var bindOn *int - if ctx.IsSet(flagBindPort.Name) { - port := ctx.Int(flagBindPort.Name) - bindOn = &port - } - - server, port, err := serveOnOpenPort(bindOn, handler) + server, port, err := serveOnOpenPort(&bindHost, &bindPort, handler) if err != nil { return err } @@ -283,11 +278,9 @@ func persistAccessCredentials(creds *session.StoredCredentials) error { }) } -func serveOnOpenPort(port *int, handler func(w http.ResponseWriter, r *http.Request)) (*http.Server, int, error) { - bindOn := "localhost:0" - if port != nil { - bindOn = fmt.Sprintf("localhost:%d", *port) - } +func serveOnOpenPort(host *string, port *int, handler func(w http.ResponseWriter, r *http.Request)) (*http.Server, int, error) { + + bindOn := fmt.Sprintf("%s:%d", *host, *port) addr, err := net.ResolveTCPAddr("tcp", bindOn) if err != nil {