-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: use Go native build info for commit and date #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Comment on lines
+80
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of relying on References
|
||
| } | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // IsJSONOutput returns whether JSON output is enabled | ||
| func IsJSONOutput() bool { | ||
| return jsonOutput | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error returned by
json.Marshalis ignored. While amap[string]stringis generally safe to marshal, it is better practice to handle potential errors to ensure the CLI doesn't fail silently. Additionally, ensure that the variablesversion,commit, anddateare unexported and set vialdflagsto avoid unnecessarily widening the package's public API, as per repository standards.References