Skip to content

Commit

Permalink
Improve version metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Apr 11, 2024
1 parent ae1468f commit a5b6129
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions info/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@ import (
)

var (
name string
name string
license string

version = "dev build"
buildSource = "[source unknown]"
buildTime = "[build time unknown]"
license = "[license unknown]"
buildSource = "unknown"
buildTime = "unknown"

info *Info
loadInfo sync.Once
)

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

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

Source string
BuildTime string
CGO bool

Commit string
CommitTime string
Expand Down Expand Up @@ -56,23 +65,26 @@ func GetInfo() *Info {
buildSettings[setting.Key] = setting.Value
}

fmt.Println(buildSettings)

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,
}

if info.Commit == "" {
info.Commit = "[commit unknown]"
info.Commit = "unknown"
}
if info.CommitTime == "" {
info.CommitTime = "[commit time unknown]"
info.CommitTime = "unknown"
}
})

Expand All @@ -99,11 +111,19 @@ func FullVersion() string {
builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, Version()))

// Build info.
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH))
cgoInfo := "-cgo"
if info.CGO {
cgoInfo = "+cgo"
}
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s %s) for %s/%s\n", runtime.Version(), runtime.Compiler, cgoInfo, runtime.GOOS, runtime.GOARCH))
builder.WriteString(fmt.Sprintf(" at %s\n", info.BuildTime))

// Commit info.
builder.WriteString(fmt.Sprintf("\ncommit %s\n", info.Commit))
dirtyInfo := "clean"
if info.Dirty {
dirtyInfo = "dirty"
}
builder.WriteString(fmt.Sprintf("\ncommit %s (%s)\n", info.Commit, dirtyInfo))
builder.WriteString(fmt.Sprintf(" at %s\n", info.CommitTime))
builder.WriteString(fmt.Sprintf(" from %s\n", info.Source))

Expand All @@ -121,7 +141,7 @@ func CheckVersion() error {
return nil // testing on windows
default:
// check version information
if name == "[NAME]" || license == "[license unknown]" {
if name == "" || license == "" {
return errors.New("must call SetInfo() before calling CheckVersion()")
}
}
Expand Down

0 comments on commit a5b6129

Please sign in to comment.