From d3bfa16813fda9f30fd30c8b7f89ba2f6a9c2b20 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 9 Nov 2021 22:42:44 +0800 Subject: [PATCH] feat: exit with non-zero code on errors (#1218) * feat: exit with non-zero code on errors * chore: use const for code --- tools/goctl/goctl.go | 53 +++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 191b4332..9f528f35 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -9,7 +9,6 @@ import ( "github.com/logrusorgru/aurora" "github.com/tal-tech/go-zero/core/load" "github.com/tal-tech/go-zero/core/logx" - "github.com/tal-tech/go-zero/core/stat" "github.com/tal-tech/go-zero/tools/goctl/api/apigen" "github.com/tal-tech/go-zero/tools/goctl/api/dartgen" "github.com/tal-tech/go-zero/tools/goctl/api/docgen" @@ -36,6 +35,11 @@ import ( pluginCtl "github.com/zeromicro/protobuf/protoc-gen-go" ) +const ( + protocGenGoctl = "protoc-gen-goctl" + codeFailure = 1 +) + var commands = []cli.Command{ { Name: "upgrade", @@ -646,28 +650,6 @@ var commands = []cli.Command{ }, } -func main() { - logx.Disable() - load.Disable() - stat.DisableLog() - - args := os.Args - pluginName := filepath.Base(args[0]) - if pluginName == protocGenGoctl { - pluginCtl.Generate() - return - } - - app := cli.NewApp() - app.Usage = "a cli tool to generate code" - app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH) - app.Commands = commands - // cli already print error messages - if err := app.Run(os.Args); err != nil { - fmt.Println(aurora.Red(errorx.Wrap(err).Error())) - } -} - func init() { err := linkProtocGenGoctl() if err != nil { @@ -675,8 +657,6 @@ func init() { } } -const protocGenGoctl = "protoc-gen-goctl" - func linkProtocGenGoctl() error { path, err := env.LookPath("goctl") if err != nil { @@ -698,3 +678,26 @@ func linkProtocGenGoctl() error { } return nil } + +func main() { + logx.Disable() + load.Disable() + + args := os.Args + pluginName := filepath.Base(args[0]) + if pluginName == protocGenGoctl { + pluginCtl.Generate() + return + } + + app := cli.NewApp() + app.Usage = "a cli tool to generate code" + app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH) + app.Commands = commands + + // cli already print error messages + if err := app.Run(os.Args); err != nil { + fmt.Println(aurora.Red(errorx.Wrap(err).Error())) + os.Exit(codeFailure) + } +}