Skip to content

Commit

Permalink
add bind host
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalhoun committed Jun 15, 2023
1 parent 5518682 commit cd8d62c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
21 changes: 17 additions & 4 deletions internal/cmd/profile/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
17 changes: 5 additions & 12 deletions internal/cmd/profile/login_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func loginCommand() *cli.Command {
ArgsUsage: "<account-alias>",
Action: loginAction,
Flags: []cli.Flag{
flagBindHost,
flagBindPort,
},
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cd8d62c

Please sign in to comment.