Files
gas-stack/pkg/version/version.go
~wispem-wantex 171da4139b
Some checks failed
CI / build-docker (push) Successful in 4s
CI / build-docker-bootstrap (push) Has been skipped
CI / release-test (push) Failing after 13s
cmd: add a 'version' subcommand and flag to show the version
2026-09-21 17:28:59 -07:00

56 lines
1.6 KiB
Go

package version
import (
"fmt"
"runtime/debug"
)
// Version is stamped in at compile time by `ops/compile.sh`, via:
//
// -ldflags "-X git.offline-twitter.com/offline-labs/gas-stack/pkg/version.Version=v0.0.4"
//
// It's empty for builds that don't go through that script-- most importantly
// `go install git.offline-twitter.com/offline-labs/gas-stack/cmd@v0.0.4`, which is the normal way
// to install `gas`. In that case, String() recovers the version from the build info that the Go
// toolchain embeds in every binary.
var Version = ""
// String returns the version of this binary, e.g. "v0.0.4".
func String() string {
if Version != "" {
return Version
}
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
// Set by `go install module@version`, and (as of Go 1.24) by `go build` in a git checkout
// that has the tags available. It's "(devel)" if the toolchain couldn't work out a version.
if info.Main.Version != "" && info.Main.Version != "(devel)" {
return info.Main.Version
}
// No module version, so fall back to the commit. This happens when building from a source
// tree with no usable VCS metadata: a git worktree, a `.git`-less export, or `-buildvcs=false`.
var revision, dirty string
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
revision = setting.Value
case "vcs.modified":
if setting.Value == "true" {
dirty = "-dirty"
}
}
}
if revision == "" {
return "unknown"
}
if len(revision) > 12 {
revision = revision[:12]
}
return fmt.Sprintf("devel-%s%s", revision, dirty)
}