Skip to content

Commit

Permalink
Provide version information as a CLI option
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Nov 3, 2020
1 parent 517a7ec commit 05a22c3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 29 deletions.
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

func main() {
c, err := cli.New(
cli.WithCommandName("kubebuilder"),
cli.WithVersion(version.String()),
cli.WithPlugins(
&pluginv2.Plugin{},
&pluginv3.Plugin{},
Expand All @@ -36,7 +38,6 @@ func main() {
),
cli.WithExtraCommands(
newCompletionCmd(),
version.NewCmd(),
),
)
if err != nil {
Expand Down
33 changes: 6 additions & 27 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
Expand All @@ -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()
})
}
20 changes: 19 additions & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -173,6 +176,14 @@ func WithExtraCommands(cmds ...*cobra.Command) 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
}
}

// initialize initializes the cli.
func (c *cli) initialize() error {
// Initialize cli with globally-relevant flags or flags that determine
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions pkg/cli/version.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}

0 comments on commit 05a22c3

Please sign in to comment.