Commit 33ed3564 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 9a7ce41d
......@@ -7,15 +7,14 @@ package main
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
......@@ -74,7 +73,7 @@ func htmlOutput(profile, outfile string) error {
}
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())
}
}
......@@ -133,23 +132,6 @@ func htmlGen(w io.Writer, src []byte, boundaries []Boundary) error {
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 {
......
// 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
}
......@@ -12,10 +12,10 @@ import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"time"
"cmd/internal/browser"
"cmd/internal/pprof/plugin"
"cmd/internal/pprof/report"
"cmd/internal/pprof/svg"
......@@ -85,18 +85,9 @@ func PProf(c Completer, interactive **bool) Commands {
// 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
}
......
......@@ -20,6 +20,7 @@ package main
import (
"bufio"
"cmd/internal/browser"
"flag"
"fmt"
"html/template"
......@@ -28,8 +29,6 @@ import (
"net"
"net/http"
"os"
"os/exec"
"runtime"
"sync"
)
......@@ -96,7 +95,7 @@ func main() {
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())
}
......@@ -162,24 +161,6 @@ var templMain = template.Must(template.New("").Parse(`
</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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment