garm/vendor/github.com/juju/webbrowser/webbrowser.go
Gabriel Adrian Samfira bbbe67bf7c Vendor packages and add Makefile
* Vendors packages
  * Adds a Makefile that uses docker to build a static binary against musl
using alpine linux.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2022-06-30 10:20:32 +00:00

61 lines
1.5 KiB
Go

// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package webbrowser
import (
"errors"
"net/url"
"os"
"os/exec"
"runtime"
"strings"
)
// Open opens a web browser at the given URL.
// If the OS is not recognized, an ErrNoBrowser is returned.
func Open(url *url.URL) error {
var args []string
if runtime.GOOS == "windows" {
// Windows is special because the start command is built into cmd.exe
// and hence requires the argument to be quoted.
args = []string{"cmd", "/c", "start", winCmdQuote.Replace(url.String())}
} else if b := browser[runtime.GOOS]; b != "" {
args = []string{b, url.String()}
} else {
return ErrNoBrowser
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Start()
go cmd.Wait()
return nil
}
// ErrNoBrowser is returned when a browser cannot be found for the current OS.
var ErrNoBrowser = errors.New("cannot find a browser to open the web page")
var browser = map[string]string{
"darwin": "open",
"freebsd": "xdg-open",
"linux": "xdg-open",
"netbsd": "xdg-open",
"openbsd": "xdg-open",
}
// winCmdQuote can quote metacharacters special to the Windows
// cmd.exe command interpreter. It does that by inserting
// a '^' character before each metacharacter. Note that
// most of these cannot actually be produced by URL.String,
// but we include them for completeness.
var winCmdQuote = strings.NewReplacer(
"&", "^&",
"%", "^%",
"(", "^(",
")", "^)",
"^", "^^",
"<", "^<",
">", "^>",
"|", "^|",
)