diff --git a/pkg/browser/browser.go b/pkg/browser/browser.go index 67ebca92f..c710a3b38 100644 --- a/pkg/browser/browser.go +++ b/pkg/browser/browser.go @@ -30,7 +30,7 @@ func ForOS(goos, url string) *exec.Cmd { r := strings.NewReplacer("&", "^&") args = append(args, "/c", "start", r.Replace(url)) default: - exe = "xdg-open" + exe = linuxExe() args = append(args, url) } @@ -51,3 +51,19 @@ func FromLauncher(launcher, url string) (*exec.Cmd, error) { cmd.Stderr = os.Stderr return cmd, nil } + +func linuxExe() string { + exe := "xdg-open" + + _, err := lookPath(exe) + if err != nil { + _, err := lookPath("wslview") + if err == nil { + exe = "wslview" + } + } + + return exe +} + +var lookPath = exec.LookPath diff --git a/pkg/browser/browser_test.go b/pkg/browser/browser_test.go index 749d0693e..48b91f7c1 100644 --- a/pkg/browser/browser_test.go +++ b/pkg/browser/browser_test.go @@ -1,6 +1,7 @@ package browser import ( + "errors" "reflect" "testing" ) @@ -13,6 +14,7 @@ func TestForOS(t *testing.T) { tests := []struct { name string args args + exe string want []string }{ { @@ -29,8 +31,18 @@ func TestForOS(t *testing.T) { goos: "linux", url: "https://example.com/path?a=1&b=2", }, + exe: "xdg-open", want: []string{"xdg-open", "https://example.com/path?a=1&b=2"}, }, + { + name: "WSL", + args: args{ + goos: "linux", + url: "https://example.com/path?a=1&b=2", + }, + exe: "wslview", + want: []string{"wslview", "https://example.com/path?a=1&b=2"}, + }, { name: "Windows", args: args{ @@ -41,6 +53,14 @@ func TestForOS(t *testing.T) { }, } for _, tt := range tests { + lookPath = func(file string) (string, error) { + if file == tt.exe { + return file, nil + } else { + return "", errors.New("not found") + } + } + t.Run(tt.name, func(t *testing.T) { if cmd := ForOS(tt.args.goos, tt.args.url); !reflect.DeepEqual(cmd.Args, tt.want) { t.Errorf("ForOS() = %v, want %v", cmd.Args, tt.want)