Skip to content

Commit c158f4f

Browse files
authored
Merge pull request #1773 from Adirio/version-cmd
⚠Provide version information as a CLI option
2 parents 0cebbd6 + 8c51104 commit c158f4f

File tree

7 files changed

+75
-36
lines changed

7 files changed

+75
-36
lines changed

build/.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
builds:
1818
- main: ./cmd
1919
binary: kubebuilder
20-
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}}
20+
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}}
2121
goos:
2222
- darwin
2323
- linux

cmd/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package main
1919
import (
2020
"log"
2121

22-
"sigs.k8s.io/kubebuilder/v2/cmd/version"
2322
"sigs.k8s.io/kubebuilder/v2/pkg/cli"
2423
pluginv2 "sigs.k8s.io/kubebuilder/v2/pkg/plugin/v2"
2524
pluginv3 "sigs.k8s.io/kubebuilder/v2/pkg/plugin/v3"
@@ -28,16 +27,14 @@ import (
2827
func main() {
2928
c, err := cli.New(
3029
cli.WithCommandName("kubebuilder"),
30+
cli.WithVersion(versionString()),
3131
cli.WithPlugins(
3232
&pluginv2.Plugin{},
3333
&pluginv3.Plugin{},
3434
),
3535
cli.WithDefaultPlugins(
3636
&pluginv2.Plugin{},
3737
),
38-
cli.WithExtraCommands(
39-
version.NewCmd(),
40-
),
4138
cli.WithCompletion,
4239
)
4340
if err != nil {

cmd/version/version.go renamed to cmd/version.go

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package version
17+
package main
1818

1919
import (
2020
"fmt"
21-
22-
"github.com/spf13/cobra"
2321
)
2422

2523
// var needs to be used instead of const as ldflags is used to fill this
@@ -34,8 +32,8 @@ var (
3432
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
3533
)
3634

37-
// Version contains all the information related to the CLI version
38-
type Version struct {
35+
// version contains all the information related to the CLI version
36+
type version struct {
3937
KubeBuilderVersion string `json:"kubeBuilderVersion"`
4038
KubernetesVendor string `json:"kubernetesVendor"`
4139
GitCommit string `json:"gitCommit"`
@@ -44,33 +42,14 @@ type Version struct {
4442
GoArch string `json:"goArch"`
4543
}
4644

47-
func getVersion() Version {
48-
return Version{
45+
// versionString returns the CLI version
46+
func versionString() string {
47+
return fmt.Sprintf("Version: %#v", version{
4948
kubeBuilderVersion,
5049
kubernetesVendorVersion,
5150
gitCommit,
5251
buildDate,
5352
goos,
5453
goarch,
55-
}
56-
}
57-
58-
// Print prints the CLI version
59-
func (v Version) Print() {
60-
fmt.Printf("Version: %#v\n", v)
61-
}
62-
63-
// NewCmd creates a new command that prints the CLI version
64-
func NewCmd() *cobra.Command {
65-
return &cobra.Command{
66-
Use: "version",
67-
Short: "Print the kubebuilder version",
68-
Long: `Print the kubebuilder version`,
69-
Example: `kubebuilder version`,
70-
Run: runVersion,
71-
}
72-
}
73-
74-
func runVersion(_ *cobra.Command, _ []string) {
75-
getVersion().Print()
54+
})
7655
}

common.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ function build_kb {
149149
if [ "$INJECT_KB_VERSION" = "unknown" ]; then
150150
opts=""
151151
else
152-
# TODO: what does this thing do.
153-
opts=-ldflags "-X sigs.k8s.io/kubebuilder/v2/cmd/version.kubeBuilderVersion=$INJECT_KB_VERSION"
152+
# Injects the version into the cmd/version.go file
153+
opts=-ldflags "-X sigs.k8s.io/kubebuilder/v2/cmd.kubeBuilderVersion=$INJECT_KB_VERSION"
154154
fi
155155

156156
GO111MODULE=on go build $opts -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd

pkg/cli/cli.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ type Option func(*cli) error
5757
type cli struct {
5858
// Root command name. Can be injected downstream.
5959
commandName string
60+
// CLI version string
61+
version string
62+
6063
// Default project version. Used in CLI flag setup.
6164
defaultProjectVersion string
6265
// Project version to scaffold.
@@ -118,6 +121,14 @@ func WithCommandName(name string) Option {
118121
}
119122
}
120123

124+
// WithVersion is an Option that defines the version string of the cli.
125+
func WithVersion(version string) Option {
126+
return func(c *cli) error {
127+
c.version = version
128+
return nil
129+
}
130+
}
131+
121132
// WithDefaultProjectVersion is an Option that sets the cli's default project
122133
// version. Setting an unknown version will result in an error.
123134
func WithDefaultProjectVersion(version string) Option {
@@ -363,9 +374,16 @@ func (c cli) buildRootCmd() *cobra.Command {
363374

364375
// kubebuilder edit
365376
rootCmd.AddCommand(c.newEditCmd())
377+
366378
// kubebuilder init
367379
rootCmd.AddCommand(c.newInitCmd())
368380

381+
// kubebuilder version
382+
// Only add version if a version string was provided
383+
if c.version != "" {
384+
rootCmd.AddCommand(c.newVersionCmd())
385+
}
386+
369387
return rootCmd
370388
}
371389

@@ -416,7 +434,6 @@ After the scaffold is written, api will run make on the project.
416434
make run
417435
`,
418436
c.commandName, c.commandName),
419-
420437
Run: func(cmd *cobra.Command, args []string) {
421438
if err := cmd.Help(); err != nil {
422439
log.Fatal(err)

pkg/cli/cli_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ var _ = Describe("CLI", func() {
291291
})
292292
})
293293

294+
Context("WithVersion", func() {
295+
It("should use the provided version string", func() {
296+
version := "Version: 0.0.0"
297+
c, err = New(WithVersion(version), WithDefaultPlugins(pluginAV1), WithPlugins(allPlugins...))
298+
Expect(err).NotTo(HaveOccurred())
299+
Expect(c).NotTo(BeNil())
300+
Expect(c.(*cli).version).To(Equal(version))
301+
})
302+
})
303+
294304
Context("WithDefaultProjectVersion", func() {
295305
var defaultProjectVersion string
296306

pkg/cli/version.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cli
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func (c *cli) newVersionCmd() *cobra.Command {
26+
return &cobra.Command{
27+
Use: "version",
28+
Short: fmt.Sprintf("Print the %s version", c.commandName),
29+
Long: fmt.Sprintf("Print the %s version", c.commandName),
30+
Example: fmt.Sprintf("%s version", c.commandName),
31+
RunE: func(_ *cobra.Command, _ []string) error {
32+
fmt.Println(c.version)
33+
return nil
34+
},
35+
}
36+
}

0 commit comments

Comments
 (0)