~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/macaroon-bakery.v1/httpbakery/browser.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package httpbakery
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "net/url"
 
6
        "os"
 
7
 
 
8
        "github.com/juju/webbrowser"
 
9
)
 
10
 
 
11
// OpenWebBrowser opens a web browser at the
 
12
// given URL. If the OS is not recognised, the URL
 
13
// is just printed to standard output.
 
14
func OpenWebBrowser(url *url.URL) error {
 
15
        err := webbrowser.Open(url)
 
16
        if err == nil {
 
17
                fmt.Fprintf(os.Stderr, "Opening an authorization web page in your browser.\n")
 
18
                fmt.Fprintf(os.Stderr, "If it does not open, please open this URL:\n%s\n", url)
 
19
                return nil
 
20
        }
 
21
        if err == webbrowser.ErrNoBrowser {
 
22
                fmt.Fprintf(os.Stderr, "Please open this URL in your browser to authorize:\n%s\n", url)
 
23
                return nil
 
24
        }
 
25
        return err
 
26
}
 
27
 
 
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{}
 
31
 
 
32
type webBrowserVisitor struct{}
 
33
 
 
34
func (webBrowserVisitor) VisitWebPage(client *Client, methodURLs map[string]*url.URL) error {
 
35
        u := methodURLs[UserInteractionMethod]
 
36
        if u == nil {
 
37
                return ErrMethodNotSupported
 
38
        }
 
39
        return OpenWebBrowser(u)
 
40
}