summaryrefslogtreecommitdiff
path: root/src/cmd/internal/browser
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2016-09-14 11:16:50 -0700
committerJosh Bleecher Snyder <josharian@gmail.com>2016-09-14 18:26:33 +0000
commit33ed35647520f2162c2fed1b0e5f19cec2c65de3 (patch)
tree4c45aaf3090c3e7cfd8645aa0510b0d2cd52da0f /src/cmd/internal/browser
parent9a7ce41d6c74ef30af361677d2077ad8dd0e92b7 (diff)
downloadgo-git-33ed35647520f2162c2fed1b0e5f19cec2c65de3.tar.gz
cmd: add internal/browser package
cmd/cover, cmd/trace, and cmd/pprof all open browsers. 'go bug' will soon also open a browser. It is time to unify the browser-handling code. Change-Id: Iee6b443e21e938aeaaac366a1aefb1afbc7d9b2c Reviewed-on: https://go-review.googlesource.com/29160 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/cmd/internal/browser')
-rw-r--r--src/cmd/internal/browser/browser.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/cmd/internal/browser/browser.go b/src/cmd/internal/browser/browser.go
new file mode 100644
index 0000000000..11e65c2feb
--- /dev/null
+++ b/src/cmd/internal/browser/browser.go
@@ -0,0 +1,41 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package browser provides utilities for interacting with users' browsers.
+package browser
+
+import (
+ "os"
+ "os/exec"
+ "runtime"
+)
+
+// Commands returns a list of possible commands to use to open a url.
+func Commands() [][]string {
+ var cmds [][]string
+ if exe := os.Getenv("BROWSER"); exe != "" {
+ cmds = append(cmds, []string{exe})
+ }
+ switch runtime.GOOS {
+ case "darwin":
+ cmds = append(cmds, []string{"/usr/bin/open"})
+ case "windows":
+ cmds = append(cmds, []string{"cmd", "/c", "start"})
+ default:
+ cmds = append(cmds, []string{"xdg-open"})
+ }
+ cmds = append(cmds, []string{"chrome"}, []string{"google-chrome"}, []string{"firefox"})
+ return cmds
+}
+
+// Open tries to open url in a browser and reports whether it succeeded.
+func Open(url string) bool {
+ for _, args := range Commands() {
+ cmd := exec.Command(args[0], append(args[1:], url)...)
+ if cmd.Start() == nil {
+ return true
+ }
+ }
+ return false
+}