diff --git a/cmd/auth.go b/cmd/auth.go index cf8c1d25..194626ec 100644 --- a/cmd/auth.go +++ b/cmd/auth.go @@ -72,17 +72,21 @@ Examples: os.Exit(1) } + authURL := fmt.Sprintf("%s/auth/cli", appUrl) + body := tui.Paragraph( "Copy the following code:", tui.Bold(otp), - "Then open the url in your browser and paste the code:", - tui.Link("%s/auth/cli", appUrl), + "Then open the url in your browser (or just press ENTER) and paste the code:", + tui.Link("%s", authURL), tui.Muted("This code will expire in 60 seconds"), ) tui.ShowBanner("Login to Agentuity", body, false) tui.ShowSpinner("Waiting for login to complete...", func() { + go util.PromptBrowserOpen(logger, authURL) + authResult, err := auth.PollForLoginCompletion(ctx, logger, apiUrl, otp) if err != nil { if isCancelled(ctx) { diff --git a/internal/util/browser.go b/internal/util/browser.go index 14130058..53572d40 100644 --- a/internal/util/browser.go +++ b/internal/util/browser.go @@ -1,6 +1,7 @@ package util import ( + "bufio" "context" "errors" "fmt" @@ -164,7 +165,7 @@ func BrowserFlow(opts BrowserFlowOptions) error { } if !skip { if berr := browser.OpenURL(u.String()); berr != nil { - returnErr = fmt.Errorf("failed to open browser: %s", err) + returnErr = fmt.Errorf("failed to open browser: %w", berr) return } } @@ -189,3 +190,42 @@ func BrowserFlow(opts BrowserFlowOptions) error { return returnErr } + +// PromptBrowserOpen prompts the user to press Enter to open a browser to the given URL. +// It handles display detection on Linux and provides appropriate user feedback. +func PromptBrowserOpen(logger interface{ Error(string, ...interface{}) }, url string) { + var skipOpen bool + if runtime.GOOS == "linux" { + // if we don't have a display, we can't open a browser most likely + if _, ok := os.LookupEnv("DISPLAY"); !ok { + skipOpen = true + } + } + + fmt.Println() + if skipOpen { + fmt.Print(tui.Secondary("Press Enter to continue, or Ctrl+C to skip: ")) + } else { + fmt.Print(tui.Secondary("Press Enter to open browser, or Ctrl+C to skip: ")) + } + + reader := bufio.NewReader(os.Stdin) + reader.ReadLine() + + if !skipOpen { + if err := browser.OpenURL(url); err != nil { + logger.Error("Failed to open browser: %v", err) + } else { + // Clear previous line and move cursor up to remove the "Press Enter..." prompt + fmt.Print("\r\033[K\033[A\r\033[K") + tui.ShowSuccess("Browser opened!") + fmt.Println() + } + } else { + // Clear the prompt and show the URL for manual opening (and the loading spinner) + fmt.Print("\r\033[K") + fmt.Println(tui.Muted("Please visit the URL manually:")) + fmt.Println(tui.Link("%s", url)) + fmt.Println() + } +}