From 896ac677b5e3e80278cc1ce179d8a077ac3a6d2f Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 14 Sep 2016 14:51:41 -0700 Subject: [PATCH] cmd/go: make bug subcommand open the browser Instead of dumping information for the use to copy/paste into the issue tracker, open the issue tracker directly with a pre-filled template. Change-Id: I370d0063b609200497014ccda35244fa4314a662 Reviewed-on: https://go-review.googlesource.com/29210 Run-TryBot: Josh Bleecher Snyder Reviewed-by: Robert Griesemer Reviewed-by: Minux Ma Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/cmd/go/bootstrap.go | 3 ++ src/cmd/go/bug.go | 69 ++++++++++++++++++++++++++++------------- src/cmd/go/http.go | 4 +++ 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/src/cmd/go/bootstrap.go b/src/cmd/go/bootstrap.go index caa96769d8..2148d12685 100644 --- a/src/cmd/go/bootstrap.go +++ b/src/cmd/go/bootstrap.go @@ -36,3 +36,6 @@ func httpsOrHTTP(importPath string, security securityMode) (string, io.ReadClose func parseMetaGoImports(r io.Reader) ([]metaImport, error) { panic("unreachable") } + +func queryEscape(s string) string { panic("unreachable") } +func openBrowser(url string) bool { panic("unreachable") } diff --git a/src/cmd/go/bug.go b/src/cmd/go/bug.go index 7cf39ecd84..b6d8e35b5a 100644 --- a/src/cmd/go/bug.go +++ b/src/cmd/go/bug.go @@ -7,6 +7,7 @@ package main import ( "bytes" "fmt" + "io" "io/ioutil" "os/exec" "runtime" @@ -29,31 +30,57 @@ func init() { } func runBug(cmd *Command, args []string) { - inspectGoVersion() - fmt.Println("```") - fmt.Printf("go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH) + var buf bytes.Buffer + buf.WriteString(bugHeader) + inspectGoVersion(&buf) + fmt.Fprint(&buf, "#### System details\n\n") + fmt.Fprintln(&buf, "```") + fmt.Fprintf(&buf, "go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH) for _, e := range mkEnv() { - fmt.Printf("%s=\"%s\"\n", e.name, e.value) + fmt.Fprintf(&buf, "%s=\"%s\"\n", e.name, e.value) + } + printOSDetails(&buf) + printCDetails(&buf) + fmt.Fprintln(&buf, "```") + + body := buf.String() + url := "https://github.com/golang/go/issues/new?body=" + queryEscape(body) + if !openBrowser(url) { + fmt.Print("Please file a new issue at golang.org/issue/new using this template:\n\n") + fmt.Print(body) } - printOSDetails() - printCDetails() - fmt.Println("```") } -func printOSDetails() { +const bugHeader = `Please answer these questions before submitting your issue. Thanks! + +#### What did you do? +If possible, provide a recipe for reproducing the error. +A complete runnable program is good. +A link on play.golang.org is best. + + +#### What did you expect to see? + + +#### What did you see instead? + + +` + +func printOSDetails(w io.Writer) { switch runtime.GOOS { case "darwin": - printCmdOut("uname -v: ", "uname", "-v") - printCmdOut("", "sw_vers") + printCmdOut(w, "uname -v: ", "uname", "-v") + printCmdOut(w, "", "sw_vers") case "linux": - printCmdOut("uname -sr: ", "uname", "-sr") - printCmdOut("libc:", "/lib/libc.so.6") + printCmdOut(w, "uname -sr: ", "uname", "-sr") + printCmdOut(w, "libc:", "/lib/libc.so.6") case "openbsd", "netbsd", "freebsd", "dragonfly": - printCmdOut("uname -v: ", "uname", "-v") + printCmdOut(w, "uname -v: ", "uname", "-v") case "solaris": out, err := ioutil.ReadFile("/etc/release") if err == nil { - fmt.Printf("/etc/release: %s\n", out) + fmt.Fprintf(w, "/etc/release: %s\n", out) } else { if buildV { fmt.Printf("failed to read /etc/release: %v\n", err) @@ -62,8 +89,8 @@ func printOSDetails() { } } -func printCDetails() { - printCmdOut("lldb --version: ", "lldb", "--version") +func printCDetails(w io.Writer) { + printCmdOut(w, "lldb --version: ", "lldb", "--version") cmd := exec.Command("gdb", "--version") out, err := cmd.Output() if err == nil { @@ -73,7 +100,7 @@ func printCDetails() { idx := bytes.Index(out, []byte{'\n'}) line := out[:idx] line = bytes.TrimSpace(line) - fmt.Printf("gdb --version: %s\n", line) + fmt.Fprintf(w, "gdb --version: %s\n", line) } else { if buildV { fmt.Printf("failed to run gdb --version: %v\n", err) @@ -81,7 +108,7 @@ func printCDetails() { } } -func inspectGoVersion() { +func inspectGoVersion(w io.Writer) { data, err := httpGET("https://golang.org/VERSION?m=text") if err != nil { if buildV { @@ -102,12 +129,12 @@ func inspectGoVersion() { } // Devel version or outdated release. Either way, this request is apropos. - fmt.Printf("Please check whether the issue also reproduces on the latest release, %s.\n\n", release) + fmt.Fprintf(w, "#### Does this issue reproduce with the latest release (%s)?\n\n\n", release) } // printCmdOut prints the output of running the given command. // It ignores failures; 'go bug' is best effort. -func printCmdOut(prefix, path string, args ...string) { +func printCmdOut(w io.Writer, prefix, path string, args ...string) { cmd := exec.Command(path, args...) out, err := cmd.Output() if err != nil { @@ -116,5 +143,5 @@ func printCmdOut(prefix, path string, args ...string) { } return } - fmt.Printf("%s%s\n", prefix, bytes.TrimSpace(out)) + fmt.Fprintf(w, "%s%s\n", prefix, bytes.TrimSpace(out)) } diff --git a/src/cmd/go/http.go b/src/cmd/go/http.go index 05ea503049..1dc2c12c64 100644 --- a/src/cmd/go/http.go +++ b/src/cmd/go/http.go @@ -12,6 +12,7 @@ package main import ( + "cmd/internal/browser" "crypto/tls" "fmt" "io" @@ -113,3 +114,6 @@ func httpsOrHTTP(importPath string, security securityMode) (urlStr string, body } return urlStr, res.Body, nil } + +func queryEscape(s string) string { return url.QueryEscape(s) } +func openBrowser(url string) bool { return browser.Open(url) } -- 2.48.1