From 60ba64ad0c34632e7e39906bbc385111f28c19f3 Mon Sep 17 00:00:00 2001 From: Florian Kinder Date: Sun, 12 Apr 2026 13:03:39 +0200 Subject: [PATCH 1/2] refactor: use Go native build info for commit and date Replace ldflags injection of commit and date with runtime/debug.ReadBuildInfo(), which Go embeds automatically from VCS. Only version remains as an ldflag since there is no native way to get the git tag at runtime. This aligns the build pattern across all three CLIs (esq, atl, n8nctl). --- .goreleaser.yml | 1 - cmd/esq/main.go | 7 ++----- internal/cmd/root.go | 27 +++++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index bb7465a..96543f2 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -17,7 +17,6 @@ builds: ldflags: - -s -w - -X main.version={{.Version}} - - -X main.commit={{.ShortCommit}} archives: - formats: diff --git a/cmd/esq/main.go b/cmd/esq/main.go index faef12a..d457201 100644 --- a/cmd/esq/main.go +++ b/cmd/esq/main.go @@ -6,12 +6,9 @@ import ( "github.com/enthus-appdev/esq-cli/internal/cmd" ) -var ( - version = "dev" - commit = "none" -) +var version = "dev" func main() { - exitCode := cmd.Execute(version, commit) + exitCode := cmd.Execute(version) os.Exit(exitCode) } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 8d41678..3c2ea99 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "runtime/debug" "strings" configcmd "github.com/enthus-appdev/esq-cli/internal/cmd/config" @@ -16,7 +17,8 @@ import ( var envOverride string // Execute runs the root command and returns the exit code. -func Execute(ver, commit string) int { +func Execute(ver string) int { + commit, date := vcsInfo() rootCmd := &cobra.Command{ Use: "esq", Short: "Elasticsearch Query CLI", @@ -34,7 +36,7 @@ func Execute(ver, commit string) int { esq health`, SilenceUsage: true, SilenceErrors: true, - Version: fmt.Sprintf("%s (commit: %s)", ver, commit), + Version: fmt.Sprintf("%s\ncommit: %s\nbuilt: %s", ver, commit, date), } rootCmd.PersistentFlags().StringVarP(&envOverride, "env", "e", "", "Override active environment") @@ -128,6 +130,27 @@ func joinStrings(ss []string) string { return result } +func vcsInfo() (commit, date string) { + commit, date = "unknown", "unknown" + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + for _, s := range info.Settings { + switch s.Key { + case "vcs.revision": + if len(s.Value) >= 7 { + commit = s.Value[:7] + } else { + commit = s.Value + } + case "vcs.time": + date = s.Value + } + } + return +} + // --- Simple commands (kept in root.go since they're small) --- func newGetCmd() *cobra.Command { From 58fef1ff35ccade4ec76c5e25933acaa889fa35b Mon Sep 17 00:00:00 2001 From: Florian Kinder Date: Sun, 12 Apr 2026 13:09:27 +0200 Subject: [PATCH 2/2] fix: append -dirty suffix when built from modified worktree --- internal/cmd/root.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 3c2ea99..290ffc3 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -132,6 +132,7 @@ func joinStrings(ss []string) string { func vcsInfo() (commit, date string) { commit, date = "unknown", "unknown" + var modified bool info, ok := debug.ReadBuildInfo() if !ok { return @@ -139,15 +140,20 @@ func vcsInfo() (commit, date string) { for _, s := range info.Settings { switch s.Key { case "vcs.revision": - if len(s.Value) >= 7 { + if len(s.Value) > 7 { commit = s.Value[:7] } else { commit = s.Value } case "vcs.time": date = s.Value + case "vcs.modified": + modified = s.Value == "true" } } + if modified { + commit += "-dirty" + } return }