Use LookPath to determine if xdg-open exists
This commit is contained in:
parent
6d111b2458
commit
dd1c24a20a
2 changed files with 23 additions and 21 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package browser
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
|
|
@ -31,10 +30,10 @@ func ForOS(goos, url string) *exec.Cmd {
|
|||
r := strings.NewReplacer("&", "^&")
|
||||
args = append(args, "/c", "start", r.Replace(url))
|
||||
default:
|
||||
if inWsl() {
|
||||
exe = "wslview"
|
||||
} else {
|
||||
if findExe("xdg-open") {
|
||||
exe = "xdg-open"
|
||||
} else {
|
||||
exe = "wslview"
|
||||
}
|
||||
args = append(args, url)
|
||||
}
|
||||
|
|
@ -57,20 +56,11 @@ func FromLauncher(launcher, url string) (*exec.Cmd, error) {
|
|||
return cmd, nil
|
||||
}
|
||||
|
||||
// Determine if gh is running inside of WSL2
|
||||
func inWsl() bool {
|
||||
file, err := os.Open("/proc/sys/kernel/osrelease")
|
||||
var findExe = func(command string) bool {
|
||||
_, err := exec.LookPath(command)
|
||||
if err != nil {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Scan() // Read single line
|
||||
if err := scanner.Err(); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
os := scanner.Text()
|
||||
return strings.Contains(os, "microsoft-WSL2")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,9 +11,10 @@ func TestForOS(t *testing.T) {
|
|||
url string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []string
|
||||
name string
|
||||
args args
|
||||
findExe bool
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "macOS",
|
||||
|
|
@ -29,7 +30,17 @@ func TestForOS(t *testing.T) {
|
|||
goos: "linux",
|
||||
url: "https://example.com/path?a=1&b=2",
|
||||
},
|
||||
want: []string{"xdg-open", "https://example.com/path?a=1&b=2"},
|
||||
findExe: true,
|
||||
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",
|
||||
},
|
||||
findExe: false,
|
||||
want: []string{"wslview", "https://example.com/path?a=1&b=2"},
|
||||
},
|
||||
{
|
||||
name: "Windows",
|
||||
|
|
@ -42,6 +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 }
|
||||
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