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:
71
tools/goctl/kube/cmd.go
Normal file
71
tools/goctl/kube/cmd.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package kube
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var (
|
||||
varStringName string
|
||||
varStringNamespace string
|
||||
varStringImage string
|
||||
varStringSecret string
|
||||
varIntRequestCpu int
|
||||
varIntRequestMem int
|
||||
varIntLimitCpu int
|
||||
varIntLimitMem int
|
||||
varStringO string
|
||||
varIntReplicas int
|
||||
varIntRevisions int
|
||||
varIntPort int
|
||||
varIntNodePort int
|
||||
varIntMinReplicas int
|
||||
varIntMaxReplicas int
|
||||
varStringHome string
|
||||
varStringRemote string
|
||||
varStringBranch string
|
||||
varStringServiceAccount string
|
||||
|
||||
// Cmd describes a kube command.
|
||||
Cmd = &cobra.Command{
|
||||
Use: "kube",
|
||||
Short: "Generate kubernetes files",
|
||||
}
|
||||
|
||||
deployCmd = &cobra.Command{
|
||||
Use: "deploy",
|
||||
Short: "Generate deployment yaml file",
|
||||
RunE: deploymentCommand,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
deployCmd.Flags().StringVar(&varStringName, "name", "", "The name of deployment (required)")
|
||||
deployCmd.Flags().StringVar(&varStringNamespace, "namespace", "", "The namespace of deployment (required)")
|
||||
deployCmd.Flags().StringVar(&varStringImage, "image", "", "The docker image of deployment (required)")
|
||||
deployCmd.Flags().StringVar(&varStringSecret, "secret", "", "The secret to image pull from registry")
|
||||
deployCmd.Flags().IntVar(&varIntRequestCpu, "requestCpu", 500, "The request cpu to deploy")
|
||||
deployCmd.Flags().IntVar(&varIntRequestMem, "requestMem", 512, "The request memory to deploy")
|
||||
deployCmd.Flags().IntVar(&varIntLimitCpu, "limitCpu", 1000, "The limit cpu to deploy")
|
||||
deployCmd.Flags().IntVar(&varIntLimitMem, "limitMem", 1024, "The limit memory to deploy")
|
||||
deployCmd.Flags().StringVar(&varStringO, "o", "", "The output yaml file (required)")
|
||||
deployCmd.Flags().IntVar(&varIntReplicas, "replicas", 3, "The number of replicas to deploy")
|
||||
deployCmd.Flags().IntVar(&varIntRevisions, "revisions", 5, "The number of revision history to limit")
|
||||
deployCmd.Flags().IntVar(&varIntPort, "port", 0, "The port of the deployment to listen on pod (required)")
|
||||
deployCmd.Flags().IntVar(&varIntNodePort, "nodePort", 0, "The nodePort of the deployment to expose")
|
||||
deployCmd.Flags().IntVar(&varIntMinReplicas, "minReplicas", 3, "The min replicas to deploy")
|
||||
deployCmd.Flags().IntVar(&varIntMaxReplicas, "maxReplicas", 10, "The max replicas to deploy")
|
||||
|
||||
deployCmd.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")
|
||||
deployCmd.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")
|
||||
deployCmd.Flags().StringVar(&varStringBranch, "branch", "", "The branch of the remote repo, it "+
|
||||
"does work with --remote")
|
||||
deployCmd.Flags().StringVar(&varStringServiceAccount, "serviceAccount", "", "The ServiceAccount "+
|
||||
"for the deployment")
|
||||
deployCmd.MarkFlagRequired("name")
|
||||
deployCmd.MarkFlagRequired("namespace")
|
||||
deployCmd.MarkFlagRequired("o")
|
||||
deployCmd.MarkFlagRequired("port")
|
||||
|
||||
Cmd.AddCommand(deployCmd)
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"text/template"
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -48,11 +48,11 @@ type Deployment struct {
|
||||
}
|
||||
|
||||
// DeploymentCommand is used to generate the kubernetes deployment yaml files.
|
||||
func DeploymentCommand(c *cli.Context) error {
|
||||
nodePort := c.Int("nodePort")
|
||||
home := c.String("home")
|
||||
remote := c.String("remote")
|
||||
branch := c.String("branch")
|
||||
func deploymentCommand(_ *cobra.Command, _ []string) error {
|
||||
nodePort := varIntNodePort
|
||||
home := varStringHome
|
||||
remote := varStringRemote
|
||||
branch := varStringBranch
|
||||
if len(remote) > 0 {
|
||||
repo, _ := util.CloneIntoGitHome(remote, branch)
|
||||
if len(repo) > 0 {
|
||||
@@ -74,7 +74,7 @@ func DeploymentCommand(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
out, err := pathx.CreateIfNotExist(c.String("o"))
|
||||
out, err := pathx.CreateIfNotExist(varStringO)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -82,22 +82,22 @@ func DeploymentCommand(c *cli.Context) error {
|
||||
|
||||
t := template.Must(template.New("deploymentTemplate").Parse(text))
|
||||
err = t.Execute(out, Deployment{
|
||||
Name: c.String("name"),
|
||||
Namespace: c.String("namespace"),
|
||||
Image: c.String("image"),
|
||||
Secret: c.String("secret"),
|
||||
Replicas: c.Int("replicas"),
|
||||
Revisions: c.Int("revisions"),
|
||||
Port: c.Int("port"),
|
||||
Name: varStringName,
|
||||
Namespace: varStringNamespace,
|
||||
Image: varStringImage,
|
||||
Secret: varStringSecret,
|
||||
Replicas: varIntReplicas,
|
||||
Revisions: varIntRevisions,
|
||||
Port: varIntPort,
|
||||
NodePort: nodePort,
|
||||
UseNodePort: nodePort > 0,
|
||||
RequestCpu: c.Int("requestCpu"),
|
||||
RequestMem: c.Int("requestMem"),
|
||||
LimitCpu: c.Int("limitCpu"),
|
||||
LimitMem: c.Int("limitMem"),
|
||||
MinReplicas: c.Int("minReplicas"),
|
||||
MaxReplicas: c.Int("maxReplicas"),
|
||||
ServiceAccount: c.String("serviceAccount"),
|
||||
RequestCpu: varIntRequestCpu,
|
||||
RequestMem: varIntRequestMem,
|
||||
LimitCpu: varIntLimitCpu,
|
||||
LimitMem: varIntLimitMem,
|
||||
MinReplicas: varIntMinReplicas,
|
||||
MaxReplicas: varIntMaxReplicas,
|
||||
ServiceAccount: varStringServiceAccount,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -118,7 +118,7 @@ func Clean() error {
|
||||
}
|
||||
|
||||
// GenTemplates generates the deployment template files.
|
||||
func GenTemplates(_ *cli.Context) error {
|
||||
func GenTemplates() error {
|
||||
return pathx.InitTemplates(category, map[string]string{
|
||||
deployTemplateFile: deploymentTemplate,
|
||||
jobTemplateFile: jobTemplate,
|
||||
|
||||
Reference in New Issue
Block a user