// Package buildinfo identifies the compiled binary, never the runtime workspace. package buildinfo import ( "runtime/debug" "strings" ) // Docker builds set these through -ldflags because .git is excluded from context. var Version string var Commit string var Dirty string type Info struct { Version string `json:"version"` Commit string `json:"commit"` Dirty *bool `json:"dirty"` } func Current() Info { build, _ := debug.ReadBuildInfo() return resolve(build, Version, Commit, Dirty) } func resolve(build *debug.BuildInfo, version, commit, dirty string) Info { if build != nil { if version == "" && build.Main.Version != "(devel)" { version = build.Main.Version } for _, setting := range build.Settings { switch setting.Key { case "vcs.revision": if commit == "" { commit = setting.Value } case "vcs.modified": if dirty == "" { dirty = setting.Value } } } } info := Info{Version: version, Commit: commit} if dirty == "true" || dirty == "false" { value := dirty == "true" info.Dirty = &value } if info.Version == "" { info.Version = "dev" if commit != "" && commit != "unknown" { short := commit if len(short) > 12 { short = short[:12] } info.Version = short } } if info.Dirty != nil && *info.Dirty && !strings.HasSuffix(info.Version, "-dirty") { info.Version = strings.TrimSuffix(info.Version, "+dirty") info.Version += "-dirty" } if info.Commit == "" { info.Commit = "unknown" } return info }