version.go

 1package version
 2
 3import "runtime/debug"
 4
 5// Build-time parameters set via -ldflags
 6
 7var (
 8	Version = "unknown"
 9	Commit  = "unknown"
10)
11
12// A user may install crush using `go install github.com/charmbracelet/crush@latest`.
13// without -ldflags, in which case the version above is unset. As a workaround
14// we use the embedded build version that *is* set when using `go install` (and
15// is only set for `go install` and not for `go build`).
16func init() {
17	info, ok := debug.ReadBuildInfo()
18	if !ok {
19		// < go v1.18
20		return
21	}
22	mainVersion := info.Main.Version
23	if mainVersion == "" || mainVersion == "(devel)" {
24		// bin not built using `go install`
25		return
26	}
27	// bin built using `go install`
28	Version = mainVersion
29}