Skip to content

Commit

Permalink
fix: improve error logging around attempting to open browser
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan CJ <[email protected]>
  • Loading branch information
rohantmp committed Sep 4, 2024
1 parent 7e2a6f9 commit ba86770
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 9 additions & 2 deletions cmd/vclusterctl/cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
14 changes: 11 additions & 3 deletions pkg/cli/start/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit ba86770

Please sign in to comment.