Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and improve parsing of git tag based version metadata #228

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading