From 74e1bb50dab2eb20edde9bdb3d437583838f2708 Mon Sep 17 00:00:00 2001 From: James O'Gorman Date: Wed, 9 Jul 2025 21:35:23 +0100 Subject: [PATCH] BUG: Output better "version" string when running main.go directly (#3658) --- build/build.go | 36 +++--------------------------------- pkg/version/version.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/build/build.go b/build/build.go index 4dcc99cd3..8f9ea4af7 100644 --- a/build/build.go +++ b/build/build.go @@ -6,16 +6,15 @@ import ( "log" "os" "os/exec" - "strings" -) -var sha = flag.String("sha", "", "SHA of current commit") + "github.com/StackExchange/dnscontrol/v4/pkg/version" +) var goos = flag.String("os", "", "OS to build (linux, windows, or darwin) Defaults to all.") func main() { flag.Parse() - flags := fmt.Sprintf(`-s -w -X "github.com/StackExchange/dnscontrol/v4/pkg/version.version=%s"`, getVersion()) + flags := fmt.Sprintf(`-s -w -X "github.com/StackExchange/dnscontrol/v4/pkg/version.version=%s"`, version.Version()) pkg := "github.com/StackExchange/dnscontrol/v4" build := func(out, goos string) { @@ -50,32 +49,3 @@ func main() { } } } - -func getVersion() string { - if *sha != "" { - return *sha - } - // check teamcity build version - if v := os.Getenv("BUILD_VCS_NUMBER"); v != "" { - return v - } - // check git - cmd := exec.Command("git", "rev-parse", "HEAD") - v, err := cmd.CombinedOutput() - if err != nil { - return "" - } - ver := strings.TrimSpace(string(v)) - // see if dirty - cmd = exec.Command("git", "diff-index", "--quiet", "HEAD", "--") - err = cmd.Run() - // exit status 1 indicates dirty tree - if err != nil { - if err.Error() == "exit status 1" { - ver += "[dirty]" - } else { - log.Printf("!%s!", err.Error()) - } - } - return ver -} diff --git a/pkg/version/version.go b/pkg/version/version.go index 420c5a95c..e7ac95c9b 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,17 +1,44 @@ package version -import "runtime/debug" +import ( + "os/exec" + "runtime/debug" + "strings" +) // Set by GoReleaser var version string +// VCSVersion retrieves the version information from git. +// +// If the current commit is untagged, the version string will show the last +// tag, followed by the number of commits since the tag, then the short +// hash of the current commit. +// +// If the tree is dirty, "-dirty" is appended. +func VCSVersion() string { + cmd := exec.Command("git", "describe", "--tags", "--always", "--dirty") + v, err := cmd.CombinedOutput() + if err != nil { + return "" + } + ver := strings.TrimSpace(string(v)) + return ver +} + +// Version returns either the tag set by GoReleaser, or the version information +// from Git. func Version() string { if version != "" { return version } bi, ok := debug.ReadBuildInfo() - if !ok { - return "dev" + if !ok || + // When running with "go run main.go" no module information is available + bi.Main.Version == "" || + // Go gives no commit information if not on a tag + bi.Main.Version == "(devel)" { + return VCSVersion() } return bi.Main.Version }