-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add adder to generate cmd stubs from markdown #613
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
base: main
Are you sure you want to change the base?
Conversation
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.
Summary of Changes
Hello @jrschumacher, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a new code generation tool, adder, to automate the creation of CLI command stubs from markdown documentation. I've refactored the existing command-line interface to leverage this new system, which will enhance the consistency, maintainability, and development speed of our CLI by ensuring that command definitions are always in sync with their documentation.
Highlights
- Automated CLI Generation: I've integrated the
addertool to automate the generation of Cobra command stubs directly from markdown documentation. This significantly streamlines the process of defining and updating CLI commands, ensuring consistency between documentation and code. - Refactored Command Structure: Existing CLI commands (e.g.,
config,dev-selectors,profile,root,tdf-decrypt,tdf-encrypt,tdf-inspect) have been refactored to utilize these newly generated stubs. This involves replacing manual Cobra command definitions and flag parsing with calls toadder-generated constructors and handler functions, improving code organization and maintainability. - Enhanced Documentation-as-Code: The
docs/manmarkdown files have been updated to include richer metadata (like flag types and explicit argument definitions). This metadata is now consumed byadderto generate robust Go code, reinforcing a documentation-as-code approach for the CLI. - Build Process Updates: The
Makefilenow includes new targets (generate,clean-generated,regenerate,toolcheck) to manage theaddertool and its generated output, fully integrating it into the project's build workflow.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request is a significant step towards improving the maintainability of the CLI by introducing code generation for cobra commands from markdown files. The changes are extensive and well-structured. My feedback focuses on improving error handling consistency by returning errors from handlers instead of exiting directly, fixing a few critical issues in the generated code (like missing imports and incorrect flag types), and addressing minor inconsistencies in documentation and code.
|
|
||
| "github.com/spf13/cobra" | ||
| ) |
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.
This generated file is missing an import for the fmt package, but fmt.Errorf is used in the runOtdfctl function on line 85. This will cause a compilation error. The adder tool should be configured or fixed to include necessary imports.
| "github.com/spf13/cobra" | |
| ) | |
| "fmt" | |
| "github.com/spf13/cobra" |
| // Register persistent flags | ||
|
|
||
| // Register flags | ||
| cmd.Flags().String("all", "%!s(bool=false)", "Deprecated -- see the `profile` subcommand") |
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 all flag is generated as a String flag with a default value of "%!s(bool=false)". This is incorrect; it should be a Bool flag. This issue seems to stem from the adder tool's code generation when a boolean flag is defined in the markdown without type: bool.
You've correctly added type: bool for flags in docs/man/_index.md. Please apply the same fix to the markdown file for this command (docs/man/auth/clear-client-credentials.md) and other commands where boolean flags are being generated incorrectly.
For example, the markdown should look like this:
...
flags:
- name: all
description: Deprecated -- see the `profile` subcommand
default: false
type: bool
...This will ensure the generator creates a Bool flag, which will fix the runtime behavior. This issue is present in multiple generated files for other boolean flags as well.
| if err != nil { | ||
| c.ExitWithError("Failed to update output format", err) | ||
| } |
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 handler function handleConfigOutput has a signature that returns an error, which is the standard for cobra's RunE functions. However, on error, c.ExitWithError is called, which terminates the program immediately. This bypasses cobra's error handling mechanism.
Instead of calling c.ExitWithError, you should return a descriptive error. The generated command wrapper will then handle printing the error and exiting. This pattern should be applied to all new command handlers in this PR for consistency and proper error flow.
return fmt.Errorf("Failed to update output format: %w", err)| if len(selectorsList) == 0 { | ||
| cli.ExitWithError("Must provide at least one selector", nil) | ||
| } |
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.
| c.Println("failed") | ||
| c.ExitWithError("Failed to create profile", err) | ||
| } | ||
| c.Println("ok") |
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 handler function handleProfileCreate calls c.ExitWithError instead of returning an error. To align with cobra's RunE error handling pattern, this should be changed to return an error. This allows the caller to manage program exit and error reporting. This applies to all other handlers in this file as well.
| c.Println("failed") | |
| c.ExitWithError("Failed to create profile", err) | |
| } | |
| c.Println("ok") | |
| return fmt.Errorf("Failed to create profile: %w", err) |
|
|
||
| c.PrintJson(n) | ||
| } else { | ||
| c.ExitWithError("failed to inspect TDF", nil) |
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.
| @echo "✓ go version is compatible" | ||
| @go run github.com/jrschumacher/adder/cmd/[email protected] version >/dev/null 2>&1 || { echo >&2 "adder tool check failed. Run 'go mod download' to ensure dependencies are available."; exit 1; } | ||
| @echo "✓ adder is available" | ||
| @command -v bats >/dev/null 2>&1 || echo "⚠ bats is not installed (required for e2e tests). Install with: brew install bats-core" |
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 suggestion to install bats using brew is specific to macOS. To make this Makefile more portable for developers on other operating systems (like Linux), it would be better to provide a more generic installation instruction or link to the official bats-core installation guide.
For example, you could suggest installing via npm or from source, and link to https://github.com/bats-core/bats-core#installation.
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var selectors []string |
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.
| } | ||
|
|
||
| flattened, err := handlers.FlattenSubjectContext(subject) | ||
| if err != nil { |
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.
| shorthand: r | ||
| description: Remote URI where the public key can be retrieved for the KAS | ||
| - name: label | ||
| - name: name |
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 label flag has been removed from this markdown documentation, but it's still present in the generated code (cmd/generated/policy/kas-registry/create_generated.go). This creates an inconsistency between the documentation and the actual CLI behavior. Please either remove the flag from the generator's logic or add it back to the documentation.
14574db to
703f542
Compare
Use https://github.com/jrschumacher/adder to generate stubs from markdown.