1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
15
// Open opens a web browser at the given URL.
16
// If the OS is not recognized, an ErrNoBrowser is returned.
17
func Open(url *url.URL) error {
19
if runtime.GOOS == "windows" {
20
// Windows is special because the start command is built into cmd.exe
21
// and hence requires the argument to be quoted.
22
args = []string{"cmd", "/c", "start", winCmdQuote.Replace(url.String())}
23
} else if b := browser[runtime.GOOS]; b != "" {
24
args = []string{b, url.String()}
28
cmd := exec.Command(args[0], args[1:]...)
29
cmd.Stdout = os.Stdout
30
cmd.Stderr = os.Stderr
36
// ErrNoBrowser is returned when a browser cannot be found for the current OS.
37
var ErrNoBrowser = errors.New("cannot find a browser to open the web page")
39
var browser = map[string]string{
40
"linux": "sensible-browser",
42
"freebsd": "xdg-open",
44
"openbsd": "xdg-open",
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(