Bläddra i källkod

Feat goctl bug (#1332)

* Support goctl bug

* fix typo

* format code

Co-authored-by: anqiansong <anqiansong@bytedance.com>
anqiansong 3 år sedan
förälder
incheckning
0b17e0e5d9
4 ändrade filer med 126 tillägg och 0 borttagningar
  1. 49 0
      tools/goctl/bug/bug.go
  2. 32 0
      tools/goctl/bug/env.go
  3. 39 0
      tools/goctl/bug/issue.go
  4. 6 0
      tools/goctl/goctl.go

+ 49 - 0
tools/goctl/bug/bug.go

@@ -0,0 +1,49 @@
+package bug
+
+import (
+	"fmt"
+	"net/url"
+	"os/exec"
+	"runtime"
+
+	"github.com/urfave/cli"
+)
+
+const (
+	windows = "windows"
+	darwin  = "darwin"
+
+	windowsOpen = "start"
+	darwinOpen  = "open"
+	linuxOpen   = "xdg-open"
+
+	os           = "OS"
+	arch         = "ARCH"
+	goctlVersion = "GOCTL_VERSION"
+)
+
+var openCmd = map[string]string{
+	windows: windowsOpen,
+	darwin:  darwinOpen,
+}
+
+func Action(_ *cli.Context) error {
+	env := getEnv()
+	content := fmt.Sprintf(issueTemplate, "<pre>\n"+env.string()+"</pre>")
+	content = url.QueryEscape(content)
+	url := fmt.Sprintf("https://github.com/zeromicro/go-zero/issues/new?title=TODO&body=%s", content)
+
+	goos := runtime.GOOS
+	var cmd string
+	var args []string
+	cmd, ok := openCmd[goos]
+	if !ok {
+		cmd = linuxOpen
+	}
+	if goos == windows {
+		args = []string{"/c", "start"}
+	}
+
+	args = append(args, url)
+	return exec.Command(cmd, args...).Start()
+}

+ 32 - 0
tools/goctl/bug/env.go

@@ -0,0 +1,32 @@
+package bug
+
+import (
+	"bytes"
+	"fmt"
+	"runtime"
+
+	"github.com/tal-tech/go-zero/tools/goctl/internal/version"
+)
+
+type env map[string]string
+
+func (e env) string() string {
+	if e == nil {
+		return ""
+	}
+
+	w := bytes.NewBuffer(nil)
+	for k, v := range e {
+		w.WriteString(fmt.Sprintf("%s = %q\n", k, v))
+	}
+
+	return w.String()
+}
+
+func getEnv() env {
+	e := make(env)
+	e[os] = runtime.GOOS
+	e[arch] = runtime.GOARCH
+	e[goctlVersion] = version.BuildVersion
+	return e
+}

+ 39 - 0
tools/goctl/bug/issue.go

@@ -0,0 +1,39 @@
+package bug
+
+const issueTemplate=`
+<!-- Please submit an issue order by the following template. Thanks! -->
+
+**Describe the bug**
+<!-- A clear and concise description of what the bug is. -->
+
+**To Reproduce**
+<!-- Steps to reproduce the behavior, if applicable: -->
+
+1. The code is
+
+	<pre>
+	
+	</pre>
+
+2. The error is
+
+	<pre>
+	
+	</pre>
+
+**Expected behavior**
+<!-- A clear and concise description of what you expected to happen. -->
+
+**Screenshots**
+<!-- If applicable, add screenshots to help explain your problem. -->
+
+**Environments (please complete the following information):**
+<!--  - OS: [e.g. Linux]
+ - go-zero version [e.g. 1.2.1]
+ - goctl version [e.g. 1.2.1, optional] -->
+%s
+
+**More description**
+<!-- Add any other context about the problem here. -->
+
+`

+ 6 - 0
tools/goctl/goctl.go

@@ -18,6 +18,7 @@ import (
 	"github.com/tal-tech/go-zero/tools/goctl/api/new"
 	"github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
 	"github.com/tal-tech/go-zero/tools/goctl/api/validate"
+	"github.com/tal-tech/go-zero/tools/goctl/bug"
 	"github.com/tal-tech/go-zero/tools/goctl/docker"
 	"github.com/tal-tech/go-zero/tools/goctl/internal/errorx"
 	"github.com/tal-tech/go-zero/tools/goctl/internal/version"
@@ -34,6 +35,11 @@ import (
 const codeFailure = 1
 
 var commands = []cli.Command{
+	{
+		Name:   "bug",
+		Usage:  "report a bug",
+		Action: bug.Action,
+	},
 	{
 		Name:   "upgrade",
 		Usage:  "upgrade goctl to latest version",