~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

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

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
        "fmt"
5
5
        "net/url"
6
6
        "os"
7
 
        "os/exec"
8
 
        "runtime"
9
 
        "strings"
 
7
 
 
8
        "github.com/juju/webbrowser"
10
9
)
11
10
 
12
 
var browser = map[string]string{
13
 
        "linux":   "sensible-browser",
14
 
        "darwin":  "open",
15
 
        "freebsd": "xdg-open",
16
 
        "netbsd":  "xdg-open",
17
 
        "openbsd": "xdg-open",
18
 
}
19
 
 
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 {
24
 
        var args []string
25
 
        if runtime.GOOS == "windows" {
26
 
                // Windows is special because the start command is
27
 
                // built into cmd.exe and hence requires the argument
28
 
                // to be quoted.
29
 
                args = []string{"cmd", "/c", "start", winCmdQuote.Replace(url.String())}
30
 
        } else if b := browser[runtime.GOOS]; b != "" {
31
 
                args = []string{b, url.String()}
32
 
        }
33
 
        if args != nil {
 
15
        err := webbrowser.Open(url)
 
16
        if err == nil {
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
39
 
                cmd.Start()
40
 
                go cmd.Wait()
41
 
        } else {
 
19
                return nil
 
20
        }
 
21
        if err == webbrowser.ErrNoBrowser {
42
22
                fmt.Fprintf(os.Stderr, "Please open this URL in your browser to authorize:\n%s\n", url)
43
 
        }
44
 
        return nil
45
 
}
46
 
 
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(
53
 
        "&", "^&",
54
 
        "%", "^%",
55
 
        "(", "^(",
56
 
        ")", "^)",
57
 
        "^", "^^",
58
 
        "<", "^<",
59
 
        ">", "^>",
60
 
        "|", "^|",
61
 
)
 
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
}