feat: Replace cli to cobra (#1855)

* Replace cli

* Replace cli

* Replace cli

* Format code

* Add compare case

* Add compare case

* Add compare case

* Support go style flag

* Support go style flag

* Add test case
This commit is contained in:
anqiansong
2022-05-07 15:40:11 +08:00
committed by GitHub
parent 51472004a3
commit 5383e29ce6
61 changed files with 1858 additions and 1590 deletions

View File

@@ -5,7 +5,7 @@ import (
"strings"
"time"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/pkg/env"
"github.com/zeromicro/go-zero/tools/goctl/pkg/protoc"
"github.com/zeromicro/go-zero/tools/goctl/pkg/protocgengo"
@@ -37,11 +37,8 @@ var bins = []bin{
},
}
func Check(ctx *cli.Context) error {
install := ctx.Bool("install")
force := ctx.Bool("force")
verbose := ctx.Bool("verbose")
return Prepare(install, force, verbose)
func check(_ *cobra.Command, _ []string) error {
return Prepare(boolVarInstall, boolVarForce, boolVarVerbose)
}
func Prepare(install, force, verbose bool) error {

46
tools/goctl/env/cmd.go vendored Normal file
View File

@@ -0,0 +1,46 @@
package env
import "github.com/spf13/cobra"
var (
sliceVarWriteValue []string
boolVarForce bool
boolVarVerbose bool
boolVarInstall bool
// Cmd describes a env command.
Cmd = &cobra.Command{
Use: "env",
Short: "Check or edit goctl environment",
RunE: write,
}
installCmd = &cobra.Command{
Use: "install",
Short: "Goctl env installation",
RunE: install,
}
checkCmd = &cobra.Command{
Use: "check",
Short: "Detect goctl env and dependency tools",
RunE: check,
}
)
func init() {
// The root command flags
Cmd.Flags().StringSliceVarP(&sliceVarWriteValue,
"write", "w", nil, "Edit goctl environment")
Cmd.PersistentFlags().BoolVarP(&boolVarForce,
"force", "f", false,
"Silent installation of non-existent dependencies")
Cmd.PersistentFlags().BoolVarP(&boolVarVerbose,
"verbose", "v", false, "Enable log output")
// The sub-command flags
checkCmd.Flags().BoolVarP(&boolVarInstall, "install", "i",
false, "Install dependencies if not found")
// Add sub-command
Cmd.AddCommand(installCmd)
Cmd.AddCommand(checkCmd)
}

View File

@@ -3,14 +3,13 @@ package env
import (
"fmt"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/pkg/env"
)
func Action(c *cli.Context) error {
write := c.StringSlice("write")
if len(write) > 0 {
return env.WriteEnv(write)
func write(_ *cobra.Command, _ []string) error {
if len(sliceVarWriteValue) > 0 {
return env.WriteEnv(sliceVarWriteValue)
}
fmt.Println(env.Print())
return nil

View File

@@ -1,9 +1,9 @@
package env
import "github.com/urfave/cli"
import (
"github.com/spf13/cobra"
)
func Install(c *cli.Context) error {
force := c.Bool("force")
verbose := c.Bool("verbose")
return Prepare(true, force, verbose)
func install(_ *cobra.Command, _ []string) error {
return Prepare(true, boolVarForce, boolVarVerbose)
}