Skip to content

Commit dd1c24a

Browse files
committed
Use LookPath to determine if xdg-open exists
1 parent 6d111b2 commit dd1c24a

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

pkg/browser/browser.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package browser
22

33
import (
4-
"bufio"
54
"os"
65
"os/exec"
76
"runtime"
@@ -31,10 +30,10 @@ func ForOS(goos, url string) *exec.Cmd {
3130
r := strings.NewReplacer("&", "^&")
3231
args = append(args, "/c", "start", r.Replace(url))
3332
default:
34-
if inWsl() {
35-
exe = "wslview"
36-
} else {
33+
if findExe("xdg-open") {
3734
exe = "xdg-open"
35+
} else {
36+
exe = "wslview"
3837
}
3938
args = append(args, url)
4039
}
@@ -57,20 +56,11 @@ func FromLauncher(launcher, url string) (*exec.Cmd, error) {
5756
return cmd, nil
5857
}
5958

60-
// Determine if gh is running inside of WSL2
61-
func inWsl() bool {
62-
file, err := os.Open("/proc/sys/kernel/osrelease")
59+
var findExe = func(command string) bool {
60+
_, err := exec.LookPath(command)
6361
if err != nil {
6462
return false
63+
} else {
64+
return true
6565
}
66-
defer file.Close()
67-
68-
scanner := bufio.NewScanner(file)
69-
scanner.Scan() // Read single line
70-
if err := scanner.Err(); err != nil {
71-
return false
72-
}
73-
74-
os := scanner.Text()
75-
return strings.Contains(os, "microsoft-WSL2")
7666
}

pkg/browser/browser_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ func TestForOS(t *testing.T) {
1111
url string
1212
}
1313
tests := []struct {
14-
name string
15-
args args
16-
want []string
14+
name string
15+
args args
16+
findExe bool
17+
want []string
1718
}{
1819
{
1920
name: "macOS",
@@ -29,7 +30,17 @@ func TestForOS(t *testing.T) {
2930
goos: "linux",
3031
url: "https://example.com/path?a=1&b=2",
3132
},
32-
want: []string{"xdg-open", "https://example.com/path?a=1&b=2"},
33+
findExe: true,
34+
want: []string{"xdg-open", "https://example.com/path?a=1&b=2"},
35+
},
36+
{
37+
name: "WSL",
38+
args: args{
39+
goos: "linux",
40+
url: "https://example.com/path?a=1&b=2",
41+
},
42+
findExe: false,
43+
want: []string{"wslview", "https://example.com/path?a=1&b=2"},
3344
},
3445
{
3546
name: "Windows",
@@ -42,6 +53,7 @@ func TestForOS(t *testing.T) {
4253
}
4354
for _, tt := range tests {
4455
t.Run(tt.name, func(t *testing.T) {
56+
findExe = func(string) bool { return tt.findExe }
4557
if cmd := ForOS(tt.args.goos, tt.args.url); !reflect.DeepEqual(cmd.Args, tt.want) {
4658
t.Errorf("ForOS() = %v, want %v", cmd.Args, tt.want)
4759
}

0 commit comments

Comments
 (0)