diff options
| -rw-r--r-- | README.md | 44 | ||||
| -rw-r--r-- | browser.go | 39 | ||||
| -rw-r--r-- | browser_darwin.go | 10 | ||||
| -rw-r--r-- | browser_linux.go | 10 | ||||
| -rw-r--r-- | browser_unsupported.go | 12 | ||||
| -rw-r--r-- | browser_windows.go | 10 | ||||
| -rw-r--r-- | example_test.go | 23 | ||||
| -rw-r--r-- | examples/Open/main.go | 38 | 
8 files changed, 184 insertions, 2 deletions
| @@ -1,4 +1,44 @@ -browser -======= + +# browser +    import "github.com/pkg/browser"  Package browser provides helpers to open files, readers, and urls in a browser window. + +The choice of which browser is started is entirely client dependant. + + + + + + +## func OpenFile +``` go +func OpenFile(path string) error +``` +OpenFile opens new browser window for the file path. + + +## func OpenReader +``` go +func OpenReader(r io.Reader) error +``` +OpenReader consumes the contents of r and presents the +results in a new browser window. + + +## func OpenURL +``` go +func OpenURL(url string) error +``` +OpenURL opens a new browser window pointing to url. + + + + + + + + + +- - - +Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)
\ No newline at end of file diff --git a/browser.go b/browser.go new file mode 100644 index 0000000..4b922ee --- /dev/null +++ b/browser.go @@ -0,0 +1,39 @@ +// Package browser provides helpers to open files, readers, and urls in a browser window. +// +// The choice of which browser is started is entirely client dependant. +package browser + +import ( +	"fmt" +	"io" +	"io/ioutil" +) + +// OpenFile opens new browser window for the file path. +func OpenFile(path string) error { +	return openBrowser(path) +} + +// OpenReader consumes the contents of r and presents the +// results in a new browser window. +func OpenReader(r io.Reader) error { +	f, err := ioutil.TempFile("", "browser") +	if err != nil { +		return fmt.Errorf("browser: could not create temporary file: %v", err) +	} +	if _, err := io.Copy(f, r); err != nil { +		f.Close() +		return fmt.Errorf("browser: caching temporary file failed: %v", err) +	} +	if err := f.Close(); err != nil { +		return fmt.Errorf("browser: caching temporary file failed: %v", err) +	} + +	//defer os.Remove(f.Name()) +	return openBrowser(f.Name()) +} + +// OpenURL opens a new browser window pointing to url. +func OpenURL(url string) error { +	return openBrowser(url) +} diff --git a/browser_darwin.go b/browser_darwin.go new file mode 100644 index 0000000..6b1fe85 --- /dev/null +++ b/browser_darwin.go @@ -0,0 +1,10 @@ +package browser + +import ( +	"os/exec" +) + +func openBrowser(url string) error { +	cmd := exec.Command("open", url) +	return cmd.Run() +} diff --git a/browser_linux.go b/browser_linux.go new file mode 100644 index 0000000..e9e9b8c --- /dev/null +++ b/browser_linux.go @@ -0,0 +1,10 @@ +package browser + +import ( +	"os/exec" +) + +func openBrowser(url string) error { +	cmd := exec.Command("xdg-open", url) +	return cmd.Run() +} diff --git a/browser_unsupported.go b/browser_unsupported.go new file mode 100644 index 0000000..3bdc6ff --- /dev/null +++ b/browser_unsupported.go @@ -0,0 +1,12 @@ +// +build !linux,!windows,!darwin + +package browser + +import ( +	"fmt" +	"runtime" +) + +func openBrowser(url string) error { +	return fmt.Errorf("openBrowser: unsupported operating system: %v", runtime.GOOS) +} diff --git a/browser_windows.go b/browser_windows.go new file mode 100644 index 0000000..0f304b5 --- /dev/null +++ b/browser_windows.go @@ -0,0 +1,10 @@ +package browser + +import ( +	"os/exec" +) + +func openBrowser(url string) error { +	cmd := exec.Command("cmd", "/c", "start", url) +	return cmd.Run() +} diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..0c1fd3f --- /dev/null +++ b/example_test.go @@ -0,0 +1,23 @@ +package browser + +import "strings" + +func ExampleOpenFile() { +	OpenFile("index.html") +} + +func ExampleOpenReader() { +	// https://github.com/rust-lang/rust/issues/13871 +	const quote = `There was a night when winds from unknown spaces whirled us irresistibly into +limitless vacum beyond all thought and entity. Perceptions of the most +maddeningly untransmissible sort thronged upon us; perceptions of infinity +which at the time convulsed us with joy, yet which are now partly lost to my +memory and partly incapable of presentation to others.` +	r := strings.NewReader(quote) +	OpenReader(r) +} + +func ExampleOpenURL() { +	const url = "http://golang.org/" +	OpenURL(url) +} diff --git a/examples/Open/main.go b/examples/Open/main.go new file mode 100644 index 0000000..14c714f --- /dev/null +++ b/examples/Open/main.go @@ -0,0 +1,38 @@ +package main + +import ( +	"flag" +	"fmt" +	"log" +	"os" + +	"github.com/pkg/browser" +) + +func usage() { +	fmt.Fprintf(os.Stderr, "Usage:\n  %s [file]\n", os.Args[0]) +	flag.PrintDefaults() +} + +func init() { +	flag.Usage = usage +	flag.Parse() +} + +func check(err error) { +	if err != nil { +		log.Fatal(err) +	} +} + +func main() { +	args := flag.Args() +	switch len(args) { +	case 0: +		check(browser.OpenReader(os.Stdin)) +	case 1: +		check(browser.OpenFile(args[0])) +	default: +		usage() +	} +} | 
