version.go

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