From ea92c92d61ea5a5aeb0782ddadfd26c5ca84b9ca Mon Sep 17 00:00:00 2001 From: Adrian Orive Date: Tue, 3 Nov 2020 09:00:38 +0100 Subject: [PATCH] Provide version information as a CLI option Signed-off-by: Adrian Orive --- cmd/main.go | 2 +- cmd/version/version.go | 33 ++++++--------------------------- pkg/cli/cli.go | 20 +++++++++++++++++++- pkg/cli/cli_test.go | 10 ++++++++++ pkg/cli/version.go | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 29 deletions(-) create mode 100644 pkg/cli/version.go diff --git a/cmd/main.go b/cmd/main.go index 0839bcd2ba4..03028563ce5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,6 +27,7 @@ import ( func main() { c, err := cli.New( + cli.WithVersion(version.String()), cli.WithPlugins( &pluginv2.Plugin{}, &pluginv3.Plugin{}, @@ -36,7 +37,6 @@ func main() { ), cli.WithExtraCommands( newCompletionCmd(), - version.NewCmd(), ), ) if err != nil { diff --git a/cmd/version/version.go b/cmd/version/version.go index 4c9cb189ace..03fb6d6cb49 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -18,8 +18,6 @@ package version import ( "fmt" - - "github.com/spf13/cobra" ) // var needs to be used instead of const as ldflags is used to fill this @@ -34,8 +32,8 @@ var ( buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') ) -// Version contains all the information related to the CLI version -type Version struct { +// version contains all the information related to the CLI version +type version struct { KubeBuilderVersion string `json:"kubeBuilderVersion"` KubernetesVendor string `json:"kubernetesVendor"` GitCommit string `json:"gitCommit"` @@ -44,33 +42,14 @@ type Version struct { GoArch string `json:"goArch"` } -func getVersion() Version { - return Version{ +// String returns the CLI version +func String() string { + return fmt.Sprintf("Version: %#v", version{ kubeBuilderVersion, kubernetesVendorVersion, gitCommit, buildDate, goos, goarch, - } -} - -// Print prints the CLI version -func (v Version) Print() { - fmt.Printf("Version: %#v\n", v) -} - -// NewCmd creates a new command that prints the CLI version -func NewCmd() *cobra.Command { - return &cobra.Command{ - Use: "version", - Short: "Print the kubebuilder version", - Long: `Print the kubebuilder version`, - Example: `kubebuilder version`, - Run: runVersion, - } -} - -func runVersion(_ *cobra.Command, _ []string) { - getVersion().Print() + }) } diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 350094386d0..8fe813e990f 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -57,6 +57,9 @@ type Option func(*cli) error type cli struct { // Base command name. Can be injected downstream. commandName string + // CLI version string + version string + // Default project version. Used in CLI flag setup. defaultProjectVersion string // Project version to scaffold. @@ -115,6 +118,14 @@ func WithCommandName(name string) Option { } } +// WithVersion is an Option that defines the version string of the cli. +func WithVersion(version string) Option { + return func(c *cli) error { + c.version = version + return nil + } +} + // WithDefaultProjectVersion is an Option that sets the cli's default project // version. Setting an unknown version will result in an error. func WithDefaultProjectVersion(version string) Option { @@ -356,9 +367,16 @@ func (c cli) buildRootCmd() *cobra.Command { // kubebuilder edit rootCmd.AddCommand(c.newEditCmd()) + // kubebuilder init rootCmd.AddCommand(c.newInitCmd()) + // kubebuilder version + // Only add version if a version string was provided + if c.version != "" { + rootCmd.AddCommand(c.newVersionCmd()) + } + return rootCmd } @@ -409,7 +427,7 @@ After the scaffold is written, api will run make on the project. make run `, c.commandName, c.commandName), - + Version: c.version, Run: func(cmd *cobra.Command, args []string) { if err := cmd.Help(); err != nil { log.Fatal(err) diff --git a/pkg/cli/cli_test.go b/pkg/cli/cli_test.go index bb7c2163ac5..cddcfc49617 100644 --- a/pkg/cli/cli_test.go +++ b/pkg/cli/cli_test.go @@ -231,6 +231,16 @@ var _ = Describe("CLI", func() { }) }) + Context("WithVersion", func() { + It("should use the provided version string", func() { + version := "Version: 0.0.0" + c, err = New(WithVersion(version), WithDefaultPlugins(pluginAV1), WithPlugins(allPlugins...)) + Expect(err).NotTo(HaveOccurred()) + Expect(c).NotTo(BeNil()) + Expect(c.(*cli).version).To(Equal(version)) + }) + }) + Context("with extra commands set", func() { It("should work successfully with extra commands", func() { setPluginsFlag("go.test.com/v2") diff --git a/pkg/cli/version.go b/pkg/cli/version.go new file mode 100644 index 00000000000..29e0957d01f --- /dev/null +++ b/pkg/cli/version.go @@ -0,0 +1,36 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func (c *cli) newVersionCmd() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: fmt.Sprintf("Print the %s version", c.commandName), + Long: fmt.Sprintf("Print the %s version", c.commandName), + Example: fmt.Sprintf("%s version", c.commandName), + RunE: func(_ *cobra.Command, _ []string) error { + fmt.Print(c.version) + return nil + }, + } +}