diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 1bedf4a..02b403c 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -21,6 +21,7 @@ builds: mod_timestamp: "{{ .CommitTimestamp }}" ldflags: - -s -w + - -X main.Version={{.Version}} archives: - formats: tar.gz diff --git a/main.go b/main.go index de5d7d1..3cd5ada 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "os" "os/exec" "runtime" + "runtime/debug" "runtime/pprof" "slices" "sort" @@ -42,6 +43,17 @@ const ( zeroOIDSHA256 = "0000000000000000000000000000000000000000000000000000000000000000" ) +var Version = "dev" + +func init() { + if Version != "dev" { + return + } + if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "" && bi.Main.Version != "(devel)" { + Version = bi.Main.Version + } +} + var ( cpuProfile = newOption("") memProfile = newOption("") @@ -69,6 +81,7 @@ func newRootCommand() *cobra.Command { root := &cobra.Command{ Use: "git-spdx", Short: "Find license changes across Git history", + Version: Version, SilenceErrors: true, SilenceUsage: true, CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true}, diff --git a/main_test.go b/main_test.go index f89c1f3..5950967 100644 --- a/main_test.go +++ b/main_test.go @@ -459,6 +459,13 @@ func cli(t *testing.T, args ...string) string { return string(out) } +func TestCLIVersion(t *testing.T) { + out := cli(t, "--version") + if !strings.HasPrefix(out, "git-spdx version ") { + t.Fatalf("--version output = %q", out) + } +} + func TestCLI(t *testing.T) { repo := repository(t) commitFile(t, repo, "source.go", "// SPDX-License-Identifier: MIT\npackage example\n", "Add source")