Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functional options to allow configuring the underlying Cmd #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ var Stdout io.Writer = os.Stdout
// Stderr is the io.Writer to which executed commands write standard error.
var Stderr io.Writer = os.Stderr

type CmdOption func (*exec.Cmd)

// OpenFile opens new browser window for the file path.
func OpenFile(path string) error {
// options can be used to configure the underlying exec.Cmd, if any
func OpenFile(path string, options ...CmdOption) error {
path, err := filepath.Abs(path)
if err != nil {
return err
}
return OpenURL("file://" + path)
return OpenURL("file://" + path, options...)
}

// OpenReader consumes the contents of r and presents the
// results in a new browser window.
func OpenReader(r io.Reader) error {
// options can be used to configure the underlying exec.Cmd, if any
func OpenReader(r io.Reader, options ...CmdOption) error {
f, err := ioutil.TempFile("", "browser")
if err != nil {
return fmt.Errorf("browser: could not create temporary file: %v", err)
Expand All @@ -46,18 +50,22 @@ func OpenReader(r io.Reader) error {
if err := os.Rename(oldname, newname); err != nil {
return fmt.Errorf("browser: renaming temporary file failed: %v", err)
}
return OpenFile(newname)
return OpenFile(newname, options...)
}

// OpenURL opens a new browser window pointing to url.
func OpenURL(url string) error {
return openBrowser(url)
// options can be used to configure the underlying exec.Cmd, if any
func OpenURL(url string, options ...CmdOption) error {
return openBrowser(url, options)
}

func runCmd(prog string, args ...string) error {
func runCmd(prog string, args []string, options []CmdOption) error {
cmd := exec.Command(prog, args...)
cmd.Stdout = Stdout
cmd.Stderr = Stderr
setFlags(cmd)
for _, o := range options {
o(cmd)
}
return cmd.Run()
}
4 changes: 2 additions & 2 deletions browser_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package browser

import "os/exec"

func openBrowser(url string) error {
return runCmd("open", url)
func openBrowser(url string, cmdOptions []CmdOption) error {
return runCmd("open", []string{url}, cmdOptions)
}

func setFlags(cmd *exec.Cmd) {}
4 changes: 2 additions & 2 deletions browser_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os/exec"
)

func openBrowser(url string) error {
err := runCmd("xdg-open", url)
func openBrowser(url string, cmdOptions []CmdOption) error {
err := runCmd("xdg-open", []string{url}, cmdOptions)
if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound {
return errors.New("xdg-open: command not found - install xdg-utils from ports(8)")
}
Expand Down
4 changes: 2 additions & 2 deletions browser_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"strings"
)

func openBrowser(url string) error {
func openBrowser(url string, cmdOptions []CmdOption) error {
providers := []string{"xdg-open", "x-www-browser", "www-browser"}

// There are multiple possible providers to open a browser on linux
// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
// Look for one that exists and run it
for _, provider := range providers {
if _, err := exec.LookPath(provider); err == nil {
return runCmd(provider, url)
return runCmd(provider, []string{url}, cmdOptions)
}
}

Expand Down
4 changes: 2 additions & 2 deletions browser_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os/exec"
)

func openBrowser(url string) error {
err := runCmd("xdg-open", url)
func openBrowser(url string, cmdOptions []CmdOption) error {
err := runCmd("xdg-open", []string{url}, cmdOptions)
if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound {
return errors.New("xdg-open: command not found - install xdg-utils from ports(8)")
}
Expand Down
2 changes: 1 addition & 1 deletion browser_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"runtime"
)

func openBrowser(url string) error {
func openBrowser(url string, cmdOptions []CmdOption) error {
return fmt.Errorf("openBrowser: unsupported operating system: %v", runtime.GOOS)
}

Expand Down
2 changes: 1 addition & 1 deletion browser_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package browser
import "os/exec"
const SW_SHOWNORMAL = 1

func openBrowser(url string) error {
func openBrowser(url string, cmdOptions []CmdOption) error {
return ShellExecute(0, "", url, "", "", SW_SHOWNORMAL)
}

Expand Down
17 changes: 16 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package browser

import "strings"
import (
"bytes"
"fmt"
"os/exec"
"strings"
)

func ExampleOpenFile() {
OpenFile("index.html")
Expand All @@ -21,3 +26,13 @@ func ExampleOpenURL() {
const url = "http://golang.org/"
OpenURL(url)
}

func ExampleOpenURLWithOptions() {
out := &bytes.Buffer{}
const url = "http://golang.org/"
OpenURL(url, func(cmd *exec.Cmd) {
cmd.Stdout = out
cmd.Stderr = out
})
fmt.Printf("browser open output: %q\n", out.String())
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/pkg/browser

go 1.14

require golang.org/x/sys v0.0.0-20210415045647-66c3f260301c
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c h1:6L+uOeS3OQt/f4eFHXZcTxeZrGCuz+CLElgEBjbcTA4=
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=