From 57a4192f8a90b8e2446cb360431023f8ec3bfcf2 Mon Sep 17 00:00:00 2001
From: bryanl <bryanliles@gmail.com>
Date: Fri, 12 Jan 2018 00:05:47 -0500
Subject: [PATCH] Construct apimachinery version

Using `Version()` as a string leaves a preprocessed version tag in the
ksonnet version. Construct a new version using the `Major` and `Minor`
fields. Adds short SHA1 from k8s.io/apimachinery as well. SHA1 is pulled
from `dep status`.

Re: #250

Signed-off-by: bryanl <bryanliles@gmail.com>
---
 Makefile       |  4 +++-
 cmd/version.go | 10 +++++++++-
 main.go        |  2 ++
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 234272dc..06e6e3ec 100644
--- a/Makefile
+++ b/Makefile
@@ -16,9 +16,11 @@
 VERSION?=dev-$(shell date +%FT%T%z)
 KS_BIN?=ks
 
+APIMACHINERY_VER := $(shell dep status | grep k8s.io/apimachinery | awk '{print $$3}')
+
 GO = go
 EXTRA_GO_FLAGS =
-GO_FLAGS = -ldflags="-X main.version=$(VERSION) $(GO_LDFLAGS)" $(EXTRA_GO_FLAGS)
+GO_FLAGS = -ldflags="-X main.version=$(VERSION) -X main.apimachineryVersion=$(APIMACHINERY_VER) $(GO_LDFLAGS)" $(EXTRA_GO_FLAGS)
 GOFMT = gofmt
 # GINKGO = "go test" also works if you want to avoid ginkgo tool
 GINKGO = ginkgo
diff --git a/cmd/version.go b/cmd/version.go
index 190ba723..e82f465d 100644
--- a/cmd/version.go
+++ b/cmd/version.go
@@ -30,14 +30,22 @@ func init() {
 // Version is overridden by main
 var Version = "(dev build)"
 
+var APImachineryVersion = ""
+
 var versionCmd = &cobra.Command{
 	Use:   "version",
 	Short: "Print version information for this ksonnet binary",
 	Run: func(cmd *cobra.Command, args []string) {
 		out := cmd.OutOrStdout()
+
+		clientGoVersion := fmt.Sprintf("%s.%s", version.Get().Major, version.Get().Minor)
+		if APImachineryVersion != "" {
+			clientGoVersion += fmt.Sprintf("-%s", APImachineryVersion)
+		}
+
 		fmt.Fprintln(out, "ksonnet version:", Version)
 		fmt.Fprintln(out, "jsonnet version:", jsonnet.Version())
-		fmt.Fprintln(out, "client-go version:", version.Get())
+		fmt.Fprintln(out, "client-go version:", clientGoVersion)
 	},
 	Long: `
 The ` + "`version`" + ` command prints out version info about the current ksonnet CLI,
diff --git a/main.go b/main.go
index 767f3dfa..df5baa9d 100644
--- a/main.go
+++ b/main.go
@@ -26,9 +26,11 @@ import (
 
 // Version is overridden using `-X main.version` during release builds
 var version = "(dev build)"
+var apimachineryVersion = ""
 
 func main() {
 	cmd.Version = version
+	cmd.APImachineryVersion = apimachineryVersion
 
 	if err := cmd.RootCmd.Execute(); err != nil {
 		// PersistentPreRunE may not have been run for early
-- 
GitLab