Skip to content
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

Various completion improvements #140

Merged
merged 4 commits into from
Apr 8, 2024
Merged

Various completion improvements #140

merged 4 commits into from
Apr 8, 2024

Conversation

scop
Copy link
Contributor

@scop scop commented Apr 7, 2024

See individual commits for details.

Summary by CodeRabbit

  • New Features

    • Enhanced command-line argument handling and flag completions across various commands for improved user experience.
    • Added new arguments and flag completions for file paths and CPU core usage in the import subcommand.
    • Introduced validation for the minimum number of arguments required for the remove command.
  • Refactor

    • Updated several commands for consistent argument handling and improved command behavior.

Copy link
Contributor

coderabbitai bot commented Apr 7, 2024

Walkthrough

The recent updates focus on enhancing argument handling and flag completions across various commands, ensuring consistent behavior and user experience. Improvements include setting specific Args and ValidArgsFunction, adding new arguments and validations, and introducing CPU core usage flag completions. These changes aim to streamline command functionality and provide clearer, more efficient interactions.

Changes

Files Change Summary
cmd/bug_report.go, cmd/export.go, cmd/man.go, cmd/version.go Updated Args and ValidArgsFunction for consistent argument handling.
cmd/check.go Set Args to cobra.NoArgs, added ValidArgsFunction and registered completeNCPUs.
cmd/import.go Added new arguments and flag completions for file paths and CPU core usage.
cmd/list.go Enhanced with specific Args and ValidArgsFunction settings.
cmd/remove.go Added validation for minimum arguments required.
cmd/root.go Imported runtime, strconv, and defined completeNCPUs function.
cmd/update.go Registered flag completions for "exclude", "main", and "jobs" flags.

🐰✨
In the garden of code, where the commands do bloom,
A rabbit hopped in, with a brush and a broom.
"Let's tidy this up," it said with a cheer,
Adding flags and args, making purpose clear.
Now every command, so sleek and so fine,
Works just as intended, by design.
🌟🎉🐇

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 42c5e8a and f5ddf3f.
Files selected for processing (10)
  • cmd/bug_report.go (1 hunks)
  • cmd/check.go (1 hunks)
  • cmd/export.go (1 hunks)
  • cmd/import.go (2 hunks)
  • cmd/list.go (1 hunks)
  • cmd/man.go (1 hunks)
  • cmd/remove.go (1 hunks)
  • cmd/root.go (2 hunks)
  • cmd/update.go (1 hunks)
  • cmd/version.go (1 hunks)
Additional comments not posted (13)
cmd/version.go (1)

12-15: The changes to enforce no arguments and disable file completions for the version command are appropriate and align with the PR's objectives.

cmd/list.go (1)

15-19: The modifications to enforce no arguments and disable file completions for the list command are well-aligned with the PR's goals.

cmd/bug_report.go (1)

13-18: The updates to enforce no arguments and disable file completions for the bug-report command align well with the PR's goals.

cmd/root.go (2)

6-7: The addition of runtime and strconv imports supports the new functionality introduced in this PR.


65-71: The implementation of completeNCPUs for calculating and returning CPU numbers as strings for flag completion is well-aligned with the PR's objectives.

cmd/remove.go (1)

24-24: The addition of argument validation to enforce a minimum of one argument for the remove command enhances its robustness and user guidance.

cmd/import.go (2)

24-25: The updates to enforce no arguments and disable file completions for the import subcommand align well with the PR's goals.


34-40: The addition of new arguments and flag completions for specifying file paths and CPU core usage significantly enhances the import subcommand's functionality and user experience.

cmd/man.go (1)

19-24: The updates to enforce no arguments and disable file completions for the man command align well with the PR's goals.

cmd/export.go (1)

26-27: The modifications to enforce no arguments and disable file completions for the export command are well-aligned with the PR's objectives.

cmd/check.go (1)

25-26: Setting Args to cobra.NoArgs and ValidArgsFunction to cobra.NoFileCompletions correctly standardizes command behavior for commands that do not require arguments or file completions.

cmd/update.go (2)

37-39: Registering the "exclude" flag with cobra.NoFileCompletions is a good practice, as it guides users towards providing valid input values.


41-43: Consistently applying cobra.NoFileCompletions to the "main" flag, as with other flags, helps maintain a uniform user experience.

Comment on lines +33 to +35
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
panic(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider handling the error from RegisterFlagCompletionFunc more gracefully instead of panicking. This could improve the robustness of the application by allowing it to handle unexpected situations more gracefully.

- panic(err)
+ print.Err(fmt.Errorf("failed to register flag completion for 'jobs': %w", err))

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
panic(err)
}
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
print.Err(fmt.Errorf("failed to register flag completion for 'jobs': %w", err))
}

Comment on lines +46 to +48
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
panic(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned in the review of cmd/check.go, consider handling the error from RegisterFlagCompletionFunc more gracefully instead of panicking to enhance application robustness.

- panic(err)
+ print.Err(fmt.Errorf("failed to register flag completion for 'jobs': %w", err))

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
panic(err)
}
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
print.Err(fmt.Errorf("failed to register flag completion for 'jobs': %w", err))
}

Copy link
Owner

@nao1215 nao1215 left a comment

Choose a reason for hiding this comment

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

@scop
I really appreciate your contribution.

However, there's a small point I'd like you to revise. Your code is excellent, but there are just a few parts that don't quite align with my thinking.

// cmd.Flags().BoolP("main-all", "M", false, "update all binaries by @main or @master (delimiter: ',')")
cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use")
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
panic(err)
Copy link
Owner

Choose a reason for hiding this comment

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

@scop
"gup" is not a library, it's a small-scale code. I don't think "gup" encounters situations where panic() is necessary. It would be preferable to output error messages instead.

Copy link
Contributor Author

@scop scop Apr 7, 2024

Choose a reason for hiding this comment

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

In my opinion, if gup was a library, panic would be completely unacceptable :)

Regarding this case, the only case where registering a flag completion function returns an error is when it is attempted for a flag that does not exist, or for which a completion function is already registered. Both are permanent programming errors on the gup side, and not really expected to happen. Therefore I think panic is appropriate here, and it's good that it happens up front when invoking any gup command, so it's really easily noticeable, no need for any fancier error handling or need to be more "gentle" about it. For ref, see https://github.com/spf13/cobra/blob/6b5f577ebce858ee70fcdd1f062ea3af4b1c03ab/completions.go#L135-L148

If you still disagree, let me know and I can see about doing it some other more elaborate way.

Copy link
Owner

@nao1215 nao1215 Apr 7, 2024

Choose a reason for hiding this comment

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

I wrote a long comment, but the conclusion is "no need for modifications (this code is ok, good job)"

In my opinion, if gup was a library, panic would be completely unacceptable :)

This time, the part where you added panic() is within the new method. If gup were a library, I would tolerate embedding panic() in such places. I use panic() as an assert. That is, when something that I believe should never happen occurs, it indicates (panic()) that the programmer is misusing the method.

However, in the case of a command-line interface, users may not necessarily be developers, so I want to display more understandable error messages. Furthermore, putting panic() makes it harder to test, so I don't like using panic().

However, writing unit tests for initialization code is absurd. Therefore, I would like to adopt your opinion. Reading your thoughtful opinion, I felt that you are an excellent engineer.

Copy link
Owner

@nao1215 nao1215 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for your contributions!!

@nao1215 nao1215 merged commit 3742bde into nao1215:main Apr 8, 2024
11 checks passed
@scop scop deleted the feat/completion-improvements branch April 8, 2024 21:17
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.

2 participants