mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
* build: add BuildDate to version info will be used in enterprise to compare to license expiration time * cli: multi-line version output, add BuildDate before: $ nomad version Nomad v1.4.3 (coolfakecommithashomgoshsuchacoolonewoww) after: $ nomad version Nomad v1.5.0-dev BuildDate 2023-02-17T19:29:26Z Revision coolfakecommithashomgoshsuchacoolonewoww compare consul: $ consul version Consul v1.14.4 Revision dae670fe Build Date 2023-01-26T15:47:10Z Protocol 2 spoken by default, blah blah blah... and vault: $ vault version Vault v1.12.3 (209b3dd99fe8ca320340d08c70cff5f620261f9b), built 2023-02-02T09:07:27Z * docs: update version command output
107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package version
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
// BuildDate is the time of the git commit used to build the program,
|
|
// in RFC3339 format. It is filled in by the compiler via makefile.
|
|
BuildDate string
|
|
|
|
// The git commit that was compiled. This will be filled in by the compiler.
|
|
GitCommit string
|
|
GitDescribe string
|
|
|
|
// The main version number that is being run at the moment.
|
|
Version = "1.5.0"
|
|
|
|
// A pre-release marker for the version. If this is "" (empty string)
|
|
// then it means that it is a final release. Otherwise, this is a pre-release
|
|
// such as "dev" (in development), "beta", "rc1", etc.
|
|
VersionPrerelease = "dev"
|
|
|
|
// VersionMetadata is metadata further describing the build type.
|
|
VersionMetadata = ""
|
|
)
|
|
|
|
// VersionInfo
|
|
type VersionInfo struct {
|
|
BuildDate time.Time
|
|
Revision string
|
|
Version string
|
|
VersionPrerelease string
|
|
VersionMetadata string
|
|
}
|
|
|
|
func (v *VersionInfo) Copy() *VersionInfo {
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
|
|
nv := *v
|
|
return &nv
|
|
}
|
|
|
|
func GetVersion() *VersionInfo {
|
|
ver := Version
|
|
rel := VersionPrerelease
|
|
md := VersionMetadata
|
|
if GitDescribe != "" {
|
|
ver = GitDescribe
|
|
}
|
|
if GitDescribe == "" && rel == "" && VersionPrerelease != "" {
|
|
rel = "dev"
|
|
}
|
|
|
|
// on parse error, will be zero value time.Time{}
|
|
built, _ := time.Parse(time.RFC3339, BuildDate)
|
|
|
|
return &VersionInfo{
|
|
BuildDate: built,
|
|
Revision: GitCommit,
|
|
Version: ver,
|
|
VersionPrerelease: rel,
|
|
VersionMetadata: md,
|
|
}
|
|
}
|
|
|
|
func (c *VersionInfo) VersionNumber() string {
|
|
version := c.Version
|
|
|
|
if c.VersionPrerelease != "" {
|
|
version = fmt.Sprintf("%s-%s", version, c.VersionPrerelease)
|
|
}
|
|
|
|
if c.VersionMetadata != "" {
|
|
version = fmt.Sprintf("%s+%s", version, c.VersionMetadata)
|
|
}
|
|
|
|
return version
|
|
}
|
|
|
|
func (c *VersionInfo) FullVersionNumber(rev bool) string {
|
|
var versionString bytes.Buffer
|
|
|
|
fmt.Fprintf(&versionString, "Nomad v%s", c.Version)
|
|
if c.VersionPrerelease != "" {
|
|
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
|
|
}
|
|
|
|
if c.VersionMetadata != "" {
|
|
fmt.Fprintf(&versionString, "+%s", c.VersionMetadata)
|
|
}
|
|
|
|
if !c.BuildDate.IsZero() {
|
|
fmt.Fprintf(&versionString, "\nBuildDate %s", c.BuildDate.Format(time.RFC3339))
|
|
}
|
|
|
|
if rev && c.Revision != "" {
|
|
fmt.Fprintf(&versionString, "\nRevision %s", c.Revision)
|
|
}
|
|
|
|
return versionString.String()
|
|
}
|