8
"github.com/juju/webbrowser"
12
var browser = map[string]string{
13
"linux": "sensible-browser",
15
"freebsd": "xdg-open",
17
"openbsd": "xdg-open",
20
11
// OpenWebBrowser opens a web browser at the
21
12
// given URL. If the OS is not recognised, the URL
22
13
// is just printed to standard output.
23
14
func OpenWebBrowser(url *url.URL) error {
25
if runtime.GOOS == "windows" {
26
// Windows is special because the start command is
27
// built into cmd.exe and hence requires the argument
29
args = []string{"cmd", "/c", "start", winCmdQuote.Replace(url.String())}
30
} else if b := browser[runtime.GOOS]; b != "" {
31
args = []string{b, url.String()}
15
err := webbrowser.Open(url)
34
17
fmt.Fprintf(os.Stderr, "Opening an authorization web page in your browser.\n")
35
18
fmt.Fprintf(os.Stderr, "If it does not open, please open this URL:\n%s\n", url)
36
cmd := exec.Command(args[0], args[1:]...)
37
cmd.Stdout = os.Stdout
38
cmd.Stderr = os.Stderr
21
if err == webbrowser.ErrNoBrowser {
42
22
fmt.Fprintf(os.Stderr, "Please open this URL in your browser to authorize:\n%s\n", url)
47
// winCmdQuote can quote metacharacters special to the Windows
48
// cmd.exe command interpreter. It does that by inserting
49
// a '^' character before each metacharacter. Note that
50
// most of these cannot actually be produced by URL.String,
51
// but we include them for completeness.
52
var winCmdQuote = strings.NewReplacer(
28
// WebBrowserVisitor holds an interactor that supports the "Interactive"
29
// method by opening a web browser at the required location.
30
var WebBrowserVisitor Visitor = webBrowserVisitor{}
32
type webBrowserVisitor struct{}
34
func (webBrowserVisitor) VisitWebPage(client *Client, methodURLs map[string]*url.URL) error {
35
u := methodURLs[UserInteractionMethod]
37
return ErrMethodNotSupported
39
return OpenWebBrowser(u)