Просмотр исходного кода

Add verbose flag (#1696)

Co-authored-by: anqiansong <anqiansong@bytedance.com>
anqiansong 3 лет назад
Родитель
Сommit
0aeb49a6b0

+ 18 - 16
tools/goctl/env/check.go

@@ -40,21 +40,23 @@ var bins = []bin{
 func Check(ctx *cli.Context) error {
 	install := ctx.Bool("install")
 	force := ctx.Bool("force")
-	return Prepare(install, force)
+	verbose := ctx.Bool("verbose")
+	return Prepare(install, force, verbose)
 }
 
-func Prepare(install, force bool) error {
+func Prepare(install, force, verbose bool) error {
+	log := console.NewColorConsole(verbose)
 	pending := true
-	console.Info("[goctl-env]: preparing to check env")
+	log.Info("[goctl-env]: preparing to check env")
 	defer func() {
 		if p := recover(); p != nil {
-			console.Error("%+v", p)
+			log.Error("%+v", p)
 			return
 		}
 		if pending {
-			console.Success("\n[goctl-env]: congratulations! your goctl environment is ready!")
+			log.Success("\n[goctl-env]: congratulations! your goctl environment is ready!")
 		} else {
-			console.Error(`
+			log.Error(`
 [goctl-env]: check env finish, some dependencies is not found in PATH, you can execute
 command 'goctl env check --install' to install it, for details, please execute command 
 'goctl env check --help'`)
@@ -62,29 +64,29 @@ command 'goctl env check --install' to install it, for details, please execute c
 	}()
 	for _, e := range bins {
 		time.Sleep(200 * time.Millisecond)
-		console.Info("")
-		console.Info("[goctl-env]: looking up %q", e.name)
+		log.Info("")
+		log.Info("[goctl-env]: looking up %q", e.name)
 		if e.exists {
-			console.Success("[goctl-env]: %q is installed", e.name)
+			log.Success("[goctl-env]: %q is installed", e.name)
 			continue
 		}
-		console.Warning("[goctl-env]: %q is not found in PATH", e.name)
+		log.Warning("[goctl-env]: %q is not found in PATH", e.name)
 		if install {
 			install := func() {
-				console.Info("[goctl-env]: preparing to install %q", e.name)
+				log.Info("[goctl-env]: preparing to install %q", e.name)
 				path, err := e.get(env.Get(env.GoctlCache))
 				if err != nil {
-					console.Error("[goctl-env]: an error interrupted the installation: %+v", err)
+					log.Error("[goctl-env]: an error interrupted the installation: %+v", err)
 					pending = false
 				} else {
-					console.Success("[goctl-env]: %q is already installed in %q", e.name, path)
+					log.Success("[goctl-env]: %q is already installed in %q", e.name, path)
 				}
 			}
 			if force {
 				install()
 				continue
 			}
-			console.Info("[goctl-env]: do you want to install %q [y: YES, n: No]", e.name)
+			log.Info("[goctl-env]: do you want to install %q [y: YES, n: No]", e.name)
 			for {
 				var in string
 				fmt.Scanln(&in)
@@ -95,10 +97,10 @@ command 'goctl env check --install' to install it, for details, please execute c
 					brk = true
 				case strings.EqualFold(in, "n"):
 					pending = false
-					console.Info("[goctl-env]: %q installation is ignored", e.name)
+					log.Info("[goctl-env]: %q installation is ignored", e.name)
 					brk = true
 				default:
-					console.Error("[goctl-env]: invalid input, input 'y' for yes, 'n' for no")
+					log.Error("[goctl-env]: invalid input, input 'y' for yes, 'n' for no")
 				}
 				if brk {
 					break

+ 13 - 2
tools/goctl/goctl.go

@@ -23,7 +23,6 @@ import (
 	"github.com/zeromicro/go-zero/tools/goctl/completion"
 	"github.com/zeromicro/go-zero/tools/goctl/docker"
 	"github.com/zeromicro/go-zero/tools/goctl/env"
-	"github.com/zeromicro/go-zero/tools/goctl/internal/errorx"
 	"github.com/zeromicro/go-zero/tools/goctl/internal/version"
 	"github.com/zeromicro/go-zero/tools/goctl/kube"
 	"github.com/zeromicro/go-zero/tools/goctl/migrate"
@@ -70,6 +69,10 @@ var commands = []cli.Command{
 						Name:  "force, f",
 						Usage: "silent installation of non-existent dependencies",
 					},
+					cli.BoolFlag{
+						Name:  "verbose, v",
+						Usage: "enable log output",
+					},
 				},
 				Action: env.Check,
 			},
@@ -519,6 +522,10 @@ var commands = []cli.Command{
 						Name:  "branch",
 						Usage: "the branch of the remote repo, it does work with --remote",
 					},
+					cli.BoolFlag{
+						Name:  "verbose, v",
+						Usage: "enable log output",
+					},
 				},
 				Action: rpc.RPCNew,
 			},
@@ -601,6 +608,10 @@ var commands = []cli.Command{
 						Name:  "branch",
 						Usage: "the branch of the remote repo, it does work with --remote",
 					},
+					cli.BoolFlag{
+						Name:  "verbose, v",
+						Usage: "enable log output",
+					},
 				},
 			},
 		},
@@ -894,7 +905,7 @@ func main() {
 
 	// cli already print error messages.
 	if err := app.Run(os.Args); err != nil {
-		fmt.Println(aurora.Red(errorx.Wrap(err).Error()))
+		fmt.Println(aurora.Red(err.Error()))
 		os.Exit(codeFailure)
 	}
 }

+ 2 - 1
tools/goctl/rpc/cli/cli.go

@@ -23,6 +23,7 @@ func RPCNew(c *cli.Context) error {
 	home := c.String("home")
 	remote := c.String("remote")
 	branch := c.String("branch")
+	verbose := c.Bool("verbose")
 	if len(remote) > 0 {
 		repo, _ := util.CloneIntoGitHome(remote, branch)
 		if len(repo) > 0 {
@@ -52,7 +53,7 @@ func RPCNew(c *cli.Context) error {
 	ctx.IsGooglePlugin = true
 	ctx.Output = filepath.Dir(src)
 	ctx.ProtocCmd = fmt.Sprintf("protoc -I=%s %s --go_out=%s --go-grpc_out=%s", filepath.Dir(src), filepath.Base(src), filepath.Dir(src), filepath.Dir(src))
-	g := generator.NewGenerator(style)
+	g := generator.NewGenerator(style, verbose)
 	return g.Generate(&ctx)
 }
 

+ 5 - 2
tools/goctl/rpc/cli/zrpc.go

@@ -42,6 +42,7 @@ func ZRPC(c *cli.Context) error {
 	home := c.String("home")
 	remote := c.String("remote")
 	branch := c.String("branch")
+	verbose := c.Bool("verbose")
 	if len(grpcOutList) == 0 {
 		return errInvalidGrpcOutput
 	}
@@ -107,7 +108,7 @@ func ZRPC(c *cli.Context) error {
 	ctx.IsGooglePlugin = isGooglePlugin
 	ctx.Output = zrpcOut
 	ctx.ProtocCmd = strings.Join(protocArgs, " ")
-	g := generator.NewGenerator(style)
+	g := generator.NewGenerator(style, verbose)
 	return g.Generate(&ctx)
 }
 
@@ -117,11 +118,13 @@ func removeGoctlFlag(args []string) []string {
 	for step < len(args) {
 		arg := args[step]
 		switch {
-		case arg == "--style", arg == "--home", arg == "--zrpc_out":
+		case arg == "--style", arg == "--home", arg == "--zrpc_out", arg == "--verbose", arg == "-v":
 			step += 2
 			continue
 		case strings.HasPrefix(arg, "--style="),
 			strings.HasPrefix(arg, "--home="),
+			strings.HasPrefix(arg, "--verbose="),
+			strings.HasPrefix(arg, "-v="),
 			strings.HasPrefix(arg, "--zrpc_out="):
 			step += 1
 			continue

+ 9 - 7
tools/goctl/rpc/generator/generator.go

@@ -10,25 +10,27 @@ import (
 
 // Generator defines the environment needs of rpc service generation
 type Generator struct {
-	log console.Console
-	cfg *conf.Config
+	log     console.Console
+	cfg     *conf.Config
+	verbose bool
 }
 
 // NewGenerator returns an instance of Generator
-func NewGenerator(style string) *Generator {
+func NewGenerator(style string, verbose bool) *Generator {
 	cfg, err := conf.NewConfig(style)
 	if err != nil {
 		log.Fatalln(err)
 	}
-	log := console.NewColorConsole()
+	log := console.NewColorConsole(verbose)
 	return &Generator{
-		log: log,
-		cfg: cfg,
+		log:     log,
+		cfg:     cfg,
+		verbose: verbose,
 	}
 }
 
 // Prepare provides environment detection generated by rpc service,
 // including go environment, protoc, whether protoc-gen-go is installed or not
 func (g *Generator) Prepare() error {
-	return env.Prepare(true, true)
+	return env.Prepare(true, true, g.verbose)
 }

+ 35 - 3
tools/goctl/util/console/console.go

@@ -24,7 +24,9 @@ type (
 		Must(err error)
 	}
 
-	colorConsole struct{}
+	colorConsole struct {
+		enable bool
+	}
 
 	// for idea log
 	ideaConsole struct{}
@@ -39,45 +41,75 @@ func NewConsole(idea bool) Console {
 }
 
 // NewColorConsole returns an instance of colorConsole
-func NewColorConsole() Console {
-	return &colorConsole{}
+func NewColorConsole(enable ...bool) Console {
+	logEnable := true
+	for _, e := range enable {
+		logEnable = e
+	}
+	return &colorConsole{
+		enable: logEnable,
+	}
 }
 
 func (c *colorConsole) Info(format string, a ...interface{}) {
+	if !c.enable {
+		return
+	}
 	msg := fmt.Sprintf(format, a...)
 	fmt.Println(msg)
 }
 
 func (c *colorConsole) Debug(format string, a ...interface{}) {
+	if !c.enable {
+		return
+	}
 	msg := fmt.Sprintf(format, a...)
 	println(aurora.BrightCyan(msg))
 }
 
 func (c *colorConsole) Success(format string, a ...interface{}) {
+	if !c.enable {
+		return
+	}
 	msg := fmt.Sprintf(format, a...)
 	println(aurora.BrightGreen(msg))
 }
 
 func (c *colorConsole) Warning(format string, a ...interface{}) {
+	if !c.enable {
+		return
+	}
 	msg := fmt.Sprintf(format, a...)
 	println(aurora.BrightYellow(msg))
 }
 
 func (c *colorConsole) Error(format string, a ...interface{}) {
+	if !c.enable {
+		return
+	}
 	msg := fmt.Sprintf(format, a...)
 	println(aurora.BrightRed(msg))
 }
 
 func (c *colorConsole) Fatalln(format string, a ...interface{}) {
+	if !c.enable {
+		return
+	}
 	c.Error(format, a...)
 	os.Exit(1)
 }
 
 func (c *colorConsole) MarkDone() {
+	if !c.enable {
+		return
+	}
 	c.Success("Done.")
 }
 
 func (c *colorConsole) Must(err error) {
+	if !c.enable {
+		return
+	}
 	if err != nil {
 		c.Fatalln("%+v", err)
 	}