Skip to content

Commit

Permalink
RSDK-9295 - refactor disable-web-browser name and behavior (#4653)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog authored Dec 30, 2024
1 parent dfe5947 commit 4e8c584
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,9 @@ var app = &cli.App{
HideHelpCommand: true,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: loginFlagDisableBrowser,
Usage: "prevent opening the default browser during login",
Name: loginFlagDisableBrowser,
Aliases: []string{"no-browser"}, // ease of use alias, not related to backwards compatibility
Usage: "prevent opening the default browser during login",
},
},
Action: createCommandWithT[loginActionArgs](LoginAction),
Expand Down
18 changes: 12 additions & 6 deletions cli/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (c *viamClient) loginAction(cCtx *cli.Context) error {
return errors.New("error while refreshing token, logging out. Please log in again")
}
} else {
t, err = c.authFlow.loginAsUser(c.c.Context)
t, err = c.authFlow.loginAsUser(c.c)
if err != nil {
debugf(c.c.App.Writer, globalArgs.Debug, "Login error: %v", err)

Expand Down Expand Up @@ -700,7 +700,8 @@ func newCLIAuthFlowWithAuthDomain(authDomain, audience, clientID string, console
}
}

func (a *authFlow) loginAsUser(ctx context.Context) (*token, error) {
func (a *authFlow) loginAsUser(c *cli.Context) (*token, error) {
ctx := c.Context
discovery, err := a.loadOIDiscoveryEndpoint(ctx)
if err != nil {
return nil, errors.Wrapf(err, "failed retrieving discovery endpoint")
Expand All @@ -713,8 +714,9 @@ func (a *authFlow) loginAsUser(ctx context.Context) (*token, error) {

err = a.directUser(deviceCode)
if err != nil {
return nil, fmt.Errorf("unable to open the browser to complete the login flow due to %w."+
"You can use the --%s flag to skip this behavior", err, loginFlagDisableBrowser)
warningf(c.App.ErrWriter, "unable to open the browser to complete the login flow due to %w. "+
"Please go to the provided URL to log in; you can use the --%s flag to skip this warning in the future",
err, loginFlagDisableBrowser)
}

token, err := a.waitForUser(ctx, deviceCode, discovery)
Expand Down Expand Up @@ -781,9 +783,13 @@ func (a *authFlow) makeDeviceCodeRequest(ctx context.Context, discovery *openIDD
}

func (a *authFlow) directUser(code *deviceCodeResponse) error {
infof(a.console, `You can log into Viam through the opened browser window or follow the URL below.
suggestedLoginMethods := ""
if !a.disableBrowserOpen {
suggestedLoginMethods = " through the opened browser window or"
}
infof(a.console, `You can log into Viam%s by following the URL below.
Ensure the code in the URL matches the one shown in your browser.
%s`, code.VerificationURIComplete)
%s`, suggestedLoginMethods, code.VerificationURIComplete)

if a.disableBrowserOpen {
return nil
Expand Down

0 comments on commit 4e8c584

Please sign in to comment.