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