aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--browserenv.go28
-rw-r--r--browserenv_unix.go25
-rw-r--r--browserenv_unix_test.go29
-rw-r--r--browserenv_windows.go15
4 files changed, 97 insertions, 0 deletions
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...)
+}
diff --git a/browserenv_unix.go b/browserenv_unix.go
new file mode 100644
index 0000000..e6416f8
--- /dev/null
+++ b/browserenv_unix.go
@@ -0,0 +1,25 @@
+// +build !windows
+
+package browserenv
+
+import (
+ "fmt"
+ "os"
+)
+
+// TODO
+func shell() (args []string) {
+ shell := os.Getenv("SHELL")
+
+ if shell == "" {
+ shell = "/bin/sh"
+ }
+
+ return []string{shell, "-c"}
+}
+
+// TODO
+func fmtBrowserCommand(browser, url string) string {
+ // TODO: handle %s in browser command
+ return fmt.Sprintf("%s '%s'", browser, url)
+}
diff --git a/browserenv_unix_test.go b/browserenv_unix_test.go
new file mode 100644
index 0000000..bcc7c2d
--- /dev/null
+++ b/browserenv_unix_test.go
@@ -0,0 +1,29 @@
+// +build !windows
+
+package browserenv
+
+import (
+ "fmt"
+ "os"
+ "reflect"
+ "testing"
+)
+
+func TestBrowserCommand(t *testing.T) {
+ envValue := "open -a Firefox"
+ url := "https://duckduckgo.com"
+
+ cmd := browserCommand(envValue, url)
+
+ shell := os.Getenv("SHELL")
+ if shell == "" {
+ shell = "/bin/sh"
+ }
+
+ browserCommand := fmt.Sprintf("%s '%s'", envValue, url)
+
+ wantArgs := []string{shell, "-c", browserCommand}
+ if !reflect.DeepEqual(cmd.Args, wantArgs) {
+ t.Errorf("got args '%#v' want '%#v'", cmd.Args, wantArgs)
+ }
+}
diff --git a/browserenv_windows.go b/browserenv_windows.go
new file mode 100644
index 0000000..ef9c2b7
--- /dev/null
+++ b/browserenv_windows.go
@@ -0,0 +1,15 @@
+package browserenv
+
+import "fmt"
+
+var shellArgs = []string{"cmd", "/c"}
+
+// TODO
+func shell() (args []string) {
+ return shellArgs
+}
+
+// TODO
+func fmtBrowserCommand(browser, url string) string {
+ return fmt.Sprintf("%s %s", browser, url)
+}