From ba867705c4327f7e08ec92eb3ca2298213e85782 Mon Sep 17 00:00:00 2001 From: Rohan CJ Date: Wed, 28 Aug 2024 13:06:48 +0530 Subject: [PATCH] fix: improve error logging around attempting to open browser Signed-off-by: Rohan CJ --- cmd/vclusterctl/cmd/ui.go | 11 +++++++++-- pkg/cli/start/login.go | 14 +++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cmd/vclusterctl/cmd/ui.go b/cmd/vclusterctl/cmd/ui.go index d09227b4c..a85d3cfe7 100644 --- a/cmd/vclusterctl/cmd/ui.go +++ b/cmd/vclusterctl/cmd/ui.go @@ -2,8 +2,10 @@ package cmd import ( "context" + "errors" "fmt" "os" + "os/exec" "github.com/loft-sh/api/v4/pkg/product" "github.com/loft-sh/log" @@ -63,10 +65,15 @@ func (cmd *UICmd) Run(ctx context.Context) error { return fmt.Errorf("please login first using '%s' or start using '%s'", product.LoginCmd(), product.StartCmd()) } + // still open the UI err = open.Run(url) - if err != nil { - return fmt.Errorf("error opening url: %w", err) + if errors.Is(err, exec.ErrNotFound) { + cmd.Log.Warnf("Couldn't open the login page in a browser. No browser found: %v", err) + } else if err != nil { + return fmt.Errorf("couldn't open the login page in a browser: %w", err) } + cmd.Log.Infof("If the browser does not open automatically, please navigate to %s", url) + return nil } diff --git a/pkg/cli/start/login.go b/pkg/cli/start/login.go index da5bcf825..c49bf309b 100644 --- a/pkg/cli/start/login.go +++ b/pkg/cli/start/login.go @@ -4,10 +4,12 @@ import ( "bytes" "crypto/tls" "encoding/json" + "errors" "fmt" "io" "net/http" netUrl "net/url" + "os/exec" "strings" types "github.com/loft-sh/api/v4/pkg/auth" @@ -29,10 +31,14 @@ func (l *LoftStarter) login(url string) error { if l.isLoggedIn(url) { // still open the UI err := open.Run(url) - if err != nil { + if errors.Is(err, exec.ErrNotFound) { + l.Log.Warnf("Couldn't open the login page in a browser. No browser found: %v", err) + } else if err != nil { return fmt.Errorf("couldn't open the login page in a browser: %w", err) } + l.Log.Infof("If the browser does not open automatically, please navigate to %s", url) + return nil } @@ -107,11 +113,13 @@ func (l *LoftStarter) loginUI(url string) error { loginURL := fmt.Sprintf("%s/login#%s", url, queryString) err := open.Run(loginURL) - if err != nil { + if errors.Is(err, exec.ErrNotFound) { + l.Log.Warnf("Couldn't open the login page in a browser. No browser found: %v", err) + } else if err != nil { return fmt.Errorf("couldn't open the login page in a browser: %w", err) } - l.Log.Infof("If the browser does not open automatically, please navigate to %s", loginURL) + l.Log.Infof("If the browser does not open automatically, please navigate to %s", url) return nil }