Files
query-orchestration/internal/serviceconfig/build/build.go
T
Michael McGuinness bbd86fb4ea Merged in feature/CollectorBuildVersion (pull request #100)
Collector Build Version

* lint

* fixtests
2025-03-11 18:15:49 +00:00

77 lines
1.6 KiB
Go

package build
import (
"fmt"
"runtime/debug"
"sync"
"time"
)
const (
SystemName = "doczy"
)
func IsValidUnixVersion(v int64) error {
currentVersion := GetVersionUnixTimestamp()
if v < 1 || v > currentVersion {
return fmt.Errorf("version must be in the range 0 < version <= %d", currentVersion)
}
return nil
}
var startupTime time.Time
var startupTimeOnce sync.Once
func GetVersionTimestamp() time.Time {
startupTimeOnce.Do(func() {
info, ok := debug.ReadBuildInfo()
if !ok {
startupTime = time.Now()
return
}
// Look for vcs.time in build settings
for _, setting := range info.Settings {
if setting.Key == "vcs.time" {
// Parse the build timestamp
buildTime, err := time.Parse(time.RFC3339, setting.Value)
if err == nil {
startupTime = buildTime
return
}
break
}
}
// Return current time if no build time found
startupTime = time.Now()
})
return startupTime
}
func GetVersionUnixTimestamp() int64 {
return GetVersionTimestamp().UnixMilli()
}
// GetVersion returns the version of the application in the Golang build info format.
// This is a raw format and will change depending on the build environment and the git tags.
// When building from a commit with an associated Git tag (e.g., v1.2.3),
//
// the runtime version uses the tag name without a timestamp.
//
// The "+dirty" suffix is appended when there are uncommitted changes
//
// present in the version control system (VCS) at the time of building.
//
// An example of a version v0.0.2-0.20250310175756-a91372dc0602+dirty.
func GetVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
return info.Main.Version
}