Add shell completions and version flag with build info#42
Conversation
- completion command: generates bash, zsh, fish, powershell scripts - version command: prints version, git commit, build date - Build info injected via -ldflags at build time Closes #17 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR adds two quality-of-life features to the Zenodo CLI: shell completion generation for bash/zsh/fish/powershell and a version command that displays build information (version, commit hash, and build date). The version information is designed to be injected at build time via Go's -ldflags mechanism, which will work seamlessly with goreleaser when it's configured in issue #18.
Changes:
- Added
zenodo completion [bash|zsh|fish|powershell]command to generate shell-specific completion scripts - Added
zenodo versionsubcommand to display version, commit hash, and build date - Version variables default to "dev"/"unknown" and can be overridden via ldflags at build time
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
internal/cli/completion.go |
Implements completion command with support for 4 shells (bash, zsh, fish, powershell) using cobra's built-in completion generators |
internal/cli/version.go |
Implements version command with ldflags-injectable build variables for version, commit, and date |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var versionCmd = &cobra.Command{ | ||
| Use: "version", | ||
| Short: "Print version and build information", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| fmt.Printf("zenodo %s\n", version) | ||
| fmt.Printf(" commit: %s\n", commit) | ||
| fmt.Printf(" built: %s\n", date) | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(versionCmd) | ||
| } |
There was a problem hiding this comment.
Issue #17 specifies a --version flag on the root command, but the implementation provides a version subcommand instead. While both patterns are acceptable in CLI design, this doesn't match the original requirement. Consider either: (1) adding a --version flag to the root command by setting the Version field on rootCmd and using cobra's built-in version flag support, or (2) updating issue #17 to reflect that a subcommand approach is being used. Many modern CLIs support both patterns (e.g., kubectl version and kubectl --version both work).
| Run: func(cmd *cobra.Command, args []string) { | ||
| fmt.Printf("zenodo %s\n", version) | ||
| fmt.Printf(" commit: %s\n", commit) | ||
| fmt.Printf(" built: %s\n", date) |
There was a problem hiding this comment.
For consistency with all other commands in the codebase (records.go, communities.go, config.go, etc.), consider using RunE instead of Run. While fmt.Printf errors are rare, using RunE provides consistent error handling patterns and allows for proper error propagation if needed. All other cobra.Command definitions in internal/cli/ use RunE.
| Run: func(cmd *cobra.Command, args []string) { | |
| fmt.Printf("zenodo %s\n", version) | |
| fmt.Printf(" commit: %s\n", commit) | |
| fmt.Printf(" built: %s\n", date) | |
| RunE: func(cmd *cobra.Command, args []string) error { | |
| if _, err := fmt.Printf("zenodo %s\n", version); err != nil { | |
| return err | |
| } | |
| if _, err := fmt.Printf(" commit: %s\n", commit); err != nil { | |
| return err | |
| } | |
| if _, err := fmt.Printf(" built: %s\n", date); err != nil { | |
| return err | |
| } | |
| return nil |
| } | ||
| return nil |
There was a problem hiding this comment.
This return statement is unreachable because the switch statement above handles all possible values validated by cobra.OnlyValidArgs. Since all four valid args (bash, zsh, fish, powershell) are explicitly handled in the switch cases and each returns, execution will never reach this line. Consider removing this unreachable return statement.
| } | |
| return nil | |
| default: | |
| return nil | |
| } |
ELI5
Two quality-of-life features: (1) tab completion — type
zenodo rec<TAB>and it fills inrecordsfor bash/zsh/fish/powershell, and (2)zenodo versionshows the version number, git commit, and build date. The version info gets baked in at build time via Go's-ldflags, sogoreleaser(#18) will inject the real values automatically.Summary
zenodo completion [bash|zsh|fish|powershell]generates shell completion scriptszenodo versionprints version, commit hash, build date-ldflagsat build time (defaults to "dev"/"unknown")Code changes
internal/cli/completion.gointernal/cli/version.goTest plan
go test ./...— all 75 tests passzenodo versionprints "dev" (default)go build -ldflags "-X ...version=0.1.0"thenzenodo versionprints "0.1.0"zenodo completion bashgenerates valid completion scriptCloses #17
🤖 Generated with Claude Code