Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement gno bug #1325

Merged
merged 21 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions gnovm/cmd/gno/bug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"context"
"flag"
"net/url"
"os/exec"
"runtime"
"strings"
"text/template"

"github.com/gnolang/gno/tm2/pkg/commands"
)

const bugTmpl = `## [Subject of the issue]
harry-hov marked this conversation as resolved.
Show resolved Hide resolved

### Description

Describe your issue in as much detail as possible here

### Your environment

* go version {{.GoVersion}} {{.Os}}/{{.Arch}}
* version of gno
thehowl marked this conversation as resolved.
Show resolved Hide resolved
* branch that causes this issue (with the commit hash)

### 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
}

Check warning on line 82 in gnovm/cmd/gno/bug.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/bug.go#L81-L82

Added lines #L81 - L82 were not covered by tests

bugReportEnv := struct { // TODO: include gno version or commit?
Os, Arch, GoVersion string
}{
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
}

var buf strings.Builder
tmpl, err := template.New("bug.tmpl").Parse(bugTmpl)
if err != nil {
return err
}

Check warning on line 96 in gnovm/cmd/gno/bug.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/bug.go#L95-L96

Added lines #L95 - L96 were not covered by tests
tmpl.Execute(&buf, bugReportEnv)

body := buf.String()
url := "https://github.com/gnolang/gno/issues/new?body=" + url.QueryEscape(body)
harry-hov marked this conversation as resolved.
Show resolved Hide resolved

if !cfg.skipBrowser {
// Try opening browser (ignore error)
_ = openBrowser(url)
thehowl marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 105 in gnovm/cmd/gno/bug.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/bug.go#L103-L105

Added lines #L103 - L105 were not covered by tests

// Print on console, regardless if the browser opened or not
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) error {
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}

Check warning on line 124 in gnovm/cmd/gno/bug.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/bug.go#L116-L124

Added lines #L116 - L124 were not covered by tests
}

cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
return cmd.Start()

Check warning on line 128 in gnovm/cmd/gno/bug.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/bug.go#L127-L128

Added lines #L127 - L128 were not covered by tests
}
thehowl marked this conversation as resolved.
Show resolved Hide resolved
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
harry-hov marked this conversation as resolved.
Show resolved Hide resolved

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.",
},
}
harry-hov marked this conversation as resolved.
Show resolved Hide resolved
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 gnodev, golang versions
harry-hov marked this conversation as resolved.
Show resolved Hide resolved
)

return cmd
Expand Down
Loading