-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathbrowser_linux.go
More file actions
45 lines (38 loc) · 1.09 KB
/
browser_linux.go
File metadata and controls
45 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package browser
import (
"errors"
"fmt"
"io/ioutil"
"os/exec"
"strings"
)
func openBrowser(url string) error {
providers := []string{"xdg-open", "x-www-browser", "www-browser"}
wsl, err := isWSL()
if err != nil {
return errors.New("error determining Windows Subsystem for Linux")
}
if wsl {
cmd := "cmd.exe"
args := []string{"/c", "start"}
url := strings.ReplaceAll(url, "&", "^&")
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
// There are multiple possible providers to open a browser on linux
// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
// Look for one that exists and run it
for _, provider := range providers {
if _, err := exec.LookPath(provider); err == nil {
return runCmd(provider, url)
}
}
return &exec.Error{Name: strings.Join(providers, ","), Err: exec.ErrNotFound}
}
func isWSL() (bool, error) {
data, err := ioutil.ReadFile("/proc/version")
if err != nil {
return false, fmt.Errorf("read /proc/version: %v", err)
}
return strings.Contains(strings.ToLower(string(data)), "microsoft"), nil
}