Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions cmd/buildinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import "runtime/debug"

func init() {
if version == "dev" {
applyBuildInfo()
}
}

// applyBuildInfo reads Go's embedded build metadata and populates the version,
// commit, and buildDate vars when they were not injected via ldflags (e.g. go install).
func applyBuildInfo() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
if v := info.Main.Version; v != "" && v != "(devel)" {
version = v
}
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
if s.Value != "" {
commit = s.Value
}
case "vcs.time":
if s.Value != "" {
buildDate = s.Value
}
}
}
}
28 changes: 28 additions & 0 deletions cmd/buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import "testing"

func TestApplyBuildInfoDoesNotPanic(t *testing.T) {
originalVersion := version
originalCommit := commit
originalBuildDate := buildDate
t.Cleanup(func() {
version = originalVersion
commit = originalCommit
buildDate = originalBuildDate
})

applyBuildInfo()
}

func TestApplyBuildInfoDoesNotSetDevilVersion(t *testing.T) {
originalVersion := version
t.Cleanup(func() { version = originalVersion })

version = "dev"
applyBuildInfo()

if version == "(devel)" {
t.Fatal("applyBuildInfo must not set version to '(devel)'")
}
}