Skip to content

Commit

Permalink
feat: implement gno bug (#1325)
Browse files Browse the repository at this point in the history
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
3 people authored Feb 29, 2024
1 parent 75539ea commit 9b37107
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/BUG-REPORT.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
name: Bug Report Template
about: Create a bug report
# NOTE: keep in sync with gnovm/cmd/gno/bug.go
---

## [Subject of the issue]
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/go-gno-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ Legend:

| go command | gno command | comment |
|-------------------|------------------|-----------------------------------------------------------------------|
| go bug | | see https://github.com/gnolang/gno/issues/733 |
| go bug | gno bug | same behavior |
| go build | gno build | same intention, limited compatibility |
| go clean | gno clean | same intention, limited compatibility |
| go doc | gno doc | limited compatibility; see https://github.com/gnolang/gno/issues/522 |
Expand Down
165 changes: 165 additions & 0 deletions gnovm/cmd/gno/bug.go
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
}
}
21 changes: 21 additions & 0 deletions gnovm/cmd/gno/bug_test.go
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)
}
4 changes: 2 additions & 2 deletions gnovm/cmd/gno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func newGnocliCmd(io commands.IO) *commands.Command {
newReplCmd(),
newDocCmd(io),
newEnvCmd(io),
newBugCmd(io),
// fmt -- gofmt
// graph
// vendor -- download deps from the chain in vendor/
Expand All @@ -41,8 +42,7 @@ func newGnocliCmd(io commands.IO) *commands.Command {
// publish/release
// generate
// "vm" -- starts an in-memory chain that can be interacted with?
// bug -- start a bug report
// version -- show gno, golang versions
// version -- show cmd/gno, golang versions
)

return cmd
Expand Down

0 comments on commit 9b37107

Please sign in to comment.