Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error logging around attempting to open browser #2122

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading