Add runtime version reporting
This commit is contained in:
26
internal/runtimeinfo/version.go
Normal file
26
internal/runtimeinfo/version.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Package runtimeinfo implements the private runtime version state used by the
|
||||
// public buildinfo facade.
|
||||
package runtimeinfo
|
||||
|
||||
import "runtime/debug"
|
||||
|
||||
var injectedVersion string
|
||||
|
||||
// Version returns the injected version or the running executable's main module
|
||||
// version, falling back to "dev" when neither provides a release identifier.
|
||||
func Version() string {
|
||||
return version(injectedVersion, debug.ReadBuildInfo)
|
||||
}
|
||||
|
||||
func version(injected string, readBuildInfo func() (*debug.BuildInfo, bool)) string {
|
||||
if injected != "" {
|
||||
return injected
|
||||
}
|
||||
|
||||
info, ok := readBuildInfo()
|
||||
if ok && info != nil && info.Main.Version != "" && info.Main.Version != "(devel)" {
|
||||
return info.Main.Version
|
||||
}
|
||||
|
||||
return "dev"
|
||||
}
|
||||
57
internal/runtimeinfo/version_test.go
Normal file
57
internal/runtimeinfo/version_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package runtimeinfo
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
injected string
|
||||
info *debug.BuildInfo
|
||||
ok bool
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "injected version takes precedence",
|
||||
injected: "v1.2.3",
|
||||
info: &debug.BuildInfo{Main: debug.Module{Version: "v4.5.6"}},
|
||||
ok: true,
|
||||
want: "v1.2.3",
|
||||
},
|
||||
{
|
||||
name: "main module version",
|
||||
info: &debug.BuildInfo{Main: debug.Module{Version: "v4.5.6"}},
|
||||
ok: true,
|
||||
want: "v4.5.6",
|
||||
},
|
||||
{
|
||||
name: "missing build information",
|
||||
want: "dev",
|
||||
},
|
||||
{
|
||||
name: "development main module",
|
||||
info: &debug.BuildInfo{Main: debug.Module{Version: "(devel)"}},
|
||||
ok: true,
|
||||
want: "dev",
|
||||
},
|
||||
{
|
||||
name: "empty main module version",
|
||||
info: &debug.BuildInfo{Main: debug.Module{}},
|
||||
ok: true,
|
||||
want: "dev",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got := version(test.injected, func() (*debug.BuildInfo, bool) {
|
||||
return test.info, test.ok
|
||||
})
|
||||
if got != test.want {
|
||||
t.Fatalf("version() = %q, want %q", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
7
internal/versioncontract/injection.go
Normal file
7
internal/versioncontract/injection.go
Normal file
@@ -0,0 +1,7 @@
|
||||
// Package versioncontract holds the internal contract shared by runtime
|
||||
// version reporting and the release builder.
|
||||
package versioncontract
|
||||
|
||||
// InjectedVersionLinkerSymbol is the -X linker target used to set the runtime
|
||||
// version. Keep it in sync with runtimeinfo.injectedVersion.
|
||||
const InjectedVersionLinkerSymbol = "gitea.maximumdirect.net/eric/ReleaseKit/internal/runtimeinfo.injectedVersion"
|
||||
Reference in New Issue
Block a user