Add verbose flag (#1696)

Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
anqiansong
2022-03-22 21:00:26 +08:00
committed by GitHub
parent fe262766b4
commit 0aeb49a6b0
6 changed files with 82 additions and 31 deletions

View File

@@ -40,21 +40,23 @@ var bins = []bin{
func Check(ctx *cli.Context) error { func Check(ctx *cli.Context) error {
install := ctx.Bool("install") install := ctx.Bool("install")
force := ctx.Bool("force") 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 pending := true
console.Info("[goctl-env]: preparing to check env") log.Info("[goctl-env]: preparing to check env")
defer func() { defer func() {
if p := recover(); p != nil { if p := recover(); p != nil {
console.Error("%+v", p) log.Error("%+v", p)
return return
} }
if pending { 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 { } else {
console.Error(` log.Error(`
[goctl-env]: check env finish, some dependencies is not found in PATH, you can execute [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 command 'goctl env check --install' to install it, for details, please execute command
'goctl env check --help'`) '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 { for _, e := range bins {
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
console.Info("") log.Info("")
console.Info("[goctl-env]: looking up %q", e.name) log.Info("[goctl-env]: looking up %q", e.name)
if e.exists { if e.exists {
console.Success("[goctl-env]: %q is installed", e.name) log.Success("[goctl-env]: %q is installed", e.name)
continue 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 { if install {
install := func() { 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)) path, err := e.get(env.Get(env.GoctlCache))
if err != nil { 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 pending = false
} else { } 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 { if force {
install() install()
continue 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 { for {
var in string var in string
fmt.Scanln(&in) fmt.Scanln(&in)
@@ -95,10 +97,10 @@ command 'goctl env check --install' to install it, for details, please execute c
brk = true brk = true
case strings.EqualFold(in, "n"): case strings.EqualFold(in, "n"):
pending = false pending = false
console.Info("[goctl-env]: %q installation is ignored", e.name) log.Info("[goctl-env]: %q installation is ignored", e.name)
brk = true brk = true
default: 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 { if brk {
break break

View File

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

View File

@@ -23,6 +23,7 @@ func RPCNew(c *cli.Context) error {
home := c.String("home") home := c.String("home")
remote := c.String("remote") remote := c.String("remote")
branch := c.String("branch") branch := c.String("branch")
verbose := c.Bool("verbose")
if len(remote) > 0 { if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch) repo, _ := util.CloneIntoGitHome(remote, branch)
if len(repo) > 0 { if len(repo) > 0 {
@@ -52,7 +53,7 @@ func RPCNew(c *cli.Context) error {
ctx.IsGooglePlugin = true ctx.IsGooglePlugin = true
ctx.Output = filepath.Dir(src) 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)) 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) return g.Generate(&ctx)
} }

View File

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

View File

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

View File

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