Stamp shared build metadata into banger binaries

Treat `banger`, `bangerd`, and `banger-vsock-agent` as one release by
stamping the same version, commit SHA, and build timestamp into every
binary through a shared ldflag-backed `internal/buildinfo` package.

Add `banger version`, extend daemon ping/status to report the running
daemon's build tuple, and keep the guest helper linked to the same build
metadata without adding a new public version surface for it.

Validate with `GOCACHE=/tmp/banger-gocache go test ./...`, `make build`,
`./build/bin/banger version`, and `./build/bin/banger daemon status`
after the daemon restarts onto the new binary.
This commit is contained in:
Thales Maciel 2026-03-22 17:14:06 -03:00
parent ea2db1e868
commit f798e1db33
No known key found for this signature in database
GPG key ID: 33112E6833C34679
9 changed files with 219 additions and 13 deletions

View file

@ -0,0 +1,34 @@
package buildinfo
import "strings"
var (
Version = "dev"
Commit = "unknown"
BuiltAt = "unknown"
)
type Info struct {
Version string
Commit string
BuiltAt string
}
func Current() Info {
return Normalize(Version, Commit, BuiltAt)
}
func Normalize(version, commit, builtAt string) Info {
return Info{
Version: normalizedValue(version, "dev"),
Commit: normalizedValue(commit, "unknown"),
BuiltAt: normalizedValue(builtAt, "unknown"),
}
}
func normalizedValue(value, fallback string) string {
if trimmed := strings.TrimSpace(value); trimmed != "" {
return trimmed
}
return fallback
}

View file

@ -0,0 +1,33 @@
package buildinfo
import "testing"
func TestNormalizeUsesFallbacks(t *testing.T) {
t.Parallel()
info := Normalize("", " ", "\t")
if info.Version != "dev" {
t.Fatalf("Version = %q, want dev", info.Version)
}
if info.Commit != "unknown" {
t.Fatalf("Commit = %q, want unknown", info.Commit)
}
if info.BuiltAt != "unknown" {
t.Fatalf("BuiltAt = %q, want unknown", info.BuiltAt)
}
}
func TestNormalizeTrimsValues(t *testing.T) {
t.Parallel()
info := Normalize(" v1.2.3 ", " abc123 ", " 2026-03-22T12:00:00Z ")
if info.Version != "v1.2.3" {
t.Fatalf("Version = %q, want v1.2.3", info.Version)
}
if info.Commit != "abc123" {
t.Fatalf("Commit = %q, want abc123", info.Commit)
}
if info.BuiltAt != "2026-03-22T12:00:00Z" {
t.Fatalf("BuiltAt = %q, want 2026-03-22T12:00:00Z", info.BuiltAt)
}
}