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>
import (
"bufio"
"bytes"
+ "cmd/internal/browser"
"fmt"
"html/template"
"io"
"io/ioutil"
"math"
"os"
- "os/exec"
"path/filepath"
- "runtime"
)
// htmlOutput reads the profile data from profile and generates an HTML
}
if outfile == "" {
- if !startBrowser("file://" + out.Name()) {
+ if !browser.Open("file://" + out.Name()) {
fmt.Fprintf(os.Stderr, "HTML output written to %s\n", out.Name())
}
}
return dst.Flush()
}
-// startBrowser tries to open the URL in a browser
-// and reports whether it succeeds.
-func startBrowser(url string) bool {
- // try to start the browser
- var args []string
- switch runtime.GOOS {
- case "darwin":
- args = []string{"open"}
- case "windows":
- args = []string{"cmd", "/c", "start"}
- default:
- args = []string{"xdg-open"}
- }
- cmd := exec.Command(args[0], append(args[1:], url)...)
- return cmd.Start() == nil
-}
-
// rgb returns an rgb value for the specified coverage value
// between 0 (no coverage) and 10 (max coverage).
func rgb(n int) string {
--- /dev/null
+// 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
+}
"io/ioutil"
"os"
"os/exec"
- "runtime"
"strings"
"time"
+ "cmd/internal/browser"
"cmd/internal/pprof/plugin"
"cmd/internal/pprof/report"
"cmd/internal/pprof/svg"
// on the current platform
func browsers() []string {
var cmds []string
- if exe := os.Getenv("BROWSER"); exe != "" {
- cmds = append(cmds, exe)
+ for _, cmd := range browser.Commands() {
+ cmds = append(cmds, strings.Join(cmd, " "))
}
- switch runtime.GOOS {
- case "darwin":
- cmds = append(cmds, "/usr/bin/open")
- case "windows":
- cmds = append(cmds, "cmd /c start")
- default:
- cmds = append(cmds, "xdg-open")
- }
- cmds = append(cmds, "chrome", "google-chrome", "firefox")
return cmds
}
import (
"bufio"
+ "cmd/internal/browser"
"flag"
"fmt"
"html/template"
"net"
"net/http"
"os"
- "os/exec"
- "runtime"
"sync"
)
ranges = splitTrace(data)
log.Printf("Opening browser")
- if !startBrowser("http://" + ln.Addr().String()) {
+ if !browser.Open("http://" + ln.Addr().String()) {
fmt.Fprintf(os.Stderr, "Trace viewer is listening on http://%s\n", ln.Addr().String())
}
</html>
`))
-// startBrowser tries to open the URL in a browser
-// and reports whether it succeeds.
-// Note: copied from x/tools/cmd/cover/html.go
-func startBrowser(url string) bool {
- // try to start the browser
- var args []string
- switch runtime.GOOS {
- case "darwin":
- args = []string{"open"}
- case "windows":
- args = []string{"cmd", "/c", "start"}
- default:
- args = []string{"xdg-open"}
- }
- cmd := exec.Command(args[0], append(args[1:], url)...)
- return cmd.Start() == nil
-}
-
func dief(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)