Skip to content

Commit

Permalink
version: add way to display a version when using go get or go install (
Browse files Browse the repository at this point in the history
…#405)

Signed-off-by: Carlos Panato <ctadeu@gmail.com>
  • Loading branch information
cpanato authored Aug 8, 2021
1 parent f801aaf commit abecdc7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions cmd/rekor-cli/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package app

import (
"fmt"
"runtime/debug"
"strings"

homedir "github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/rekor-cli/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions cmd/rekor-server/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package app
import (
"fmt"
"os"
"runtime/debug"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cmd/rekor-server/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit abecdc7

Please sign in to comment.