open-vault/version/version.go

88 lines
1.7 KiB
Go
Raw Normal View History

2015-11-09 18:52:55 +00:00
package version
import (
"bytes"
"fmt"
)
var (
// The git commit that was compiled. This will be filled in by the compiler.
GitCommit string
GitDescribe string
// Whether cgo is enabled or not; set at build time
CgoEnabled bool
2016-08-11 21:23:47 +00:00
Version = "unknown"
VersionPrerelease = "unknown"
2017-04-28 20:22:00 +00:00
VersionMetadata = ""
)
2015-11-09 18:52:55 +00:00
// VersionInfo
type VersionInfo struct {
Revision string
Version string
VersionPrerelease string
2017-04-28 20:22:00 +00:00
VersionMetadata string
2015-11-09 18:52:55 +00:00
}
func GetVersion() *VersionInfo {
ver := Version
rel := VersionPrerelease
2017-04-28 20:22:00 +00:00
md := VersionMetadata
2015-11-09 18:52:55 +00:00
if GitDescribe != "" {
ver = GitDescribe
}
if GitDescribe == "" && rel == "" && VersionPrerelease != "" {
rel = "dev"
}
return &VersionInfo{
Revision: GitCommit,
Version: ver,
VersionPrerelease: rel,
2017-04-28 20:22:00 +00:00
VersionMetadata: md,
2015-11-09 18:52:55 +00:00
}
}
func (c *VersionInfo) VersionNumber() string {
if Version == "unknown" && VersionPrerelease == "unknown" {
return "(version unknown)"
}
2016-09-01 19:23:23 +00:00
version := fmt.Sprintf("%s", c.Version)
if c.VersionPrerelease != "" {
version = fmt.Sprintf("%s-%s", version, c.VersionPrerelease)
}
2017-04-28 20:22:00 +00:00
if c.VersionMetadata != "" {
version = fmt.Sprintf("%s+%s", version, c.VersionMetadata)
}
return version
}
func (c *VersionInfo) FullVersionNumber(rev bool) string {
2015-11-09 18:52:55 +00:00
var versionString bytes.Buffer
if Version == "unknown" && VersionPrerelease == "unknown" {
return "Vault (version unknown)"
}
2015-11-09 18:52:55 +00:00
fmt.Fprintf(&versionString, "Vault v%s", c.Version)
if c.VersionPrerelease != "" {
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
}
2017-04-28 20:22:00 +00:00
if c.VersionMetadata != "" {
fmt.Fprintf(&versionString, "+%s", c.VersionMetadata)
}
if rev && c.Revision != "" {
fmt.Fprintf(&versionString, " (%s)", c.Revision)
2015-11-09 18:52:55 +00:00
}
return versionString.String()
}