From 7f6531fea4dde95c5492038007924d4204b580f8 Mon Sep 17 00:00:00 2001 From: Hongchao Deng Date: Fri, 23 Feb 2018 14:53:10 -0800 Subject: [PATCH] cmd: add generate and refactor structure --- commands/operator-sdk/cmd/build.go | 27 +++++++++-------- commands/operator-sdk/cmd/generate.go | 18 ++++++++++++ commands/operator-sdk/cmd/generate/k8s.go | 16 ++++++++++ commands/operator-sdk/cmd/new.go | 36 +++++++++++------------ commands/operator-sdk/cmd/root.go | 15 +++++++--- commands/operator-sdk/main.go | 2 +- 6 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 commands/operator-sdk/cmd/generate.go create mode 100644 commands/operator-sdk/cmd/generate/k8s.go diff --git a/commands/operator-sdk/cmd/build.go b/commands/operator-sdk/cmd/build.go index fc03afc91b..d500873c82 100644 --- a/commands/operator-sdk/cmd/build.go +++ b/commands/operator-sdk/cmd/build.go @@ -2,21 +2,24 @@ package cmd import "github.com/spf13/cobra" -// buildCmd represents the build command -var buildCmd = &cobra.Command{ - Use: "build ", - Short: "Compiles code and builds artifacts", - Long: `The operator-sdk build command compiles the code, builds the executables, - and generates Kubernetes manifests. +func NewBuildCmd() *cobra.Command { + return &cobra.Command{ + Use: "build ", + Short: "Compiles code and builds artifacts", + Long: `The operator-sdk build command compiles the code, builds the executables, +and generates Kubernetes manifests. - is the container image to be built, e.g. "quay.io/example/operator:v0.0.1". - This image will be automatically set in the deployment manifests. + is the container image to be built, e.g. "quay.io/example/operator:v0.0.1". +This image will be automatically set in the deployment manifests. - After build completes, the image would be built locally in docker. Then it needs to - be pushed to remote registry. - For example: +After build completes, the image would be built locally in docker. Then it needs to +be pushed to remote registry. +For example: $ operator-sdk build quay.io/example/operator:v0.0.1 $ docker push quay.io/example/operator:v0.0.1 `, - Run: newFunc, + Run: func(cmd *cobra.Command, args []string) { + panic("UNIMPLEMENTED") + }, + } } diff --git a/commands/operator-sdk/cmd/generate.go b/commands/operator-sdk/cmd/generate.go new file mode 100644 index 0000000000..fc2e95a87c --- /dev/null +++ b/commands/operator-sdk/cmd/generate.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/coreos/operator-sdk/commands/operator-sdk/cmd/generate" + + "github.com/spf13/cobra" +) + +func NewGenerateCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "generate ", + Short: "Invokes specific generator", + Long: `The operator-sdk generate command invokes specific generator to generate code as needed. +`, + } + cmd.AddCommand(generate.NewGenerateK8SCmd()) + return cmd +} diff --git a/commands/operator-sdk/cmd/generate/k8s.go b/commands/operator-sdk/cmd/generate/k8s.go new file mode 100644 index 0000000000..6dd835903f --- /dev/null +++ b/commands/operator-sdk/cmd/generate/k8s.go @@ -0,0 +1,16 @@ +package generate + +import "github.com/spf13/cobra" + +func NewGenerateK8SCmd() *cobra.Command { + return &cobra.Command{ + Use: "k8s", + Short: "Generates Kubernetes code for custom resource", + Long: `k8s generator generates code for custom resource given the API spec +to comply with kube-API requirements. +`, + Run: func(cmd *cobra.Command, args []string) { + panic("UNIMPLEMENTED") + }, + } +} diff --git a/commands/operator-sdk/cmd/new.go b/commands/operator-sdk/cmd/new.go index 7a0675e226..e43f4a623a 100644 --- a/commands/operator-sdk/cmd/new.go +++ b/commands/operator-sdk/cmd/new.go @@ -11,24 +11,32 @@ import ( "github.com/spf13/cobra" ) -// newCmd represents the new command -var newCmd = &cobra.Command{ - Use: "new [required-flags]", - Short: "Creates a new operator application", - Long: `The operator-sdk new command creates a new operator application and - generates a default directory layout based on the input . +func NewNewCmd() *cobra.Command { + newCmd := &cobra.Command{ + Use: "new [required-flags]", + Short: "Creates a new operator application", + Long: `The operator-sdk new command creates a new operator application and +generates a default directory layout based on the input . - is the project name of the new operator. (e.g app-operator) + is the project name of the new operator. (e.g app-operator) --api-version and --kind are required flags to generate the new operator application. - For example, +For example: $ mkdir $GOPATH/src/github.com/example.com/ $ cd $GOPATH/src/github.com/example.com/ $ operator-sdk new app-operator --api-group=app.example.com --kind=AppService - generates a skeletal app-operator application in $GOPATH/src/github.com/example.com/app-operator. +generates a skeletal app-operator application in $GOPATH/src/github.com/example.com/app-operator. `, - Run: newFunc, + Run: newFunc, + } + + newCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes apiVersion and has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)") + newCmd.MarkFlagRequired("api-version") + newCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes CustomResourceDefintion kind. (e.g AppService)") + newCmd.MarkFlagRequired("kind") + + return newCmd } var ( @@ -42,14 +50,6 @@ const ( src = "src" ) -func init() { - RootCmd.AddCommand(newCmd) - newCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes apiVersion and has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)") - newCmd.MarkFlagRequired("api-version") - newCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes CustomResourceDefintion kind. (e.g AppService)") - newCmd.MarkFlagRequired("kind") -} - func newFunc(cmd *cobra.Command, args []string) { if len(args) != 1 { ExitWithError(ExitBadArgs, fmt.Errorf("new command needs 1 argument.")) diff --git a/commands/operator-sdk/cmd/root.go b/commands/operator-sdk/cmd/root.go index f9c8c22f7b..b9d3863492 100644 --- a/commands/operator-sdk/cmd/root.go +++ b/commands/operator-sdk/cmd/root.go @@ -2,8 +2,15 @@ package cmd import "github.com/spf13/cobra" -// This represents the base command when called without any subcommands -var RootCmd = &cobra.Command{ - Use: "operator-sdk", - Short: "A sdk for building operator with ease", +func NewRootCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "operator-sdk", + Short: "A sdk for building operator with ease", + } + + cmd.AddCommand(NewNewCmd()) + cmd.AddCommand(NewBuildCmd()) + cmd.AddCommand(NewGenerateCmd()) + + return cmd } diff --git a/commands/operator-sdk/main.go b/commands/operator-sdk/main.go index 42dfcfb37c..ad5cc46c09 100644 --- a/commands/operator-sdk/main.go +++ b/commands/operator-sdk/main.go @@ -8,7 +8,7 @@ import ( ) func main() { - if err := cmd.RootCmd.Execute(); err != nil { + if err := cmd.NewRootCmd().Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(-1) }