Skip to content

Commit abecdc7

Browse files
authored
version: add way to display a version when using go get or go install (sigstore#405)
Signed-off-by: Carlos Panato <ctadeu@gmail.com>
1 parent f801aaf commit abecdc7

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ endif
4444
SWAGGER := $(TOOLS_BIN_DIR)/swagger
4545

4646
CLI_PKG=github.com/sigstore/rekor/cmd/rekor-cli/app
47-
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)"
47+
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)"
4848

4949
SERVER_PKG=github.com/sigstore/rekor/cmd/rekor-server/app
50-
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)"
50+
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)"
5151

5252
$(GENSRC): $(SWAGGER) $(OPENAPIDEPS)
5353
$(SWAGGER) generate client -f openapi.yaml -q -r COPYRIGHT.txt -t pkg/generated --default-consumes application/json\;q=1

cmd/rekor-cli/app/root.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package app
1717

1818
import (
1919
"fmt"
20+
"runtime/debug"
2021
"strings"
2122

2223
homedir "github.com/mitchellh/go-homedir"
@@ -67,6 +68,19 @@ func init() {
6768
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
6869
log.CliLogger.Fatal(err)
6970
}
71+
72+
// look for the default version and replace it, if found, from runtime build info
73+
if GitVersion != "devel" {
74+
return
75+
}
76+
77+
bi, ok := debug.ReadBuildInfo()
78+
if !ok {
79+
return
80+
}
81+
// Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-cli/app.GitVersion=1.2.3
82+
// Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-cli
83+
GitVersion = bi.Main.Version
7084
}
7185

7286
func initConfig(cmd *cobra.Command) error {

cmd/rekor-cli/app/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
var (
3434
// Output of "git describe". The prerequisite is that the branch should be
3535
// tagged using the correct versioning strategy.
36-
gitVersion = "unknown"
36+
GitVersion string = "devel"
3737
// SHA1 from git, output of $(git rev-parse HEAD)
3838
gitCommit = "unknown"
3939
// State of git tree, either "clean" or "dirty"
@@ -93,7 +93,7 @@ func VersionInfo() Info {
9393
// These variables typically come from -ldflags settings and in
9494
// their absence fallback to the global defaults set above.
9595
return Info{
96-
GitVersion: gitVersion,
96+
GitVersion: GitVersion,
9797
GitCommit: gitCommit,
9898
GitTreeState: gitTreeState,
9999
BuildDate: buildDate,

cmd/rekor-server/app/root.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package app
1818
import (
1919
"fmt"
2020
"os"
21+
"runtime/debug"
2122

2223
homedir "github.com/mitchellh/go-homedir"
2324
"github.com/spf13/cobra"
@@ -79,6 +80,19 @@ func init() {
7980
}
8081

8182
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
83+
84+
// look for the default version and replace it, if found, from runtime build info
85+
if GitVersion != "devel" {
86+
return
87+
}
88+
89+
bi, ok := debug.ReadBuildInfo()
90+
if !ok {
91+
return
92+
}
93+
// Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-server/app.GitVersion=1.2.3
94+
// Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-server
95+
GitVersion = bi.Main.Version
8296
}
8397

8498
// initConfig reads in config file and ENV variables if set.

cmd/rekor-server/app/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
var (
3434
// Output of "git describe". The prerequisite is that the branch should be
3535
// tagged using the correct versioning strategy.
36-
gitVersion = "unknown"
36+
GitVersion string = "devel"
3737
// SHA1 from git, output of $(git rev-parse HEAD)
3838
gitCommit = "unknown"
3939
// State of git tree, either "clean" or "dirty"
@@ -93,7 +93,7 @@ func VersionInfo() Info {
9393
// These variables typically come from -ldflags settings and in
9494
// their absence fallback to the global defaults set above.
9595
return Info{
96-
GitVersion: gitVersion,
96+
GitVersion: GitVersion,
9797
GitCommit: gitCommit,
9898
GitTreeState: gitTreeState,
9999
BuildDate: buildDate,

0 commit comments

Comments
 (0)