Skip to content

refactor: use Go native build info for commit and date#14

Merged
fank merged 1 commit intomainfrom
claude/use-native-go-build-info
Apr 12, 2026
Merged

refactor: use Go native build info for commit and date#14
fank merged 1 commit intomainfrom
claude/use-native-go-build-info

Conversation

@fank
Copy link
Copy Markdown
Member

@fank fank commented Apr 12, 2026

Summary

  • Replace ldflags injection of commit and date with runtime/debug.ReadBuildInfo() which Go embeds automatically from VCS
  • Move version variable from internal/cmd to main.go — ldflags simplify from -X github.com/enthus-appdev/n8n-cli/internal/cmd.version= to -X main.version=
  • Only version remains as an ldflag (no native way to get the git tag at runtime)
  • Aligns the build pattern across all three CLIs (esq, atl, n8nctl) to use identical ldflags: -X main.version={{.Version}}

Test plan

  • go vet ./... passes
  • Local build shows correct commit hash and build date from VCS
  • JSON output (--json version) includes commit and date
  • Version injection via ldflag still works
  • -s -w stripping does NOT affect runtime/debug build info

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).
@fank fank merged commit 2a7bcbf into main Apr 12, 2026
5 checks passed
@fank fank deleted the claude/use-native-go-build-info branch April 12, 2026 11:07
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread internal/cmd/root.go
Run: func(cmd *cobra.Command, args []string) {
commit, date := vcsInfo()
if jsonOutput {
out, _ := json.Marshal(map[string]string{"version": version, "commit": commit, "date": date})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. In Go, use ldflags with the -X option to set unexported variables for build information. This avoids unnecessarily widening the package's public API.

Comment thread internal/cmd/root.go
Comment on lines +80 to +81
case "vcs.time":
date = s.Value
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. In Go, use ldflags with the -X option to set unexported variables for build information. This avoids unnecessarily widening the package's public API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant