]> Cypherpunks repositories - gostls13.git/commitdiff
cmd: add internal/browser package
authorJosh Bleecher Snyder <josharian@gmail.com>
Wed, 14 Sep 2016 18:16:50 +0000 (11:16 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Wed, 14 Sep 2016 18:26:33 +0000 (18:26 +0000)
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>
src/cmd/cover/html.go
src/cmd/internal/browser/browser.go [new file with mode: 0644]
src/cmd/internal/pprof/commands/commands.go
src/cmd/trace/main.go

index d0ac4476ba64b4eddb2e632c4c86e1dc49776480..b49f934d1b8cc9fc863b4429675c9bb95fa75ccb 100644 (file)
@@ -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 {
diff --git a/src/cmd/internal/browser/browser.go b/src/cmd/internal/browser/browser.go
new file mode 100644 (file)
index 0000000..11e65c2
--- /dev/null
@@ -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
+}
index 5dfbbd4a5dc8acc75e9bf7cacc5541b88f09f952..4a4fb927ef012e01f6c7c7e3cf269cc44df644fa 100644 (file)
@@ -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
 }
 
index 893719edbf8fdc3cabc98350a686416d6a544cb0..1b84d838f036847918d834c5e71b3879410fad75 100644 (file)
@@ -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)