feat(deploy): 在线更新 - 新增后台检查、强制备份迁移与失败自动回退
This commit is contained in:
1 parent
8227921c68
commit
694cb974fb
60 files changed
+4124
-93
No files matched your search
@@ -0,0 +1,49 @@
|
||||
package buildinfo
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
Version = "dev"
|
||||
Commit = "unknown"
|
||||
BuildTime = "unknown"
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
Version string `json:"version"`
|
||||
Commit string `json:"commit"`
|
||||
BuildTime string `json:"buildTime"`
|
||||
GoVersion string `json:"goVersion"`
|
||||
}
|
||||
|
||||
func Current() Info {
|
||||
version := normalized(Version, "dev")
|
||||
commit := normalized(Commit, "unknown")
|
||||
buildTime := normalized(BuildTime, "unknown")
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
for _, setting := range info.Settings {
|
||||
switch setting.Key {
|
||||
case "vcs.revision":
|
||||
if commit == "unknown" {
|
||||
commit = normalized(setting.Value, commit)
|
||||
}
|
||||
case "vcs.time":
|
||||
if buildTime == "unknown" {
|
||||
buildTime = normalized(setting.Value, buildTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Info{Version: version, Commit: commit, BuildTime: buildTime, GoVersion: runtime.Version()}
|
||||
}
|
||||
|
||||
func normalized(value string, fallback string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package buildinfo
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCurrentUsesInjectedValues(t *testing.T) {
|
||||
previousVersion, previousCommit, previousBuildTime := Version, Commit, BuildTime
|
||||
t.Cleanup(func() { Version, Commit, BuildTime = previousVersion, previousCommit, previousBuildTime })
|
||||
Version, Commit, BuildTime = " v1.2.3 ", " abc123 ", " 2026-08-30T00:00:00Z "
|
||||
|
||||
info := Current()
|
||||
if info.Version != "v1.2.3" || info.Commit != "abc123" || info.BuildTime != "2026-08-30T00:00:00Z" {
|
||||
t.Fatalf("unexpected build info: %#v", info)
|
||||
}
|
||||
if info.GoVersion == "" {
|
||||
t.Fatal("Go version should be populated")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user