Files
2026-08-15 07:13:00 +00:00

70 lines
1.3 KiB
Go

// Package version exposes build metadata injected at link time.
package version
import (
"fmt"
"runtime"
"runtime/debug"
"sync"
)
// Injected via -ldflags "-X github.com/iceBear67/simplepages/internal/version.Version=...".
var (
Version = "dev"
Commit = ""
Date = ""
)
var once sync.Once
// vcsFromBuildInfo fills Commit/Date from the embedded build info when the
// linker flags were not supplied (the usual case for `go run` and `go install`).
func vcsFromBuildInfo() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
if Commit == "" {
Commit = s.Value
}
case "vcs.time":
if Date == "" {
Date = s.Value
}
}
}
}
// String renders a one-line human-readable version banner.
func String() string {
once.Do(vcsFromBuildInfo)
s := Version
if Commit != "" {
short := Commit
if len(short) > 12 {
short = short[:12]
}
s += "+" + short
}
if Date != "" {
s += " (" + Date + ")"
}
return fmt.Sprintf("%s %s/%s %s", s, runtime.GOOS, runtime.GOARCH, runtime.Version())
}
// Short returns just the version string, for API responses.
func Short() string {
once.Do(vcsFromBuildInfo)
if Commit == "" {
return Version
}
short := Commit
if len(short) > 12 {
short = short[:12]
}
return Version + "+" + short
}