Files

53 lines
1.4 KiB
Go

package version
import (
"fmt"
"runtime"
"strings"
)
// Info describes a version of an executable
type Info struct {
Major uint `json:"major"`
Minor uint `json:"minor"`
Commit uint `json:"commi"`
Build string `json:"build"`
Runtime string `json:"runtime"`
flt float32
}
// PopulateFromBuild parses the major, minor and commit values from Build,
// which is expected in the format [v]major.minor.commit[-dirty]; make build
// links it in from GIT_VERSION (see current.go). Values that do not match
// stay zero.
func (v *Info) PopulateFromBuild() {
build := strings.TrimPrefix(v.Build, "v")
_, _ = fmt.Sscanf(build, "%d.%d.%d", &v.Major, &v.Minor, &v.Commit)
_, _ = fmt.Sscanf(build, "%f-", &v.flt)
v.flt = v.flt*1000000 + float32(v.Commit)
v.Runtime = runtime.Version()
}
func (v Info) String() string {
return v.Build
}
// GreaterOrEqual returns true if the version 'v' is the same or new that the supplied parameter 'other'
// This only examines the Major & Minor field (as the SHA in Build provides no ordering indication)
func (v Info) GreaterOrEqual(than Info) bool {
if v.Major > than.Major {
return true
}
if v.Major < than.Major {
return false
}
return v.Minor >= than.Minor
}
// Float returns the version Major/Minor as a float Major.Minor
// e.g. given Major:3 Minor:52001, it'll return 3.52001
// this is only valid if PopulateFromBuild has been called.
func (v Info) Float() float32 {
return v.flt
}