diff options
| author | James Mills <1290234+prologic@users.noreply.github.com> | 2023-01-21 11:25:30 +1000 |
|---|---|---|
| committer | James Mills <1290234+prologic@users.noreply.github.com> | 2023-01-21 11:25:30 +1000 |
| commit | 07d888c2c60d727dab49aa8dbc905cb3ccedd7aa (patch) | |
| tree | b82546830b36630eab15007a3da03342fca2dbf9 /version.go | |
| parent | c727404d9156861dc40a657b3599da36ed0bae62 (diff) | |
Fix build and versioning (borrowed from yarn)
Diffstat (limited to 'version.go')
| -rw-r--r-- | version.go | 46 |
1 files changed, 39 insertions, 7 deletions
@@ -1,18 +1,50 @@ -package main +package tube import ( "fmt" + runtime "runtime/debug" + "strings" +) + +const ( + defaultVersion = "0.0.0" + defaultCommit = "HEAD" + defaultBuild = "0000-01-01:00:00+00:00" ) var ( - // Version release version - Version = "0.0.1" + // Version is the tagged release version in the form <major>.<minor>.<patch> + // following semantic versioning and is overwritten by the build system. + Version = defaultVersion - // Commit will be overwritten automatically by the build system - Commit = "HEAD" + // Commit is the commit sha of the build (normally from Git) and is overwritten + // by the build system. + Commit = defaultCommit + + // Build is the date and time of the build as an RFC3339 formatted string + // and is overwritten by the build system. + Build = defaultBuild ) -// FullVersion returns the full version and commit hash +// FullVersion display the full version and build func FullVersion() string { - return fmt.Sprintf("%s@%s", Version, Commit) + var sb strings.Builder + + isDefault := Version == defaultVersion && Commit == defaultCommit && Build == defaultBuild + + if !isDefault { + sb.WriteString(fmt.Sprintf("%s@%s %s", Version, Commit, Build)) + } + + if info, ok := runtime.ReadBuildInfo(); ok { + if isDefault { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Version)) + } + sb.WriteString(fmt.Sprintf(" %s", info.GoVersion)) + if info.Main.Sum != "" { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Sum)) + } + } + + return sb.String() } |
