Skip to content

Commit 2a365ac

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

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

cmd/version.go

Lines changed: 31 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,55 @@ 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+
62+
for _, setting := range info.Settings {
63+
if buildDate == unknown && setting.Key == "vcs.revision" {
64+
buildDate = setting.Value
65+
}
66+
67+
if gitCommit == unknown && setting.Key == "vcs.revision" {
68+
gitCommit = setting.Value
69+
}
5370
}
5471
}
5572

5673
return fmt.Sprintf("Version: %#v", version{
5774
kubeBuilderVersion,
5875
kubernetesVendorVersion,
76+
goVersion,
77+
goOs,
78+
goArch,
5979
gitCommit,
6080
buildDate,
61-
goos,
62-
goarch,
6381
})
6482
}

0 commit comments

Comments
 (0)