Skip to content

Commit e3072e5

Browse files
[Improvement] Infer version information for go-install
Signed-off-by: Miguel Elias dos Santos <migueleliasweb@gmail.com>
1 parent c73f506 commit e3072e5

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

cmd/version.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818

1919
import (
2020
"fmt"
21+
"runtime"
2122
"runtime/debug"
2223
)
2324

@@ -27,38 +28,54 @@ const unknown = "unknown"
2728
// information in the release process
2829
var (
2930
kubeBuilderVersion = unknown
30-
kubernetesVendorVersion = unknown
31-
goos = unknown
32-
goarch = unknown
33-
gitCommit = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
34-
35-
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
31+
kubernetesVendorVersion = "1.32.1"
32+
goVersion = runtime.Version()
33+
goOs = runtime.GOOS
34+
goArch = runtime.GOARCH
35+
gitCommit = unknown // "$Format:%H$" sha1 from git, output of $(git rev-parse HEAD)
36+
buildDate = unknown // "1970-01-01T00:00:00Z" build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
3637
)
3738

3839
// version contains all the information related to the CLI version
3940
type version struct {
4041
KubeBuilderVersion string `json:"kubeBuilderVersion"`
4142
KubernetesVendor string `json:"kubernetesVendor"`
42-
GitCommit string `json:"gitCommit"`
43-
BuildDate string `json:"buildDate"`
43+
GoVersion string `json:"goVersion"`
4444
GoOs string `json:"goOs"`
4545
GoArch string `json:"goArch"`
46+
GitCommit string `json:"gitCommit"`
47+
BuildDate string `json:"buildDate"`
4648
}
4749

4850
// versionString returns the CLI version
4951
func versionString() string {
50-
if kubeBuilderVersion == unknown {
51-
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
52-
kubeBuilderVersion = info.Main.Version
52+
info, ok := debug.ReadBuildInfo()
53+
54+
if ok {
55+
if info.Main.Version != "" {
56+
if kubeBuilderVersion == unknown {
57+
kubeBuilderVersion = info.Main.Version
58+
}
59+
}
60+
61+
for _, setting := range info.Settings {
62+
if buildDate == unknown && setting.Key == "vcs.revision" {
63+
buildDate = setting.Value
64+
}
65+
66+
if gitCommit == unknown && setting.Key == "vcs.revision" {
67+
gitCommit = setting.Value
68+
}
5369
}
5470
}
5571

5672
return fmt.Sprintf("Version: %#v", version{
5773
kubeBuilderVersion,
5874
kubernetesVendorVersion,
75+
goVersion,
76+
goOs,
77+
goArch,
5978
gitCommit,
6079
buildDate,
61-
goos,
62-
goarch,
6380
})
6481
}

0 commit comments

Comments
 (0)