refactor: use Go native build info for commit and date#14
Conversation
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).
There was a problem hiding this comment.
Code Review
This pull request refactors the versioning logic by moving the version variable to the main package and using runtime/debug to extract VCS information (commit and date) at runtime. The review feedback suggests handling the potential error from json.Marshal in the version command and recommends using ldflags for the build date to ensure consistent formatting and adhere to best practices for unexported variables.
| Run: func(cmd *cobra.Command, args []string) { | ||
| commit, date := vcsInfo() | ||
| if jsonOutput { | ||
| out, _ := json.Marshal(map[string]string{"version": version, "commit": commit, "date": date}) |
There was a problem hiding this comment.
The error returned by json.Marshal is ignored. While a map[string]string is generally safe to marshal, it is better practice to handle potential errors to ensure the CLI doesn't fail silently. Additionally, ensure that the variables version, commit, and date are unexported and set via ldflags to avoid unnecessarily widening the package's public API, as per repository standards.
out, err := json.Marshal(map[string]string{"version": version, "commit": commit, "date": date})
if err != nil {
fmt.Fprintf(os.Stderr, "Error marshaling version info: %v\n", err)
return
}
fmt.Println(string(out))References
- In Go, use ldflags with the -X option to set unexported variables for build information. This avoids unnecessarily widening the package's public API.
| case "vcs.time": | ||
| date = s.Value |
There was a problem hiding this comment.
Instead of relying on debug.ReadBuildInfo() to extract vcs.time, which introduces formatting inconsistencies (RFC3339 vs the expected YYYY-MM-DD), use ldflags with the -X option to set unexported variables for build information. This follows the repository's practice for build information, allows for consistent output formatting, and avoids unnecessarily widening the package's public API.
References
- In Go, use ldflags with the -X option to set unexported variables for build information. This avoids unnecessarily widening the package's public API.
Summary
commitanddatewithruntime/debug.ReadBuildInfo()which Go embeds automatically from VCSversionvariable frominternal/cmdtomain.go— ldflags simplify from-X github.com/enthus-appdev/n8n-cli/internal/cmd.version=to-X main.version=versionremains as an ldflag (no native way to get the git tag at runtime)-X main.version={{.Version}}Test plan
go vet ./...passes--json version) includes commit and date-s -wstripping does NOT affectruntime/debugbuild info