From 0b17e0e5d9c46c81c434cf7d6d580bf21c328627 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Wed, 15 Dec 2021 22:43:58 +0800 Subject: [PATCH] Feat goctl bug (#1332) * Support goctl bug * fix typo * format code Co-authored-by: anqiansong --- tools/goctl/bug/bug.go | 49 ++++++++++++++++++++++++++++++++++++++++ tools/goctl/bug/env.go | 32 ++++++++++++++++++++++++++ tools/goctl/bug/issue.go | 39 ++++++++++++++++++++++++++++++++ tools/goctl/goctl.go | 6 +++++ 4 files changed, 126 insertions(+) create mode 100644 tools/goctl/bug/bug.go create mode 100644 tools/goctl/bug/env.go create mode 100644 tools/goctl/bug/issue.go diff --git a/tools/goctl/bug/bug.go b/tools/goctl/bug/bug.go new file mode 100644 index 00000000..65d0eaa2 --- /dev/null +++ b/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, "
\n"+env.string()+"
") + 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() +} diff --git a/tools/goctl/bug/env.go b/tools/goctl/bug/env.go new file mode 100644 index 00000000..4ca90c7f --- /dev/null +++ b/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 +} diff --git a/tools/goctl/bug/issue.go b/tools/goctl/bug/issue.go new file mode 100644 index 00000000..1757e0f6 --- /dev/null +++ b/tools/goctl/bug/issue.go @@ -0,0 +1,39 @@ +package bug + +const issueTemplate=` + + +**Describe the bug** + + +**To Reproduce** + + +1. The code is + +
+	
+	
+ +2. The error is + +
+	
+	
+ +**Expected behavior** + + +**Screenshots** + + +**Environments (please complete the following information):** + +%s + +**More description** + + +` diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 85d8faca..9f4dda49 100644 --- a/tools/goctl/goctl.go +++ b/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",