From 30d8fa0a2781b6b4cc2b9c0d4301b47c95a13d05 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 19 Dec 2020 20:58:39 +0100 Subject: Add `BROWSER` support to `OpenURL()` If the `BROWSER` environment variable is set, open the given URL using the command specified in the variable. Run the command in a shell process, either from the `SHELL` environment variable or `/bin/sh` on Unix OSes. Run it through `cmd` on Windows. Essentially repurposes my code from https://github.com/pkg/browser/pull/14. --- browserenv.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'browserenv.go') diff --git a/browserenv.go b/browserenv.go index 8685fc8..2a7abcd 100644 --- a/browserenv.go +++ b/browserenv.go @@ -3,6 +3,7 @@ package browserenv import ( "io" "os" + "os/exec" "github.com/pkg/browser" ) @@ -19,5 +20,32 @@ func OpenReader(r io.Reader) error { } func OpenURL(url string) error { + envCommand := envBrowserCommand() + if envCommand != "" { + return runBrowserCommand(envCommand, url) + } + return browser.OpenURL(url) } + +// TODO +func envBrowserCommand() string { + return os.Getenv("BROWSER") +} + +// TODO +func runBrowserCommand(command, url string) error { + return browserCommand(command, url).Run() +} + +// TODO +func browserCommand(command, url string) *exec.Cmd { + shellArgs := shell() + shell := shellArgs[0] + args := shellArgs[1:] + + command = fmtBrowserCommand(command, url) + args = append(args, command) + + return exec.Command(shell, args...) +} -- cgit v1.2.3