version: add fallback to fill revision from BuildInfo

If Revision is not set by the linker, fallback to load the revision in
the same format as generated by ./hack/git-meta.

Unfortunately, we have no access to the tags, so we can't have a
fallback for the Version field.

Signed-off-by: Justin Chadwell <me@jedevc.com>
pull/1637/head
Justin Chadwell 2 years ago
parent 6f722da04d
commit 0d2a90e63b

@ -1,13 +1,47 @@
package version
import (
"runtime/debug"
"strconv"
)
const (
defaultVersion = "v0.0.0+unknown"
)
var (
// Package is filled at linking time
Package = "github.com/docker/buildx"
// Version holds the complete version number. Filled in at linking time.
Version = "v0.0.0+unknown"
Version = defaultVersion
// Revision is filled with the VCS (e.g. git) revision being used to build
// the program at linking time.
Revision = ""
)
func init() {
if Revision != "" {
return
}
if info, ok := debug.ReadBuildInfo(); ok {
revision := ""
modified := false
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
revision = setting.Value
case "vcs.modified":
modified, _ = strconv.ParseBool(setting.Value)
}
}
if revision != "" {
Revision = revision
if modified {
Revision += ".m"
}
}
}
}

Loading…
Cancel
Save