From 502fc706932e44820ec25a1063f61e2e4c814ce1 Mon Sep 17 00:00:00 2001 From: Florian Kinder Date: Sun, 12 Apr 2026 13:03:43 +0200 Subject: [PATCH] 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. Move version variable to main.go to align ldflags with esq-cli and atl-cli (-X main.version= instead of the full package path). --- .goreleaser.yml | 4 +--- cmd/n8nctl/main.go | 4 +++- internal/cmd/root.go | 32 +++++++++++++++++++++++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index b128689..c17b3b2 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -16,9 +16,7 @@ builds: - arm64 ldflags: - -s -w - - -X github.com/enthus-appdev/n8n-cli/internal/cmd.version={{.Version}} - - -X github.com/enthus-appdev/n8n-cli/internal/cmd.commit={{.ShortCommit}} - - -X github.com/enthus-appdev/n8n-cli/internal/cmd.date={{.Date}} + - -X main.version={{.Version}} archives: - formats: diff --git a/cmd/n8nctl/main.go b/cmd/n8nctl/main.go index 1225445..2a4f6cc 100644 --- a/cmd/n8nctl/main.go +++ b/cmd/n8nctl/main.go @@ -6,8 +6,10 @@ import ( "github.com/enthus-appdev/n8n-cli/internal/cmd" ) +var version = "dev" + func main() { - if err := cmd.Execute(); err != nil { + if err := cmd.Execute(version); err != nil { os.Exit(1) } } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index ecddd00..b28979e 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "runtime/debug" "github.com/spf13/cobra" @@ -15,9 +16,7 @@ import ( ) var ( - version = "dev" - commit = "none" - date = "unknown" + version string jsonOutput bool ) @@ -32,7 +31,8 @@ automation, and LLM-assisted workflow development.`, SilenceUsage: true, } -func Execute() error { +func Execute(ver string) error { + version = ver return rootCmd.Execute() } @@ -52,16 +52,38 @@ func newVersionCmd() *cobra.Command { Use: "version", Short: "Print the version number", Run: func(cmd *cobra.Command, args []string) { + commit, date := vcsInfo() if jsonOutput { out, _ := json.Marshal(map[string]string{"version": version, "commit": commit, "date": date}) fmt.Println(string(out)) } else { - fmt.Printf("n8n-cli %s\ncommit: %s\nbuilt: %s\n", version, commit, date) + fmt.Printf("n8n-cli %s\ncommit: %s\nbuilt: %s\n", version, commit, date) } }, } } +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 +} + // IsJSONOutput returns whether JSON output is enabled func IsJSONOutput() bool { return jsonOutput