From 0f4bfebf0859042d680255743a03542d5f7da941 Mon Sep 17 00:00:00 2001 From: Charlie Drage Date: Tue, 7 Nov 2017 15:28:04 -0500 Subject: [PATCH] Move version information to separate file Moves the version information to a seperate file. This is mostly due to import cycle errors occuring when using "import github.com/kubernetes/kompose/cmd" in order to get the global variable of VERSION and GITCOMMIT. Update's the Makefile and other files accordingly. If the version and commmit is unretriveable due to not being able to find the kompose binary, the one from pkg/version/version.go will be used. --- Makefile | 2 +- cmd/version.go | 13 +++---------- pkg/transformer/utils.go | 12 ++++++++++-- pkg/version/version.go | 10 ++++++++++ script/release.sh | 4 ++-- 5 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 pkg/version/version.go diff --git a/Makefile b/Makefile index dc0e8e7c..b15920a3 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ GITCOMMIT := $(shell git rev-parse --short HEAD) -BUILD_FLAGS := -ldflags="-w -X github.com/kubernetes/kompose/cmd.GITCOMMIT=$(GITCOMMIT)" +BUILD_FLAGS := -ldflags="-w -X github.com/kubernetes/kompose/pkg/version.GITCOMMIT=$(GITCOMMIT)" PKGS = $(shell glide novendor) TEST_IMAGE := kompose/tests:latest diff --git a/cmd/version.go b/cmd/version.go index ac958c72..c2f4561a 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -19,24 +19,17 @@ package cmd import ( "fmt" + "github.com/kubernetes/kompose/pkg/version" "github.com/spf13/cobra" ) -var ( - // VERSION is version number that wil be displayed when running ./kompose version - VERSION = "1.4.0" - // GITCOMMIT is hash of the commit that wil be displayed when running ./kompose version - // this will be overwritten when running build like this: go build -ldflags="-X github.com/kubernetes/kompose/cmd.GITCOMMIT=$(GITCOMMIT)" - // HEAD is default indicating that this was not set during build - GITCOMMIT = "HEAD" -) - // versionCmd represents the version command var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version of Kompose", Run: func(cmd *cobra.Command, args []string) { - fmt.Println(VERSION + " (" + GITCOMMIT + ")") + // See pkg/version/version.go for more information as to why we use the git commit / hash value + fmt.Println(version.VERSION + " (" + version.GITCOMMIT + ")") }, } diff --git a/pkg/transformer/utils.go b/pkg/transformer/utils.go index 08937405..d5568469 100644 --- a/pkg/transformer/utils.go +++ b/pkg/transformer/utils.go @@ -31,6 +31,8 @@ import ( "github.com/kubernetes/kompose/pkg/utils/docker" + "github.com/kubernetes/kompose/pkg/version" + "github.com/pkg/errors" "k8s.io/kubernetes/pkg/api" ) @@ -117,13 +119,19 @@ func ConfigAnnotations(service kobject.ServiceConfig) map[string]string { annotations[key] = value } annotations["kompose.cmd"] = strings.Join(os.Args, " ") - version := exec.Command("kompose", "version") - out, err := version.Output() + versionCmd := exec.Command("kompose", "version") + out, err := versionCmd.Output() if err != nil { errors.Wrap(err, "Failed to get kompose version") } annotations["kompose.version"] = strings.Trim(string(out), " \n") + + // If the version is blank (couldn't retrieve the kompose version for whatever reason) + if annotations["kompose.version"] == "" { + annotations["kompose.version"] = version.VERSION + " (" + version.GITCOMMIT + ")" + } + return annotations } diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 00000000..5a1d3e78 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,10 @@ +package version + +var ( + // VERSION is version number that wil be displayed when running ./kompose version + VERSION = "1.4.0" + // GITCOMMIT is hash of the commit that wil be displayed when running ./kompose version + // this will be overwritten when running build like this: go build -ldflags="-X github.com/kubernetes/kompose/pkg/version.GITCOMMIT=$(GITCOMMIT)" + // HEAD is default indicating that this was not set during build + GITCOMMIT = "HEAD" +) diff --git a/script/release.sh b/script/release.sh index 71fd7424..c5fd236e 100755 --- a/script/release.sh +++ b/script/release.sh @@ -91,8 +91,8 @@ init_sync() { } replaceversion() { - echo "Replaced version in version.go" - sed -i "s/$1/$2/g" cmd/version.go + echo "Replaced version in pkg/version/version.go" + sed -i "s/$1/$2/g" pkg/version/version.go echo "Replaced version in README.md" sed -i "s/$1/$2/g" README.md