Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixuprising committed Oct 17, 2024
1 parent a09fb8f commit 4858c83
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions internal/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,38 @@ type Opener interface {
type StderrPrint struct{}

func openURL(url string) error {
var err error
switch runtime.GOOS {
case "darwin": // macOS
err = exec.Command("open", url).Start()
return exec.Command("open", url).Run()
case "linux":
providers := []string{"xdg-open", "x-www-browser", "www-browser"}

// There are multiple possible providers to open a browser on linux
// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
// Look for one that exists and run it
for _, provider := range providers {
if _, err := exec.LookPath(provider); err == nil {
err = exec.Command(provider, url).Start()
if bin_path, err := exec.LookPath(provider); err == nil {
err = exec.Command(bin_path, url).Run()
if err == nil {
return nil
}
}
}
return fmt.Errorf("unsupported platform")
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Run()
default:
err = fmt.Errorf("unsupported platform")
return fmt.Errorf("unsupported platform")
}
return err
}

func (p *StderrPrint) Open(u *url.URL) error {
fmt.Fprintf(
os.Stdout,
"Opening the following URL in your web browser to authenticate:\n\n\t%s\n\n",
os.Stderr,
`Attempting to automaticaly open the authentication URL in your web browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:
%s
`,
u,
)
openURL(u.String())
Expand Down

0 comments on commit 4858c83

Please sign in to comment.