Skip to content

Commit 65237b5

Browse files
committed
Merge remote-tracking branch 'origin/windows-fix'
2 parents 5407605 + dfaaea7 commit 65237b5

File tree

5 files changed

+94
-29
lines changed

5 files changed

+94
-29
lines changed

browser_linux.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
package browser
22

33
import (
4-
"errors"
54
"os/exec"
5+
"strings"
66
)
77

88
func openBrowser(url string) error {
9-
browserPath, err := lookPath("xdg-open", "wslview")
10-
if err != nil {
11-
return err
12-
}
13-
return runCmd(browserPath, url)
14-
}
9+
providers := []string{"xdg-open", "x-www-browser", "www-browser", "wslview"}
1510

16-
func lookPath(cmdName, fallbackName string) (string, error) {
17-
cmdPath, err := exec.LookPath(cmdName)
18-
if errors.Is(err, exec.ErrNotFound) {
19-
if wslPath, err := exec.LookPath(fallbackName); err == nil {
20-
return wslPath, nil
11+
// There are multiple possible providers to open a browser on linux
12+
// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
13+
// Look for one that exists and run it
14+
for _, provider := range providers {
15+
if _, err := exec.LookPath(provider); err == nil {
16+
return runCmd(provider, url)
2117
}
2218
}
23-
return cmdPath, err
19+
20+
return &exec.Error{Name: strings.Join(providers, ","), Err: exec.ErrNotFound}
2421
}
2522

2623
func setFlags(cmd *exec.Cmd) {}

browser_windows.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1+
//go:generate mkwinsyscall -output zbrowser_windows.go browser_windows.go
2+
//sys shellExecute(hwnd int, verb string, file string, args string, cwd string, showCmd int) (err error) = shell32.ShellExecuteW
13
package browser
24

3-
import (
4-
"os/exec"
5-
"strings"
6-
"syscall"
5+
import "os/exec"
76

8-
"github.com/cli/safeexec"
9-
)
7+
const sW_SHOWNORMAL = 1
108

119
func openBrowser(url string) error {
12-
cmdPath, err := safeexec.LookPath("cmd")
13-
if err != nil {
14-
return err
15-
}
16-
r := strings.NewReplacer("&", "^&")
17-
return runCmd(cmdPath, "/c", "start", r.Replace(url))
10+
return shellExecute(0, "", url, "", "", sW_SHOWNORMAL)
1811
}
1912

2013
func setFlags(cmd *exec.Cmd) {
21-
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
2214
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/cli/browser
22

33
go 1.13
44

5-
require github.com/cli/safeexec v1.0.0
5+
require golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
2-
github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
1+
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d h1:jbzgAvDZn8aEnytae+4ou0J0GwFZoHR0hOrTg4qH8GA=
2+
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

zbrowser_windows.go

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)