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 8, 2020
1 parent 7868757 commit 16ee059
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
builds:
- main: ./cmd
binary: kubebuilder
ldflags: -s -X sigs.k8s.io/kubebuilder/v2/cmd/version.kubeBuilderVersion={{.Version}} -X sigs.k8s.io/kubebuilder/v2/cmd/version.gitCommit={{.Commit}} -X sigs.k8s.io/kubebuilder/v2/cmd/version.buildDate={{.Date}} -X sigs.k8s.io/kubebuilder/v2/cmd/version.kubernetesVendorVersion={{.Env.KUBERNETES_VERSION}}
ldflags: -s -X sigs.k8s.io/kubebuilder/v2/cmd.kubeBuilderVersion={{.Version}} -X sigs.k8s.io/kubebuilder/v2/cmd.gitCommit={{.Commit}} -X sigs.k8s.io/kubebuilder/v2/cmd.buildDate={{.Date}} -X sigs.k8s.io/kubebuilder/v2/cmd.kubernetesVendorVersion={{.Env.KUBERNETES_VERSION}}
goos:
- darwin
- linux
Expand Down
5 changes: 1 addition & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"log"

"sigs.k8s.io/kubebuilder/v2/cmd/version"
"sigs.k8s.io/kubebuilder/v2/pkg/cli"
pluginv2 "sigs.k8s.io/kubebuilder/v2/pkg/plugin/v2"
pluginv3 "sigs.k8s.io/kubebuilder/v2/pkg/plugin/v3"
Expand All @@ -28,16 +27,14 @@ import (
func main() {
c, err := cli.New(
cli.WithCommandName("kubebuilder"),
cli.WithVersion(versionString()),
cli.WithPlugins(
&pluginv2.Plugin{},
&pluginv3.Plugin{},
),
cli.WithDefaultPlugins(
&pluginv2.Plugin{},
),
cli.WithExtraCommands(
version.NewCmd(),
),
cli.WithCompletion,
)
if err != nil {
Expand Down
35 changes: 7 additions & 28 deletions cmd/version/version.go → cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package version
package main

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{
// versionString returns the CLI version
func versionString() 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()
})
}
4 changes: 2 additions & 2 deletions common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ function build_kb {
if [ "$INJECT_KB_VERSION" = "unknown" ]; then
opts=""
else
# TODO: what does this thing do.
opts=-ldflags "-X sigs.k8s.io/kubebuilder/v2/cmd/version.kubeBuilderVersion=$INJECT_KB_VERSION"
# Injects the version into the cmd/version.go file
opts=-ldflags "-X sigs.k8s.io/kubebuilder/v2/cmd.kubeBuilderVersion=$INJECT_KB_VERSION"
fi

GO111MODULE=on go build $opts -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd
Expand Down
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 {
// Root 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 @@ -118,6 +121,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 {
Expand Down Expand Up @@ -363,9 +374,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 @@ -416,7 +434,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
10 changes: 10 additions & 0 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,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("WithDefaultProjectVersion", func() {
var defaultProjectVersion string

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.Println(c.version)
return nil
},
}
}

0 comments on commit 16ee059

Please sign in to comment.