-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbrowser.go
50 lines (44 loc) · 974 Bytes
/
browser.go
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
46
47
48
49
50
package utils
import (
"errors"
"os"
"os/exec"
"runtime"
"github.com/kballard/go-shellquote"
)
// return the browser launcher command, copy from github.com/github/hub
func browserLauncher() ([]string, error) {
browser := os.Getenv("BROWSER")
if browser == "" {
browser = searchBrowserLauncher(runtime.GOOS)
} else {
browser = os.ExpandEnv(browser)
}
if browser == "" {
return nil, errors.New("please set $BROWSER to a web launcher")
}
return shellquote.Split(browser)
}
func searchBrowserLauncher(goos string) (browser string) {
switch goos {
case "darwin":
browser = "open"
case "windows":
browser = "cmd /c start"
case "linux":
browser = "xdg-open"
default:
browser = ""
}
return browser
}
// OpenBrowser open the url in web browser
func OpenBrowser(url string) error {
launcher, err := browserLauncher()
if err != nil {
return err
}
args := append(launcher, url)
cmd := exec.Command(args[0], args[1:]...)
return cmd.Run()
}