From fd1d09dfc21512e5eb69d762de4d61f2afac96ae Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Wed, 16 Sep 2020 16:56:47 +0200 Subject: [PATCH] Clean up linux logic to have better default logic --- pkg/browser/browser.go | 16 ++++++++++------ pkg/browser/browser_test.go | 18 +++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/pkg/browser/browser.go b/pkg/browser/browser.go index d972542f6..2408a2e08 100644 --- a/pkg/browser/browser.go +++ b/pkg/browser/browser.go @@ -30,11 +30,7 @@ func ForOS(goos, url string) *exec.Cmd { r := strings.NewReplacer("&", "^&") args = append(args, "/c", "start", r.Replace(url)) default: - if findExe("wslview") { - exe = "wslview" - } else { - exe = "xdg-open" - } + exe = linuxExe() args = append(args, url) } @@ -56,7 +52,15 @@ func FromLauncher(launcher, url string) (*exec.Cmd, error) { return cmd, nil } -var findExe = func(command string) bool { +var linuxExe = func() string { + exe := "xdg-open" + if !findExe("xdg-open") && findExe("wslview") { + exe = "wslview" + } + return exe +} + +func findExe(command string) bool { _, err := exec.LookPath(command) return err == nil } diff --git a/pkg/browser/browser_test.go b/pkg/browser/browser_test.go index 97e6b1a13..11dd80372 100644 --- a/pkg/browser/browser_test.go +++ b/pkg/browser/browser_test.go @@ -11,10 +11,10 @@ func TestForOS(t *testing.T) { url string } tests := []struct { - name string - args args - findExe bool - want []string + name string + args args + exe string + want []string }{ { name: "macOS", @@ -30,8 +30,8 @@ func TestForOS(t *testing.T) { goos: "linux", url: "https://example.com/path?a=1&b=2", }, - findExe: false, // wslview does not exist on standard Linux - want: []string{"xdg-open", "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", @@ -39,8 +39,8 @@ func TestForOS(t *testing.T) { goos: "linux", url: "https://example.com/path?a=1&b=2", }, - findExe: true, // wslview exists on WSL - want: []string{"wslview", "https://example.com/path?a=1&b=2"}, + exe: "wslview", + want: []string{"wslview", "https://example.com/path?a=1&b=2"}, }, { name: "Windows", @@ -53,7 +53,7 @@ func TestForOS(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - findExe = func(string) bool { return tt.findExe } + linuxExe = func() string { return tt.exe } 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) }