-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contains initial implementation of `gno bug` command. ![gno-bug](https://github.com/gnolang/gno/assets/37576387/c1357f99-dcfd-4d80-8b54-cd1a0caba12b) Closes #733 <details><summary>Contributors' checklist...</summary> - [ ] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [x] Provided any useful hints for running manual tests - [x] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details> --------- Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com> Co-authored-by: Morgan <git@howl.moe>
- Loading branch information
1 parent
75539ea
commit 9b37107
Showing
5 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"net/url" | ||
"os/exec" | ||
"runtime" | ||
"runtime/debug" | ||
"strings" | ||
"text/template" | ||
"time" | ||
|
||
"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 | ||
* go version {{.GoVersion}} {{.Os}}/{{.Arch}} | ||
* gno commit that causes this 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", | ||
}, | ||
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, | ||
"do not open the browser", | ||
) | ||
} | ||
|
||
func execBug(cfg *bugCfg, args []string, io commands.IO) error { | ||
if len(args) != 0 { | ||
return flag.ErrHelp | ||
} | ||
|
||
bugReportEnv := struct { | ||
Os, Arch, GoVersion, Commit string | ||
}{ | ||
runtime.GOOS, | ||
runtime.GOARCH, | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestBugApp(t *testing.T) { | ||
tc := []testMainCase{ | ||
{ | ||
args: []string{"bug -h"}, | ||
errShouldBe: "flag: help requested", | ||
}, | ||
{ | ||
args: []string{"bug unknown"}, | ||
errShouldBe: "flag: help requested", | ||
}, | ||
{ | ||
args: []string{"bug", "-skip-browser"}, | ||
stdoutShouldContain: "go version go1.", | ||
}, | ||
} | ||
testMainCaseRun(t, tc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters