mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
6c2da4b2f5
Add version and commit_hash fields to the /ping endpoint response, sourced from the existing internal/version package (ldflags or Go build info). The frontend capabilities store reads these values and displays them as badges at the bottom of the Profile page.
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package version
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime/debug"
|
|
)
|
|
|
|
var (
|
|
// Version is the current version of the application.
|
|
// It can be overridden by ldflags at build time.
|
|
Version = "dev"
|
|
// CommitHash is the git commit hash at build time.
|
|
// It can be overridden by ldflags at build time.
|
|
CommitHash = ""
|
|
// BuildTime is the time when the application was built.
|
|
// It can be overridden by ldflags at build time.
|
|
BuildTime = ""
|
|
)
|
|
|
|
// EnsureBuildInfo populates CommitHash and BuildTime from Go build info
|
|
// when they were not set via ldflags.
|
|
func EnsureBuildInfo() {
|
|
if CommitHash == "" {
|
|
if info, ok := debug.ReadBuildInfo(); ok {
|
|
for _, setting := range info.Settings {
|
|
if setting.Key == "vcs.revision" {
|
|
CommitHash = setting.Value
|
|
}
|
|
if setting.Key == "vcs.time" {
|
|
BuildTime = setting.Value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ShortCommitHash returns the first 7 characters of the commit hash.
|
|
func ShortCommitHash() string {
|
|
EnsureBuildInfo()
|
|
if len(CommitHash) > 7 {
|
|
return CommitHash[:7]
|
|
}
|
|
return CommitHash
|
|
}
|
|
|
|
// GetInfo returns a formatted version string including the version and commit hash.
|
|
func GetInfo() string {
|
|
EnsureBuildInfo()
|
|
res := Version
|
|
if h := ShortCommitHash(); h != "" {
|
|
res += fmt.Sprintf(" (%s)", h)
|
|
}
|
|
return res
|
|
}
|