From 091e1913eed074e96fbf0fc928dc42307f9838c9 Mon Sep 17 00:00:00 2001 From: Hein Date: Sun, 8 Feb 2026 15:04:03 +0200 Subject: [PATCH] feat(version): retrieve version and build date from VCS if unset --- cmd/relspec/root.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/cmd/relspec/root.go b/cmd/relspec/root.go index 239d88a..ad2e672 100644 --- a/cmd/relspec/root.go +++ b/cmd/relspec/root.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "runtime/debug" + "time" "github.com/spf13/cobra" ) @@ -12,6 +14,36 @@ var ( buildDate = "unknown" ) +func init() { + // If version wasn't set via ldflags, try to get it from build info + if version == "dev" { + if info, ok := debug.ReadBuildInfo(); ok { + // Try to get version from VCS + var vcsRevision, vcsTime string + for _, setting := range info.Settings { + switch setting.Key { + case "vcs.revision": + if len(setting.Value) >= 7 { + vcsRevision = setting.Value[:7] + } + case "vcs.time": + vcsTime = setting.Value + } + } + + if vcsRevision != "" { + version = vcsRevision + } + + if vcsTime != "" { + if t, err := time.Parse(time.RFC3339, vcsTime); err == nil { + buildDate = t.UTC().Format("2006-01-02 15:04:05 UTC") + } + } + } + } +} + var rootCmd = &cobra.Command{ Use: "relspec", Short: "RelSpec - Database schema conversion and analysis tool",