Move to setting version info explicitly in linker flags
Some checks failed
/ golint (push) Failing after 12s
Some checks failed
/ golint (push) Failing after 12s
We don't have go built-in VCS information in a nix build because the git repository isn't present. After struggling to build an overlay that would provide it, I decided this path is easier of just injecting in the data that we need. Issue: #5
This commit is contained in:
parent
81826f853e
commit
d4cbfb960e
8 changed files with 119 additions and 79 deletions
|
|
@ -1,65 +1,11 @@
|
|||
package version
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// These will be set by ldflags at build time
|
||||
var (
|
||||
Version = "dev"
|
||||
Commit = ""
|
||||
BuildTime = "0"
|
||||
IsModified = "false"
|
||||
)
|
||||
|
||||
type VersionInfo struct {
|
||||
BuildTime time.Time `json:"build_time"`
|
||||
IsModified bool `json:"is_modified"`
|
||||
Revision string `json:"revision"`
|
||||
}
|
||||
|
||||
func Get() VersionInfo {
|
||||
// Try ldflags first (set by Nix build)
|
||||
if Commit != "" && Commit != "unknown" {
|
||||
var buildTime time.Time
|
||||
if timestamp, err := strconv.ParseInt(BuildTime, 10, 64); err == nil && timestamp > 0 {
|
||||
buildTime = time.Unix(timestamp, 0)
|
||||
}
|
||||
|
||||
return VersionInfo{
|
||||
Revision: Commit,
|
||||
BuildTime: buildTime,
|
||||
IsModified: IsModified == "true",
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to debug.ReadBuildInfo() for development
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return VersionInfo{
|
||||
Revision: "unknown",
|
||||
}
|
||||
}
|
||||
|
||||
var version VersionInfo
|
||||
for _, setting := range info.Settings {
|
||||
switch setting.Key {
|
||||
case "vcs.modified":
|
||||
version.IsModified = setting.Value == "true"
|
||||
case "vcs.revision":
|
||||
if len(setting.Value) > 7 {
|
||||
version.Revision = setting.Value[:7]
|
||||
} else {
|
||||
version.Revision = setting.Value
|
||||
}
|
||||
case "vcs.time":
|
||||
if t, err := time.Parse(time.RFC3339, setting.Value); err == nil {
|
||||
version.BuildTime = t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return version
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue