Skip to content

Commit

Permalink
Use build info when available (go install) (#265)
Browse files Browse the repository at this point in the history
* use build info when available (go install). Correctly detecting local builds
  • Loading branch information
jmdacruz authored May 27, 2022
1 parent 56d57c7 commit e35fdec
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion buildversion/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@

package buildversion

import (
"fmt"
"runtime/debug"
)

const DefaultVersion = "0.0.0-dev"

var (
// these are overwritten/populated via build CLI
BuildVersion = "0.0.0-dev"
BuildVersion = DefaultVersion
BuildTime = ""
BuildCommit = ""
)
Expand All @@ -31,3 +38,16 @@ var packageManager = "source"
func PackageManager() string {
return packageManager
}

func init() {
// Use build info from debug package if available, and if no build info is
// provided via build CLI.
info, available := debug.ReadBuildInfo()
// info.Main.Version will be "" when debugging, and "(devel)" when building with no arguments
if available && info.Main.Version != "" && info.Main.Version != "(devel)" && BuildTime == "" && BuildCommit == "" && BuildVersion == DefaultVersion {
BuildVersion = info.Main.Version
BuildCommit = fmt.Sprintf("(unknown, mod sum: %q)", info.Main.Sum)
BuildTime = "(unknown)"
}

}

0 comments on commit e35fdec

Please sign in to comment.