From 55186821e4ed859e274cec6d621f024106cea4e5 Mon Sep 17 00:00:00 2001 From: Matt Calhoun Date: Thu, 15 Jun 2023 18:21:49 -0400 Subject: [PATCH 1/2] allow bind port to be set by env var --- internal/cmd/profile/flags.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/cmd/profile/flags.go b/internal/cmd/profile/flags.go index 2eff5fb..661522a 100644 --- a/internal/cmd/profile/flags.go +++ b/internal/cmd/profile/flags.go @@ -6,4 +6,5 @@ var flagBindPort = &cli.IntFlag{ Name: "bind", Usage: "[Optional] specify the port used for binding the server when logging in through a browser", Required: false, + EnvVars: []string{"SPACECTL_BIND_PORT"}, } From cd8d62ce83370003c79e03340be5a070e00ba99b Mon Sep 17 00:00:00 2001 From: Matt Calhoun Date: Thu, 15 Jun 2023 18:55:45 -0400 Subject: [PATCH 2/2] add bind host --- internal/cmd/profile/flags.go | 21 +++++++++++++++++---- internal/cmd/profile/login_command.go | 17 +++++------------ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/internal/cmd/profile/flags.go b/internal/cmd/profile/flags.go index 661522a..cc0b992 100644 --- a/internal/cmd/profile/flags.go +++ b/internal/cmd/profile/flags.go @@ -2,9 +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, - EnvVars: []string{"SPACECTL_BIND_PORT"}, + 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 {