version: add Info func to expose EmbeddedInfo

To be used to in a different repo.

Updates tailscale/corp#1297

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali 2024-05-15 12:59:44 -04:00 committed by Maisem Ali
parent 7f83f9fc83
commit e3dec086e6

View File

@ -77,7 +77,7 @@ func Long() string {
if !bi.valid { if !bi.valid {
return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-ERR-BuildInfo" return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-ERR-BuildInfo"
} }
return fmt.Sprintf("%s-dev%s-t%s%s", strings.TrimSpace(tailscaleroot.VersionDotTxt), bi.commitDate, bi.commitAbbrev(), dirtyString()) return fmt.Sprintf("%s-dev%s-t%s%s", strings.TrimSpace(tailscaleroot.VersionDotTxt), bi.CommitDate, bi.commitAbbrev(), dirtyString())
}) })
} }
@ -97,47 +97,58 @@ func Short() string {
if !bi.valid { if !bi.valid {
return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-ERR-BuildInfo" return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-ERR-BuildInfo"
} }
return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-dev" + bi.commitDate return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-dev" + bi.CommitDate
}) })
} }
type embeddedInfo struct { // Info returns information about the version embedded in the binary.
// If the version information is not present, the second return value is false.
func Info() (EmbeddedInfo, bool) {
ei := getEmbeddedInfo()
if !ei.valid {
return EmbeddedInfo{}, false
}
return ei, true
}
// EmbeddedInfo contains information about the version embedded in the binary.
type EmbeddedInfo struct {
valid bool valid bool
commit string Commit string
commitDate string CommitDate string
dirty bool Dirty bool
} }
func (i embeddedInfo) commitAbbrev() string { func (i EmbeddedInfo) commitAbbrev() string {
if len(i.commit) >= 9 { if len(i.Commit) >= 9 {
return i.commit[:9] return i.Commit[:9]
} }
return i.commit return i.Commit
} }
var getEmbeddedInfo = lazy.SyncFunc(func() embeddedInfo { var getEmbeddedInfo = lazy.SyncFunc(func() EmbeddedInfo {
bi, ok := debug.ReadBuildInfo() bi, ok := debug.ReadBuildInfo()
if !ok { if !ok {
return embeddedInfo{} return EmbeddedInfo{}
} }
ret := embeddedInfo{valid: true} ret := EmbeddedInfo{valid: true}
for _, s := range bi.Settings { for _, s := range bi.Settings {
switch s.Key { switch s.Key {
case "vcs.revision": case "vcs.revision":
ret.commit = s.Value ret.Commit = s.Value
case "vcs.time": case "vcs.time":
if len(s.Value) >= len("yyyy-mm-dd") { if len(s.Value) >= len("yyyy-mm-dd") {
ret.commitDate = s.Value[:len("yyyy-mm-dd")] ret.CommitDate = s.Value[:len("yyyy-mm-dd")]
ret.commitDate = strings.ReplaceAll(ret.commitDate, "-", "") ret.CommitDate = strings.ReplaceAll(ret.CommitDate, "-", "")
} }
case "vcs.modified": case "vcs.modified":
ret.dirty = s.Value == "true" ret.Dirty = s.Value == "true"
} }
} }
if ret.commit == "" || ret.commitDate == "" { if ret.Commit == "" || ret.CommitDate == "" {
// Build info is present in the binary, but has no useful data. Act as // Build info is present in the binary, but has no useful data. Act as
// if it's missing. // if it's missing.
return embeddedInfo{} return EmbeddedInfo{}
} }
return ret return ret
}) })
@ -146,14 +157,14 @@ func gitCommit() string {
if gitCommitStamp != "" { if gitCommitStamp != "" {
return gitCommitStamp return gitCommitStamp
} }
return getEmbeddedInfo().commit return getEmbeddedInfo().Commit
} }
func gitDirty() bool { func gitDirty() bool {
if gitDirtyStamp { if gitDirtyStamp {
return true return true
} }
return getEmbeddedInfo().dirty return getEmbeddedInfo().Dirty
} }
func dirtyString() string { func dirtyString() string {