Skip to content
Snippets Groups Projects
Unverified Commit abecdc71 authored by Carlos Tadeu Panato Junior's avatar Carlos Tadeu Panato Junior Committed by GitHub
Browse files

version: add way to display a version when using go get or go install (#405)


Signed-off-by: default avatarCarlos Panato <ctadeu@gmail.com>
parent f801aaf8
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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 {
......
......@@ -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,
......
......@@ -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.
......
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment