65c0df50e6
Minimum Testing Threshold * mintests
81 lines
1.8 KiB
Go
81 lines
1.8 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(getStartTime(&startupTime, debug.ReadBuildInfo))
|
|
|
|
return startupTime
|
|
}
|
|
|
|
func getStartTime(startupTime *time.Time, readBuildInfo func() (info *debug.BuildInfo, ok bool)) func() {
|
|
return func() {
|
|
info, ok := 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()
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|