forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbug.go
181 lines (144 loc) · 3.99 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"flag"
"net/url"
"os/exec"
"runtime"
"runtime/debug"
"strings"
"text/template"
"time"
"github.com/gnolang/gno/gnovm/pkg/version"
"github.com/gnolang/gno/tm2/pkg/commands"
)
// NOTE: keep in sync with .github/ISSUE_TEMPLATE/BUG-REPORT.md
const bugTmpl = `## [Subject of the issue]
### Description
Describe your issue in as much detail as possible here
### Your environment
* Gno version: {{ .GnoVersion }}
* Go version: {{ .GoVersion }}
* OS and CPU architecture: {{ .Os }}/{{ .Arch }}
* Gno commit hash causing the issue: {{ .Commit }}
### Steps to reproduce
* Tell us how to reproduce this issue
* Where the issue is, if you know
* Which commands triggered the issue, if any
### Expected behaviour
Tell us what should happen
### Actual behaviour
Tell us what happens instead
### Logs
Please paste any logs here that demonstrate the issue, if they exist
### Proposed solution
If you have an idea of how to fix this issue, please write it down here, so we can begin discussing it
`
type bugCfg struct {
skipBrowser bool
}
func newBugCmd(io commands.IO) *commands.Command {
cfg := &bugCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "bug",
ShortUsage: "bug",
ShortHelp: "start a bug report",
LongHelp: `opens https://github.com/gnolang/gno/issues in a browser.
The new issue body is prefilled for you with the following information:
- Gno version (the output of "gno version")
- Go version (example: go1.22.4)
- OS and CPU architecture (example: linux/amd64)
- Gno commit hash causing the issue (example: f24690e7ebf325bffcfaf9e328c3df8e6b21e50c)
The rest of the report consists of markdown sections such as ### Steps to reproduce
that you can edit.
`,
},
cfg,
func(_ context.Context, args []string) error {
return execBug(cfg, args, io)
},
)
}
func (c *bugCfg) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(
&c.skipBrowser,
"skip-browser",
false,
"output a prefilled issue template on the cli instead",
)
}
func execBug(cfg *bugCfg, args []string, io commands.IO) error {
if len(args) != 0 {
return flag.ErrHelp
}
bugReportEnv := struct {
Os, Arch, GnoVersion, GoVersion, Commit string
}{
runtime.GOOS,
runtime.GOARCH,
version.Version,
runtime.Version(),
getCommitHash(),
}
var buf strings.Builder
tmpl, err := template.New("bug.tmpl").Parse(bugTmpl)
if err != nil {
return err
}
tmpl.Execute(&buf, bugReportEnv)
body := buf.String()
url := "https://github.com/gnolang/gno/issues/new?body=" + url.QueryEscape(body)
if !cfg.skipBrowser && openBrowser(url) {
return nil
}
io.Println("Please file a new issue at github.com/gnolang/gno/issues/new using this template:")
io.Println()
io.Println(body)
return nil
}
// openBrowser opens a default web browser with the specified URL.
func openBrowser(url string) bool {
var cmdArgs []string
switch runtime.GOOS {
case "windows":
cmdArgs = []string{"cmd", "/c", "start", url}
case "darwin":
cmdArgs = []string{"/usr/bin/open", url}
default: // "linux"
cmdArgs = []string{"xdg-open", url}
}
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) {
return true
}
return false
}
// getCommitHash returns the commit hash from build info, or an
// empty string if not found.
func getCommitHash() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return ""
}
// appearsSuccessful reports whether the command appears to have run successfully.
// If the command runs longer than the timeout, it's deemed successful.
// If the command runs within the timeout, it's deemed successful if it exited cleanly.
// Note: Taken from Go's `internal/browser“
func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool {
errc := make(chan error, 1)
go func() {
errc <- cmd.Wait()
}()
select {
case <-time.After(timeout):
return true
case err := <-errc:
return err == nil
}
}