Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version: add way to display a version when using go get or go install #526

Merged
merged 1 commit into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ endif

PKG=github.com/sigstore/cosign/cmd/cosign/cli

LDFLAGS="-X $(PKG).gitVersion=$(GIT_VERSION) -X $(PKG).gitCommit=$(GIT_HASH) -X $(PKG).gitTreeState=$(GIT_TREESTATE) -X $(PKG).buildDate=$(BUILD_DATE)"
LDFLAGS="-X $(PKG).GitVersion=$(GIT_VERSION) -X $(PKG).gitCommit=$(GIT_HASH) -X $(PKG).gitTreeState=$(GIT_TREESTATE) -X $(PKG).buildDate=$(BUILD_DATE)"

.PHONY: all lint test clean cosign cross

Expand Down
6 changes: 2 additions & 4 deletions cmd/cosign/cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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 @@ -82,10 +82,8 @@ type Info struct {
}

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
21 changes: 19 additions & 2 deletions cmd/cosign/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"flag"
"fmt"
"os"
"runtime/debug"

"github.com/google/go-containerregistry/pkg/logs"
"github.com/peterbourgon/ff/v3/ffcli"
Expand All @@ -34,7 +35,7 @@ import (

var (
rootFlagSet = flag.NewFlagSet("cosign", flag.ExitOnError)
debug = rootFlagSet.Bool("d", false, "log debug output")
logDebug = rootFlagSet.Bool("d", false, "log debug output")
outputFilename = rootFlagSet.String("output-file", "", "log output to a file")
)

Expand Down Expand Up @@ -93,7 +94,7 @@ func main() {
os.Stdout = out
}

if *debug {
if *logDebug {
logs.Debug.SetOutput(os.Stderr)
}

Expand All @@ -106,3 +107,19 @@ func printErrAndExit(err error) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

func init() {
// look for the default version and replace it, if found, from runtime build info
if cli.GitVersion != "devel" {
return
}

bi, ok := debug.ReadBuildInfo()
if !ok {
return
}

// Version is set in artifacts built with -X github.com/sigstore/cosign/cli.GitVersion=1.2.3
// Ensure version is also set when installed via go install github.com/sigstore/cosign/cmd/cosign
cli.GitVersion = bi.Main.Version
}