Skip to content

Commit 3503981

Browse files
committed
fix: prompt before opening browser
1 parent f6efb74 commit 3503981

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

pkg/cmd/auth.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ func handleAuthLogin(ctx context.Context, cmd *cli.Command) error {
5252
cc := getAPICommandContext(cmd)
5353
clientID := cmd.String("client-id")
5454
scope := "openapi:read project:write project:read"
55-
config, err := startDeviceFlow(ctx, cc.client, clientID, scope)
55+
config, err := startDeviceFlow(ctx, cmd, cc.client, clientID, scope)
5656
if err != nil {
5757
return err
5858
}
5959
if err := SaveAuthConfig(config); err != nil {
6060
Error("Failed to save authentication: %v", err)
6161
return fmt.Errorf("authentication failed")
6262
}
63-
Success("Authentication successful! Your credentials have been saved.")
63+
Success("Authentication successful! Your credentials have been saved to.")
6464
return nil
6565
}
6666

@@ -179,7 +179,7 @@ func handleAuthStatus(ctx context.Context, cmd *cli.Command) error {
179179
}
180180

181181
// startDeviceFlow initiates the OAuth 2.0 device flow
182-
func startDeviceFlow(ctx context.Context, client stainless.Client, clientID, scope string) (*AuthConfig, error) {
182+
func startDeviceFlow(ctx context.Context, cmd *cli.Command, client stainless.Client, clientID, scope string) (*AuthConfig, error) {
183183
var deviceResponse struct {
184184
DeviceCode string `json:"device_code"`
185185
UserCode string `json:"user_code"`
@@ -198,14 +198,20 @@ func startDeviceFlow(ctx context.Context, client stainless.Client, clientID, sco
198198
return nil, err
199199
}
200200

201-
if err := browser.OpenURL(deviceResponse.VerificationURIComplete); err != nil {
202-
group := Info("To authenticate, visit the verification URL")
203-
group.Property("url", deviceResponse.VerificationURI)
204-
group.Property("code", deviceResponse.UserCode)
205-
group.Property("direct_url", deviceResponse.VerificationURIComplete)
206-
} else {
207-
group := Info("Browser opened")
208-
group.Property("url", deviceResponse.VerificationURIComplete)
201+
group := Info("To authenticate, visit the verification URL")
202+
group.Property("url", deviceResponse.VerificationURIComplete)
203+
group.Property("code", deviceResponse.UserCode)
204+
205+
ok, _, err := group.Confirm(cmd, "browser", "Open browser?", "", true)
206+
if err != nil {
207+
return nil, err
208+
}
209+
if ok {
210+
if err := browser.OpenURL(deviceResponse.VerificationURIComplete); err == nil {
211+
group.Info("Opening browser...")
212+
} else {
213+
group.Warn("Could not open browser")
214+
}
209215
}
210216

211217
return pollForToken(
@@ -249,7 +255,7 @@ func pollForToken(ctx context.Context, client stainless.Client, clientID, device
249255
var apierr *stainless.Error
250256
if errors.As(err, &apierr) {
251257
// If we got an error, check if it's "authorization_pending" and continue polling
252-
errorStr := gjson.Get(apierr.RawJSON(), "error.error").String()
258+
errorStr := gjson.Get(apierr.RawJSON(), "error").String()
253259
// This is expected, continue polling
254260
if errorStr == "authorization_pending" {
255261
continue

pkg/cmd/init.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,16 @@ func handleInit(ctx context.Context, cmd *cli.Command) error {
101101
if len(orgs) == 0 {
102102
signupURL := "https://app.stainless.com/signup?source=cli"
103103
group := Info("Creating organization for user...")
104-
group.Property("signup_url", signupURL)
105-
if err := browser.OpenURL(signupURL); err != nil {
106-
group.Info("Browser opened")
104+
group.Property("url", signupURL)
105+
106+
ok, err := Confirm(cmd, "browser", "Open browser?", "", true)
107+
if err != nil {
108+
return err
109+
}
110+
if ok {
111+
if err := browser.OpenURL(signupURL); err != nil {
112+
Info("Opening browser...")
113+
}
107114
}
108115

109116
group.Progress("Waiting for organization to be created...")

pkg/cmd/print.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func Success(format string, args ...any) Group {
4848
return Group{}.Success(format, args...)
4949
}
5050

51+
func Confirm(cmd *cli.Command, flagName, title, description string, defaultValue bool) (bool, error) {
52+
value, _, err := Group{}.Confirm(cmd, flagName, title, description, defaultValue)
53+
return value, err
54+
}
55+
5156
func Spacer() {
5257
fmt.Fprintf(os.Stderr, "\n")
5358
}
@@ -138,16 +143,15 @@ func (g Group) Success(format string, args ...any) Group {
138143
return Group{prefix: "✓", indent: g.indent + 1}
139144
}
140145

141-
// Confirm prompts the user with a yes/no question if the flag wasn't explicitly set
142-
func Confirm(cmd *cli.Command, flagName, title, description string, defaultValue bool) (bool, error) {
146+
func (g Group) Confirm(cmd *cli.Command, flagName, title, description string, defaultValue bool) (bool, Group, error) {
143147
if cmd.IsSet(flagName) {
144-
return cmd.Bool(flagName), nil
148+
return cmd.Bool(flagName), Group{prefix: "✱", indent: g.indent + 1}, nil
145149
}
146150

147151
foreground := lipgloss.Color("15")
148152
cyanBright := lipgloss.Color("6")
149153

150-
t := GetFormTheme(0)
154+
t := GetFormTheme(g.indent)
151155
t.Focused.Title = t.Focused.Title.Foreground(foreground)
152156
t.Focused.Base = t.Focused.Base.
153157
SetString("\b\b" + lipgloss.NewStyle().Foreground(cyanBright).Render("✱")).
@@ -163,5 +167,5 @@ func Confirm(cmd *cli.Command, flagName, title, description string, defaultValue
163167
),
164168
).WithTheme(t).WithKeyMap(GetFormKeyMap()).Run()
165169

166-
return value, err
170+
return value, Group{prefix: "✱", indent: g.indent + 1}, err
167171
}

0 commit comments

Comments
 (0)