aboutsummaryrefslogtreecommitdiffstats
path: root/browser.go
diff options
context:
space:
mode:
authorDave Cheney2014-07-10 17:18:52 +1000
committerDave Cheney2014-07-10 17:18:52 +1000
commitdc58c8f433c2ed2304866c4a44cd65f6c34456f6 (patch)
treea2d4e9546168c1c5c1d6cdb86a8a57bfc8b183ad /browser.go
parentf87def9a79c9ed6b95eaf5f74f83bc5e68397709 (diff)
downloadbrowser-dc58c8f433c2ed2304866c4a44cd65f6c34456f6.tar.bz2
initial import
Diffstat (limited to 'browser.go')
-rw-r--r--browser.go39
1 files changed, 39 insertions, 0 deletions
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)
+}