Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion cmd/n8nctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
32 changes: 27 additions & 5 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"runtime/debug"

"github.com/spf13/cobra"

Expand All @@ -15,9 +16,7 @@ import (
)

var (
version = "dev"
commit = "none"
date = "unknown"
version string
jsonOutput bool
)

Expand All @@ -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()
}

Expand All @@ -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})
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.

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
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.

}
}
return
}

// IsJSONOutput returns whether JSON output is enabled
func IsJSONOutput() bool {
return jsonOutput
Expand Down
Loading