From ad8c4e59e6777e60832b61407d3a0549418c5374 Mon Sep 17 00:00:00 2001 From: Carlos Panato Date: Sun, 8 Aug 2021 15:11:45 +0200 Subject: [PATCH] version: add way to display a version when using go get or go install Signed-off-by: Carlos Panato --- Makefile | 4 ++-- cmd/rekor-cli/app/root.go | 14 ++++++++++++++ cmd/rekor-cli/app/version.go | 4 ++-- cmd/rekor-server/app/root.go | 14 ++++++++++++++ cmd/rekor-server/app/version.go | 4 ++-- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index cb3f873ca..a561835ed 100644 --- a/Makefile +++ b/Makefile @@ -44,10 +44,10 @@ endif SWAGGER := $(TOOLS_BIN_DIR)/swagger CLI_PKG=github.com/sigstore/rekor/cmd/rekor-cli/app -CLI_LDFLAGS="-X $(CLI_PKG).gitVersion=$(GIT_VERSION) -X $(CLI_PKG).gitCommit=$(GIT_HASH) -X $(CLI_PKG).gitTreeState=$(GIT_TREESTATE) -X $(CLI_PKG).buildDate=$(BUILD_DATE)" +CLI_LDFLAGS="-X $(CLI_PKG).GitVersion=$(GIT_VERSION) -X $(CLI_PKG).gitCommit=$(GIT_HASH) -X $(CLI_PKG).gitTreeState=$(GIT_TREESTATE) -X $(CLI_PKG).buildDate=$(BUILD_DATE)" SERVER_PKG=github.com/sigstore/rekor/cmd/rekor-server/app -SERVER_LDFLAGS="-X $(SERVER_PKG).gitVersion=$(GIT_VERSION) -X $(SERVER_PKG).gitCommit=$(GIT_HASH) -X $(SERVER_PKG).gitTreeState=$(GIT_TREESTATE) -X $(SERVER_PKG).buildDate=$(BUILD_DATE)" +SERVER_LDFLAGS="-X $(SERVER_PKG).GitVersion=$(GIT_VERSION) -X $(SERVER_PKG).gitCommit=$(GIT_HASH) -X $(SERVER_PKG).gitTreeState=$(GIT_TREESTATE) -X $(SERVER_PKG).buildDate=$(BUILD_DATE)" $(GENSRC): $(SWAGGER) $(OPENAPIDEPS) $(SWAGGER) generate client -f openapi.yaml -q -r COPYRIGHT.txt -t pkg/generated --default-consumes application/json\;q=1 diff --git a/cmd/rekor-cli/app/root.go b/cmd/rekor-cli/app/root.go index 67f2333c0..fa9950490 100644 --- a/cmd/rekor-cli/app/root.go +++ b/cmd/rekor-cli/app/root.go @@ -17,6 +17,7 @@ package app import ( "fmt" + "runtime/debug" "strings" homedir "github.com/mitchellh/go-homedir" @@ -67,6 +68,19 @@ func init() { if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil { log.CliLogger.Fatal(err) } + + // look for the default version and replace it, if found, from runtime build info + if GitVersion != "devel" { + return + } + + bi, ok := debug.ReadBuildInfo() + if !ok { + return + } + // Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-cli/app.GitVersion=1.2.3 + // Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-cli + GitVersion = bi.Main.Version } func initConfig(cmd *cobra.Command) error { diff --git a/cmd/rekor-cli/app/version.go b/cmd/rekor-cli/app/version.go index 950f49410..172093a96 100644 --- a/cmd/rekor-cli/app/version.go +++ b/cmd/rekor-cli/app/version.go @@ -33,7 +33,7 @@ import ( var ( // Output of "git describe". The prerequisite is that the branch should be // tagged using the correct versioning strategy. - gitVersion = "unknown" + GitVersion string = "devel" // SHA1 from git, output of $(git rev-parse HEAD) gitCommit = "unknown" // State of git tree, either "clean" or "dirty" @@ -93,7 +93,7 @@ func VersionInfo() Info { // These variables typically come from -ldflags settings and in // their absence fallback to the global defaults set above. return Info{ - GitVersion: gitVersion, + GitVersion: GitVersion, GitCommit: gitCommit, GitTreeState: gitTreeState, BuildDate: buildDate, diff --git a/cmd/rekor-server/app/root.go b/cmd/rekor-server/app/root.go index 23bf6c246..9f53fa94f 100644 --- a/cmd/rekor-server/app/root.go +++ b/cmd/rekor-server/app/root.go @@ -18,6 +18,7 @@ package app import ( "fmt" "os" + "runtime/debug" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" @@ -79,6 +80,19 @@ func init() { } rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + + // look for the default version and replace it, if found, from runtime build info + if GitVersion != "devel" { + return + } + + bi, ok := debug.ReadBuildInfo() + if !ok { + return + } + // Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-server/app.GitVersion=1.2.3 + // Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-server + GitVersion = bi.Main.Version } // initConfig reads in config file and ENV variables if set. diff --git a/cmd/rekor-server/app/version.go b/cmd/rekor-server/app/version.go index ded30e068..917e910cf 100644 --- a/cmd/rekor-server/app/version.go +++ b/cmd/rekor-server/app/version.go @@ -33,7 +33,7 @@ import ( var ( // Output of "git describe". The prerequisite is that the branch should be // tagged using the correct versioning strategy. - gitVersion = "unknown" + GitVersion string = "devel" // SHA1 from git, output of $(git rev-parse HEAD) gitCommit = "unknown" // State of git tree, either "clean" or "dirty" @@ -93,7 +93,7 @@ func VersionInfo() Info { // These variables typically come from -ldflags settings and in // their absence fallback to the global defaults set above. return Info{ - GitVersion: gitVersion, + GitVersion: GitVersion, GitCommit: gitCommit, GitTreeState: gitTreeState, BuildDate: buildDate,