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:
32
tools/goctl/docker/cmd.go
Normal file
32
tools/goctl/docker/cmd.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package docker
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var (
|
||||
varStringGo string
|
||||
varStringBase string
|
||||
varIntPort int
|
||||
varStringHome string
|
||||
varStringRemote string
|
||||
varStringBranch string
|
||||
varStringVersion string
|
||||
varStringTZ string
|
||||
|
||||
// Cmd describes a docker command.
|
||||
Cmd = &cobra.Command{
|
||||
Use: "docker",
|
||||
Short: "Generate Dockerfile",
|
||||
RunE: dockerCommand,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
Cmd.Flags().StringVar(&varStringGo, "go", "", "The file that contains main function")
|
||||
Cmd.Flags().StringVar(&varStringBase, "base", "scratch", "The base image to build the docker image, default scratch")
|
||||
Cmd.Flags().IntVar(&varIntPort, "port", 0, "The port to expose, default none")
|
||||
Cmd.Flags().StringVar(&varStringHome, "home", "", "The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority")
|
||||
Cmd.Flags().StringVar(&varStringRemote, "remote", "", "The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure")
|
||||
Cmd.Flags().StringVar(&varStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
|
||||
Cmd.Flags().StringVar(&varStringVersion, "version", "", "The goctl builder golang image version")
|
||||
Cmd.Flags().StringVar(&varStringTZ, "tz", "Asia/Shanghai", "The timezone of the container")
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/urfave/cli"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
@@ -37,24 +37,20 @@ type Docker struct {
|
||||
Timezone string
|
||||
}
|
||||
|
||||
// DockerCommand provides the entry for goctl docker
|
||||
func DockerCommand(c *cli.Context) (err error) {
|
||||
// dockerCommand provides the entry for goctl docker
|
||||
func dockerCommand(_ *cobra.Command, _ []string) (err error) {
|
||||
defer func() {
|
||||
if err == nil {
|
||||
fmt.Println(aurora.Green("Done."))
|
||||
}
|
||||
}()
|
||||
|
||||
if c.NumFlags() == 0 {
|
||||
cli.ShowCommandHelpAndExit(c, "docker", 1)
|
||||
}
|
||||
|
||||
goFile := c.String("go")
|
||||
home := c.String("home")
|
||||
version := c.String("version")
|
||||
remote := c.String("remote")
|
||||
branch := c.String("branch")
|
||||
timezone := c.String("tz")
|
||||
goFile := varStringGo
|
||||
home := varStringHome
|
||||
version := varStringVersion
|
||||
remote := varStringRemote
|
||||
branch := varStringBranch
|
||||
timezone := varStringTZ
|
||||
if len(remote) > 0 {
|
||||
repo, _ := util.CloneIntoGitHome(remote, branch)
|
||||
if len(repo) > 0 {
|
||||
@@ -78,8 +74,8 @@ func DockerCommand(c *cli.Context) (err error) {
|
||||
return fmt.Errorf("file %q not found", goFile)
|
||||
}
|
||||
|
||||
base := c.String("base")
|
||||
port := c.Int("port")
|
||||
base := varStringBase
|
||||
port := varIntPort
|
||||
if _, err := os.Stat(etcDir); os.IsNotExist(err) {
|
||||
return generateDockerfile(goFile, base, port, version, timezone)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package docker
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
@@ -21,7 +20,7 @@ func Clean() error {
|
||||
}
|
||||
|
||||
// GenTemplates creates docker template files
|
||||
func GenTemplates(_ *cli.Context) error {
|
||||
func GenTemplates() error {
|
||||
return initTemplate()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user