Skip to content

Commit

Permalink
Fix and improve parsing of git tag based version metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Apr 16, 2024
1 parent c6fa7a8 commit e888e08
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions info/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,47 @@ var (
name string
license string

version = "dev build"
buildSource = "unknown"
buildTime = "unknown"
version = "dev build"
versionNumber = "0.0.0"
buildSource = "unknown"
buildTime = "unknown"

info *Info
loadInfo sync.Once
)

func init() {
// Convert version string space placeholders.
version = strings.ReplaceAll(version, "_", " ")
// Replace space placeholders.
buildSource = strings.ReplaceAll(buildSource, "_", " ")
buildTime = strings.ReplaceAll(buildTime, "_", " ")

// Convert version string from git tag to expected format.
version = strings.TrimSpace(strings.ReplaceAll(strings.TrimPrefix(version, "v"), "_", " "))
versionNumber = strings.TrimSpace(strings.TrimSuffix(version, "dev build"))
if versionNumber == "" {
versionNumber = "0.0.0"
}

// Get build info.
buildInfo, _ := debug.ReadBuildInfo()
buildSettings := make(map[string]string)
for _, setting := range buildInfo.Settings {
buildSettings[setting.Key] = setting.Value
}

// Add "dev build" to version if repo is dirty.
if buildSettings["vcs.modified"] == "true" &&
!strings.HasSuffix(version, "dev build") {
version += " dev build"
}
}

// Info holds the programs meta information.
type Info struct { //nolint:maligned
Name string
Version string
License string
Name string
Version string
VersionNumber string
License string

Source string
BuildTime string
Expand Down Expand Up @@ -66,16 +87,17 @@ func GetInfo() *Info {
}

info = &Info{
Name: name,
Version: version,
License: license,
Source: buildSource,
BuildTime: buildTime,
CGO: buildSettings["CGO_ENABLED"] == "1",
Commit: buildSettings["vcs.revision"],
CommitTime: buildSettings["vcs.time"],
Dirty: buildSettings["vcs.modified"] == "true",
BuildInfo: *buildInfo,
Name: name,
Version: version,
VersionNumber: versionNumber,
License: license,
Source: buildSource,
BuildTime: buildTime,
CGO: buildSettings["CGO_ENABLED"] == "1",
Commit: buildSettings["vcs.revision"],
CommitTime: buildSettings["vcs.time"],
Dirty: buildSettings["vcs.modified"] == "true",
BuildInfo: *buildInfo,
}

if info.Commit == "" {
Expand All @@ -89,24 +111,23 @@ func GetInfo() *Info {
return info
}

// Version returns the short version string.
// Version returns the annotated version.
func Version() string {
info := GetInfo()

if info.Dirty {
return version + "*"
}

return version
}

// VersionNumber returns the version number only.
func VersionNumber() string {
return versionNumber
}

// FullVersion returns the full and detailed version string.
func FullVersion() string {
info := GetInfo()
builder := new(strings.Builder)

// Name and version.
builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, Version()))
builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, version))

// Build info.
cgoInfo := "-cgo"
Expand Down

0 comments on commit e888e08

Please sign in to comment.