aboutsummaryrefslogtreecommitdiffstats
path: root/browser.go
diff options
context:
space:
mode:
authorTeddy Wing2019-06-23 18:45:47 +0200
committerTeddy Wing2019-06-23 20:42:45 +0200
commit884bf54a000924670c0a0d86f4c10c2278ff2daf (patch)
treedc6b3cdf195fb9274c79dfb8e7476c7cd5495e34 /browser.go
parent0a3d74bf9ce488f035cf5bc36f753a711bc74334 (diff)
downloadbrowser-support-for-BROWSER-environment-variable.tar.bz2
Add support for the BROWSER environment variablesupport-for-BROWSER-environment-variable
Allow user-defined URL-opening commands via the BROWSER environment variable. This enables using a non-default browser, or changing the browser for a single command. For example, on Mac: $ export BROWSER='open -a Firefox' Windows: > setx BROWSER "start iexplore" In UNIX environments, the command is run through `$SHELL -c`, or `/bin/sh` if `$SHELL` is not set. On Windows, it uses `cmd /c`. Ensure that URLs are properly quoted between UNIX and Windows environments in `fmtBrowserCmd()`.
Diffstat (limited to 'browser.go')
-rw-r--r--browser.go17
1 files changed, 16 insertions, 1 deletions
diff --git a/browser.go b/browser.go
index 3e59690..7c60821 100644
--- a/browser.go
+++ b/browser.go
@@ -55,9 +55,24 @@ func OpenURL(url string) error {
}
func runCmd(prog string, args ...string) error {
- cmd := exec.Command(prog, args...)
+ var cmd *exec.Cmd
+
+ browser := envBrowserCmd()
+ if browser != "" {
+ sh, flag := shell()
+ url := args[len(args)-1]
+ browserCmd := fmtBrowserCmd(browser, url)
+ cmd = exec.Command(sh, flag, browserCmd)
+ } else {
+ cmd = exec.Command(prog, args...)
+ }
+
cmd.Stdout = Stdout
cmd.Stderr = Stderr
setFlags(cmd)
return cmd.Run()
}
+
+func envBrowserCmd() string {
+ return os.Getenv("BROWSER")
+}