Skip to content

Commit fcb89ed

Browse files
author
ctm
committed
feat: auto-detect version via debug.ReadBuildInfo
Replaces the hardcoded Version = "0.1.0" with runtime detection from Go module build info. 'go install ...@vX.Y.Z' now correctly reports vX.Y.Z, local source builds show a pseudo-version with commit hash, and ldflags override still works for custom distributions.
1 parent b02235b commit fcb89ed

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

cmd/root.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@ package cmd
22

33
import (
44
"os"
5+
"runtime/debug"
56

67
"github.com/spf13/cobra"
78
)
89

9-
var Version = "0.1.0"
10+
// Version is the ctm version string. It is resolved at runtime from Go's
11+
// build info when installed via `go install github.com/RandomCodeSpace/ctm@vX.Y.Z`
12+
// (Go module metadata carries the tag). For local `go build` from source,
13+
// debug.ReadBuildInfo returns "(devel)" and we fall back to "dev".
14+
//
15+
// This variable is also overridable at build time via ldflags for custom
16+
// distributions:
17+
//
18+
// go build -ldflags "-X github.com/RandomCodeSpace/ctm/cmd.Version=v1.2.3"
19+
var Version = resolveVersion()
20+
21+
func resolveVersion() string {
22+
info, ok := debug.ReadBuildInfo()
23+
if !ok {
24+
return "dev"
25+
}
26+
v := info.Main.Version
27+
if v == "" || v == "(devel)" {
28+
return "dev"
29+
}
30+
return v
31+
}
1032

1133
var Verbose bool
1234

0 commit comments

Comments
 (0)