/
bug.go
116 lines (97 loc) · 2.93 KB
/
bug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package cmd
import (
"bytes"
"context"
"fmt"
"net/url"
"os"
"os/exec"
"strings"
"github.com/fatih/color"
"github.com/sahib/brig/client"
"github.com/sahib/brig/version"
"github.com/toqueteos/webbrowser"
"github.com/urfave/cli"
)
const (
reportURL = "https://github.com/sahib/brig/issues/new?"
)
// printError simply prints a nicely formatted error to stderr.
func printError(msg string) {
fmt.Fprintln(os.Stderr, color.RedString("*** ")+msg)
}
// cmdOutput runs a command at `path` with `args` and returns it's output.
// No real error checking is done, on errors an empty string is returned.
func cmdOutput(path string, args ...string) string {
out, err := exec.Command(path, args...).Output()
if err != nil {
// No other error checking here, `brig bug` is best effort.
printError(fmt.Sprintf("failed to run %s %s", path, strings.Join(args, " ")))
return ""
}
return strings.TrimSpace(string(out))
}
// handleBugReport compiles a report of useful info when providing a bug report.
func handleBugReport(ctx *cli.Context) error {
buf := &bytes.Buffer{}
fmt.Fprintln(buf, `Please answer these questions before submitting your issue.
Please include anything else you think is helpful. Thanks!
### What did you do?
### What did you expect to see?
### What did you see instead?
### Do you still see this issue with a development binary?
### Did you check if a similar bug report was already opened?
### System details:`)
fmt.Fprintf(buf, "go version: ``%s``\n", cmdOutput("go", "version"))
fmt.Fprintf(buf, "uname -s -v -m: ``%s``\n", cmdOutput("uname", "-s", "-v", "-m"))
fmt.Fprintf(buf, "\n")
fmt.Fprintf(
buf,
"brig client version: ``%s [build: %s]``\n",
version.String(),
version.BuildTime,
)
port := guessPort(ctx)
ctl, err := client.Dial(context.Background(), port)
if err == nil {
// Try to get the server side / ipfs version.
version, err := ctl.Version()
if err == nil {
fmt.Fprintf(
buf,
"brig server version: ``%s+%s``\n",
version.ServerSemVer,
version.ServerRev,
)
fmt.Fprintf(
buf,
"ipfs version: ``%s+%s``\n",
version.BackendSemVer,
version.BackendRev,
)
}
} else {
printError("Cannot get server and ipfs version.")
printError("If it is possible to start the daemon, do it now.")
printError("This will make the bug report more helpful. Thanks.")
}
printToStdout := ctx.Bool("stdout")
if !printToStdout {
// Try to open the issue tracker for convinience:
urlVal := url.Values{}
urlVal.Set("body", buf.String())
if err := webbrowser.Open(reportURL + urlVal.Encode()); err != nil {
printToStdout = true
}
}
if printToStdout {
// If not, ask the user to print it directly:
if !ctx.Bool("stdout") {
printError("I failed to open the issue tracker in your browser.")
printError("Please paste the underlying text manually at this URL:")
printError("https://github.com/sahib/brig/issues")
}
fmt.Println(buf.String())
}
return nil
}