Skip to content

Commit

Permalink
Use the github.com/pkg/browser package for opening URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jun 13, 2024
1 parent 434798f commit caedf6b
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions engine/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package engine
import (
"fmt"
"os/exec"
"runtime"
"strings"

"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
)

// OpenURL tries to open an URL with the system browser
func (ac *Config) OpenURL(host, cPort string, httpsPrefix bool) {
func (ac *Config) OpenURL(host, cPort string, httpsPrefix bool) error {
// Build the URL
var sb strings.Builder
if httpsPrefix {
Expand All @@ -26,19 +26,13 @@ func (ac *Config) OpenURL(host, cPort string, httpsPrefix bool) {
sb.WriteString(cPort)
url := sb.String()

cmd := exec.Command(ac.openExecutable, url)
if ac.openExecutable == "" {
switch runtime.GOOS {
case "linux", "solaris":
cmd = exec.Command("xdg-open", url)
case "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
default: // darwin, bsd etc
cmd = exec.Command("open", url)
}
if ac.openExecutable != "" {
// Custom command for opening URLs
cmd := exec.Command(ac.openExecutable, url)
log.Info(fmt.Sprintf("Opening %q with %q", url, cmd.String()))
return cmd.Run()
}

// Open the URL
log.Info(fmt.Sprintf("Running: %s", cmd.String()))
cmd.Run()
log.Info(fmt.Sprintf("Opening %q in a browser", url))
return browser.OpenURL(url)
}

0 comments on commit caedf6b

Please sign in to comment.