X Tutup
Skip to content

Commit fd1d09d

Browse files
committed
Clean up linux logic to have better default logic
1 parent 7551139 commit fd1d09d

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

pkg/browser/browser.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ func ForOS(goos, url string) *exec.Cmd {
3030
r := strings.NewReplacer("&", "^&")
3131
args = append(args, "/c", "start", r.Replace(url))
3232
default:
33-
if findExe("wslview") {
34-
exe = "wslview"
35-
} else {
36-
exe = "xdg-open"
37-
}
33+
exe = linuxExe()
3834
args = append(args, url)
3935
}
4036

@@ -56,7 +52,15 @@ func FromLauncher(launcher, url string) (*exec.Cmd, error) {
5652
return cmd, nil
5753
}
5854

59-
var findExe = func(command string) bool {
55+
var linuxExe = func() string {
56+
exe := "xdg-open"
57+
if !findExe("xdg-open") && findExe("wslview") {
58+
exe = "wslview"
59+
}
60+
return exe
61+
}
62+
63+
func findExe(command string) bool {
6064
_, err := exec.LookPath(command)
6165
return err == nil
6266
}

pkg/browser/browser_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ func TestForOS(t *testing.T) {
1111
url string
1212
}
1313
tests := []struct {
14-
name string
15-
args args
16-
findExe bool
17-
want []string
14+
name string
15+
args args
16+
exe string
17+
want []string
1818
}{
1919
{
2020
name: "macOS",
@@ -30,17 +30,17 @@ func TestForOS(t *testing.T) {
3030
goos: "linux",
3131
url: "https://example.com/path?a=1&b=2",
3232
},
33-
findExe: false, // wslview does not exist on standard Linux
34-
want: []string{"xdg-open", "https://example.com/path?a=1&b=2"},
33+
exe: "xdg-open",
34+
want: []string{"xdg-open", "https://example.com/path?a=1&b=2"},
3535
},
3636
{
3737
name: "WSL",
3838
args: args{
3939
goos: "linux",
4040
url: "https://example.com/path?a=1&b=2",
4141
},
42-
findExe: true, // wslview exists on WSL
43-
want: []string{"wslview", "https://example.com/path?a=1&b=2"},
42+
exe: "wslview",
43+
want: []string{"wslview", "https://example.com/path?a=1&b=2"},
4444
},
4545
{
4646
name: "Windows",
@@ -53,7 +53,7 @@ func TestForOS(t *testing.T) {
5353
}
5454
for _, tt := range tests {
5555
t.Run(tt.name, func(t *testing.T) {
56-
findExe = func(string) bool { return tt.findExe }
56+
linuxExe = func() string { return tt.exe }
5757
if cmd := ForOS(tt.args.goos, tt.args.url); !reflect.DeepEqual(cmd.Args, tt.want) {
5858
t.Errorf("ForOS() = %v, want %v", cmd.Args, tt.want)
5959
}

0 commit comments

Comments
 (0)
X Tutup