Skip to content

Commit 27fb503

Browse files
committed
added an option to open login OTP with enter
1 parent 61c639c commit 27fb503

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

cmd/auth.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,22 @@ Examples:
7171
tui.ShowWarning("Please re-run the login command to continue")
7272
os.Exit(1)
7373
}
74-
74+
75+
authURL := fmt.Sprintf("%s/auth/cli", appUrl)
76+
7577
body := tui.Paragraph(
7678
"Copy the following code:",
7779
tui.Bold(otp),
78-
"Then open the url in your browser and paste the code:",
79-
tui.Link("%s/auth/cli", appUrl),
80+
"Then open the url in your browser (Or just press ENTER) and paste the code:",
81+
tui.Link(authURL),
8082
tui.Muted("This code will expire in 60 seconds"),
8183
)
8284

8385
tui.ShowBanner("Login to Agentuity", body, false)
8486

8587
tui.ShowSpinner("Waiting for login to complete...", func() {
88+
go util.PromptBrowserOpen(logger, authURL)
89+
8690
authResult, err := auth.PollForLoginCompletion(ctx, logger, apiUrl, otp)
8791
if err != nil {
8892
if isCancelled(ctx) {
@@ -218,6 +222,9 @@ Examples:
218222
},
219223
}
220224

225+
226+
227+
221228
func init() {
222229
rootCmd.AddCommand(authCmd)
223230
authCmd.AddCommand(authLoginCmd)

internal/util/browser.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package util
22

33
import (
4+
"bufio"
45
"context"
56
"errors"
67
"fmt"
@@ -164,7 +165,7 @@ func BrowserFlow(opts BrowserFlowOptions) error {
164165
}
165166
if !skip {
166167
if berr := browser.OpenURL(u.String()); berr != nil {
167-
returnErr = fmt.Errorf("failed to open browser: %s", err)
168+
returnErr = fmt.Errorf("failed to open browser: %w", berr)
168169
return
169170
}
170171
}
@@ -189,3 +190,42 @@ func BrowserFlow(opts BrowserFlowOptions) error {
189190

190191
return returnErr
191192
}
193+
194+
// PromptBrowserOpen prompts the user to press Enter to open a browser to the given URL.
195+
// It handles display detection on Linux and provides appropriate user feedback.
196+
func PromptBrowserOpen(logger interface{ Error(string, ...interface{}) }, url string) {
197+
var skipOpen bool
198+
if runtime.GOOS == "linux" {
199+
// if we don't have a display, we can't open a browser most likely
200+
if _, ok := os.LookupEnv("DISPLAY"); !ok {
201+
skipOpen = true
202+
}
203+
}
204+
205+
fmt.Println()
206+
if skipOpen {
207+
fmt.Print(tui.Secondary("Press Enter to continue, or Ctrl+C to skip: "))
208+
} else {
209+
fmt.Print(tui.Secondary("Press Enter to open browser, or Ctrl+C to skip: "))
210+
}
211+
212+
reader := bufio.NewReader(os.Stdin)
213+
reader.ReadLine()
214+
215+
if !skipOpen {
216+
if err := browser.OpenURL(url); err != nil {
217+
logger.Error("Failed to open browser: %v", err)
218+
} else {
219+
// Clear previous line and move cursor up to remove the "Press Enter..." prompt
220+
fmt.Print("\r\033[K\033[A\r\033[K")
221+
tui.ShowSuccess("Browser opened!")
222+
fmt.Println()
223+
}
224+
} else {
225+
// Clear the prompt and show the URL for manual opening (and the loading spinner)
226+
fmt.Print("\r\033[K")
227+
fmt.Println(tui.Muted("Please visit the URL manually:"))
228+
fmt.Println(tui.Link(url))
229+
fmt.Println()
230+
}
231+
}

0 commit comments

Comments
 (0)