Skip to content

Commit

Permalink
Allow configuring optional browser open
Browse files Browse the repository at this point in the history
  • Loading branch information
ohm committed Aug 22, 2024
1 parent 99aeb64 commit 5ddb9cd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/hcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ type hcpConfig struct {
// noBrowserLogin is an option to prevent automatic browser login when no local credentials are found.
noBrowserLogin bool

// noDefaultBrowser is an option to prevent the browser login from opening the default browser.
noDefaultBrowser bool

// suppressLogging is an option to prevent this SDK from printing anything
suppressLogging bool

Expand Down
2 changes: 1 addition & 1 deletion config/tokensource.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *hcpConfig) getTokenSource() (oauth2.TokenSource, sourceType, string, er

var loginTokenSource oauth2.TokenSource
if !c.noBrowserLogin {
loginTokenSource = auth.NewBrowserLogin(&c.oauth2Config)
loginTokenSource = auth.NewBrowserLogin(&c.oauth2Config, c.noDefaultBrowser)
}

return loginTokenSource, sourceTypeLogin, "", nil
Expand Down
6 changes: 6 additions & 0 deletions config/with.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ func WithoutBrowserLogin() HCPConfigOption {
return nil
}
}
func WithoutOpenDefaultBrowser() HCPConfigOption {
return func(config *hcpConfig) error {
config.noDefaultBrowser = true
return nil
}
}

// WithoutLogging disables this SDK from printing of any kind, this is necessary
// since there is not a consistent logger that is used throughout the project so
Expand Down
11 changes: 11 additions & 0 deletions config/with_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ func TestWithout_BrowserLogin(t *testing.T) {
require.True(config.noBrowserLogin)
}

func TestWithout_OpenDefaultBrowser(t *testing.T) {
require := requirepkg.New(t)

// Exercise
config := &hcpConfig{}
require.NoError(apply(config, WithoutOpenDefaultBrowser()))

// Ensure browser login doesn't use the default browser
require.True(config.noDefaultBrowser)
}

func TestWith_CredentialFile(t *testing.T) {
require := requirepkg.New(t)

Expand Down

0 comments on commit 5ddb9cd

Please sign in to comment.