From 81ce0ca4e82c3732b1d22027e65b2fc1193614b2 Mon Sep 17 00:00:00 2001 From: Yoav Date: Wed, 18 Jun 2025 13:55:25 +0200 Subject: [PATCH 1/2] added an option to open login OTP with enter --- cmd/auth.go | 13 ++++++++++--- internal/util/browser.go | 42 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/cmd/auth.go b/cmd/auth.go index cf8c1d25..8940b4b6 100644 --- a/cmd/auth.go +++ b/cmd/auth.go @@ -71,18 +71,22 @@ Examples: tui.ShowWarning("Please re-run the login command to continue") 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(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) { @@ -218,6 +222,9 @@ Examples: }, } + + + func init() { rootCmd.AddCommand(authCmd) authCmd.AddCommand(authLoginCmd) diff --git a/internal/util/browser.go b/internal/util/browser.go index 14130058..fc19b69b 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(url)) + fmt.Println() + } +} From cc239c82a855b5d6f062b09b9a0a74faa0dd3957 Mon Sep 17 00:00:00 2001 From: yoav0gal Date: Wed, 16 Jul 2025 13:23:11 +0300 Subject: [PATCH 2/2] code rabbit fixes --- cmd/auth.go | 11 ++++------- internal/util/browser.go | 6 +++--- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/cmd/auth.go b/cmd/auth.go index 8940b4b6..194626ec 100644 --- a/cmd/auth.go +++ b/cmd/auth.go @@ -71,14 +71,14 @@ Examples: tui.ShowWarning("Please re-run the login command to continue") 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 (Or just press ENTER) and paste the code:", - tui.Link(authURL), + "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"), ) @@ -222,9 +222,6 @@ Examples: }, } - - - func init() { rootCmd.AddCommand(authCmd) authCmd.AddCommand(authLoginCmd) diff --git a/internal/util/browser.go b/internal/util/browser.go index fc19b69b..53572d40 100644 --- a/internal/util/browser.go +++ b/internal/util/browser.go @@ -165,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: %w", berr) + returnErr = fmt.Errorf("failed to open browser: %w", berr) return } } @@ -208,7 +208,7 @@ func PromptBrowserOpen(logger interface{ Error(string, ...interface{}) }, url st } else { fmt.Print(tui.Secondary("Press Enter to open browser, or Ctrl+C to skip: ")) } - + reader := bufio.NewReader(os.Stdin) reader.ReadLine() @@ -225,7 +225,7 @@ func PromptBrowserOpen(logger interface{ Error(string, ...interface{}) }, url st // 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(url)) + fmt.Println(tui.Link("%s", url)) fmt.Println() } }