aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Cheney2014-07-10 17:18:52 +1000
committerDave Cheney2014-07-10 17:18:52 +1000
commitdc58c8f433c2ed2304866c4a44cd65f6c34456f6 (patch)
treea2d4e9546168c1c5c1d6cdb86a8a57bfc8b183ad
parentf87def9a79c9ed6b95eaf5f74f83bc5e68397709 (diff)
downloadbrowser-dc58c8f433c2ed2304866c4a44cd65f6c34456f6.tar.bz2
initial import
-rw-r--r--README.md44
-rw-r--r--browser.go39
-rw-r--r--browser_darwin.go10
-rw-r--r--browser_linux.go10
-rw-r--r--browser_unsupported.go12
-rw-r--r--browser_windows.go10
-rw-r--r--example_test.go23
-rw-r--r--examples/Open/main.go38
8 files changed, 184 insertions, 2 deletions
diff --git a/README.md b/README.md
index e4d98e3..69c00ca 100644
--- a/README.md
+++ b/README.md
@@ -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()
+ }
+}