Skip to content

Commit ae97bbf

Browse files
committed
change approach
1 parent 3f7ffcd commit ae97bbf

File tree

7 files changed

+67
-70
lines changed

7 files changed

+67
-70
lines changed

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Run() {
6464

6565
c, err := cli.New(
6666
cli.WithCommandName("kubebuilder"),
67-
cli.WithVersion(versionMap()),
67+
cli.WithVersion(versionStruct()),
6868
cli.WithCliVersion(getKubebuilderVersion()),
6969
cli.WithPlugins(
7070
golangv4.Plugin{},

cmd/version.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"golang.org/x/mod/semver"
31+
"sigs.k8s.io/kubebuilder/v4/pkg/cli"
3132
)
3233

3334
const (
@@ -48,30 +49,20 @@ var (
4849
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
4950
)
5051

51-
// version contains all the information related to the CLI version
52-
type version struct {
53-
KubeBuilderVersion string `json:"kubeBuilderVersion"`
54-
KubernetesVendor string `json:"kubernetesVendor"`
55-
GitCommit string `json:"gitCommit"`
56-
BuildDate string `json:"buildDate"`
57-
GoOs string `json:"goOs"`
58-
GoArch string `json:"goArch"`
59-
}
60-
6152
// versionString returns the Full CLI version
62-
func versionMap() map[string]string {
53+
func versionStruct() *cli.Version {
6354
kubeBuilderVersion = getKubebuilderVersion()
64-
65-
version := map[string]string{
66-
"kubebuilder": kubeBuilderVersion,
67-
"kubernetes": kubernetesVendorVersion,
68-
"buildDate": buildDate,
69-
"gitCommit": gitCommit,
70-
"os": goos,
71-
"arch": goarch,
55+
56+
version := cli.Version{
57+
KubeBuilderVersion: kubeBuilderVersion,
58+
KubernetesVendor: kubernetesVendorVersion,
59+
GitCommit: gitCommit,
60+
BuildDate: buildDate,
61+
GoOs: goos,
62+
GoArch: goarch,
7263
}
73-
74-
return version
64+
65+
return &version
7566
}
7667

7768
// getKubebuilderVersion returns only the CLI version string

pkg/cli/cli.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ type CLI struct {
4848

4949
// Root command name. It is injected downstream to provide correct help, usage, examples and errors.
5050
commandName string
51-
// Full CLI version string.
52-
version map[string]string
51+
// Full CLI version struct.
52+
version *Version
5353
// CLI version string (just the CLI version number, no extra information).
5454
cliVersion string
5555
// CLI root's command description.
@@ -526,7 +526,7 @@ func (c *CLI) addSubcommands() {
526526
c.cmd.AddCommand(c.newInitCmd())
527527

528528
// kubebuilder version
529-
// Only add version if a version string was provided
529+
// Only add version if a version struct was provided
530530
if c.version != nil {
531531
c.cmd.AddCommand(c.newVersionCmd())
532532
}

pkg/cli/cli_test.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -623,18 +623,18 @@ plugins:
623623

624624
When("providing a version string", func() {
625625
It("should create a valid CLI", func() {
626-
version := map[string]string{
627-
"kubebuilder": "123",
628-
"kubernetes": "123",
629-
"buildDate": "2025/12/3",
630-
"gitCommit": "0123fasdf0123",
631-
"os": "os123",
632-
"arch": "arch123",
626+
version := Version{
627+
KubeBuilderVersion: "123",
628+
KubernetesVendor: "123",
629+
GitCommit: "0123fasdf0123",
630+
BuildDate: "2025-12-3",
631+
GoOs: "os123",
632+
GoArch: "arch123",
633633
}
634634
c, err = New(
635635
WithPlugins(&golangv4.Plugin{}),
636636
WithDefaultPlugins(projectVersion, &golangv4.Plugin{}),
637-
WithVersion(version),
637+
WithVersion(&version),
638638
)
639639
Expect(err).NotTo(HaveOccurred())
640640
Expect(hasSubCommand(c.cmd, "version")).To(BeTrue())
@@ -655,19 +655,11 @@ plugins:
655655
Expect(err).NotTo(HaveOccurred())
656656
printed, _ := io.ReadAll(r)
657657
Expect(string(printed)).To(Equal(
658-
fmt.Sprintf(`Kubebuilder: %v
659-
Kubernetes: %v
660-
Git Commit: %v
661-
Build date: %v
662-
OS/Arch: %v/%v
663-
`,
664-
version["kubebuilder"],
665-
version["kubernetes"],
666-
version["gitCommit"],
667-
version["buildDate"],
668-
version["os"],
669-
version["arch"],
670-
)))
658+
fmt.Sprintf("Kubebuilder:\t123\n" +
659+
"Kubernetes:\t123\n" +
660+
"Git Commit:\t0123fasdf0123\n" +
661+
"Build Date:\t2025-12-3\n" +
662+
"OS/Arch:\tos123/arch123\n")))
671663
})
672664
})
673665

pkg/cli/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func WithCommandName(name string) Option {
5050
}
5151

5252
// WithVersion is an Option that defines the version string of the CLI.
53-
func WithVersion(version map[string]string) Option {
53+
func WithVersion(version *Version) Option {
5454
return func(c *CLI) error {
5555
c.version = version
5656
return nil

pkg/cli/options_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -539,19 +539,19 @@ var _ = Describe("CLI options", func() {
539539
})
540540

541541
Context("WithVersion", func() {
542-
It("should use the provided version string", func() {
543-
version := map[string]string{
544-
"kubebuilder": "123",
545-
"kubernetes": "123",
546-
"buildDate": "2025/12/3",
547-
"gitCommit": "0123fasdf0123",
548-
"os": "os123",
549-
"arch": "arch123",
542+
It("should use the provided version struct", func() {
543+
version := Version{
544+
KubeBuilderVersion: "123",
545+
KubernetesVendor: "123",
546+
GitCommit: "asdf1324",
547+
BuildDate: "2025-11-19",
548+
GoOs: "os123",
549+
GoArch: "arch123",
550550
}
551-
c, err = newCLI(WithVersion(version))
551+
c, err = newCLI(WithVersion(&version))
552552
Expect(err).NotTo(HaveOccurred())
553553
Expect(c).NotTo(BeNil())
554-
Expect(c.version).To(Equal(version))
554+
Expect(c.version).To(Equal(&version))
555555
})
556556
})
557557

pkg/cli/version.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,40 @@ import (
2222
"github.com/spf13/cobra"
2323
)
2424

25+
// Version contains all the information related to the CLI version
26+
type Version struct {
27+
KubeBuilderVersion string `json:"kubeBuilderVersion"`
28+
KubernetesVendor string `json:"kubernetesVendor"`
29+
GitCommit string `json:"gitCommit"`
30+
BuildDate string `json:"buildDate"`
31+
GoOs string `json:"goOs"`
32+
GoArch string `json:"goArch"`
33+
}
34+
35+
// String implements Stringer for Version so we can format the output as needed
36+
func (v Version) String() string {
37+
return fmt.Sprintf(
38+
"Kubebuilder:\t%v\n"+
39+
"Kubernetes:\t%v\n"+
40+
"Git Commit:\t%v\n"+
41+
"Build Date:\t%v\n"+
42+
"OS/Arch:\t%v/%v",
43+
v.KubeBuilderVersion,
44+
v.KubernetesVendor,
45+
v.GitCommit,
46+
v.BuildDate,
47+
v.GoOs,
48+
v.GoArch)
49+
}
50+
2551
func (c CLI) newVersionCmd() *cobra.Command {
2652
cmd := &cobra.Command{
2753
Use: "version",
2854
Short: fmt.Sprintf("Print the %s version", c.commandName),
2955
Long: fmt.Sprintf("Print the %s version", c.commandName),
3056
Example: fmt.Sprintf("%s version", c.commandName),
3157
RunE: func(_ *cobra.Command, _ []string) error {
32-
fmt.Printf(`Kubebuilder: %v
33-
Kubernetes: %v
34-
Git Commit: %v
35-
Build date: %v
36-
OS/Arch: %v/%v
37-
`,
38-
c.version["kubebuilder"],
39-
c.version["kubernetes"],
40-
c.version["gitCommit"],
41-
c.version["buildDate"],
42-
c.version["os"],
43-
c.version["arch"],
44-
)
58+
fmt.Println(c.version)
4559
return nil
4660
},
4761
}

0 commit comments

Comments
 (0)