Clean up linux logic to have better default logic
This commit is contained in:
parent
7551139caf
commit
fd1d09dfc2
2 changed files with 19 additions and 15 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue