-
Notifications
You must be signed in to change notification settings - Fork 2
/
browser.go
51 lines (43 loc) · 998 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
51
package oauth
import (
"fmt"
"net/url"
"os/exec"
"runtime"
)
type Browser interface {
// Opens a browser with the specified url. Should be thread safe
Open(urls []*url.URL) error
// If not nil, ran in a goroutine after callback has gotten the code
Destroy() error
}
type DefaultBrowser struct {
}
func (d *DefaultBrowser) Open(urls []*url.URL) error {
var errs []error
for _, url := range urls {
err := open(url)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return fmt.Errorf("Open default browser failed: %v", errs)
}
return nil
}
func (d *DefaultBrowser) Destroy() error {
return nil
}
func open(url *url.URL) error {
switch runtime.GOOS {
case "linux":
return exec.Command("xdg-open", url.String()).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url.String()).Start()
case "darwin":
return exec.Command("open", url.String()).Start()
default:
return fmt.Errorf("unsupported platform")
}
}