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

add output json format to keys add command #29

Merged
merged 1 commit into from
Oct 5, 2024
Merged

Conversation

sh-cha
Copy link
Collaborator

@sh-cha sh-cha commented Oct 4, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new --output flag for the keys add command, allowing users to choose between "plain" or "json" output formats.
    • Enhanced key recovery functionality by enabling mnemonic retrieval from a specified source file.
  • Bug Fixes

    • Improved the output handling logic for better integration with other tools and automated processes.

@sh-cha sh-cha self-assigned this Oct 4, 2024
@sh-cha sh-cha requested a review from a team as a code owner October 4, 2024 10:17
Copy link
Contributor

coderabbitai bot commented Oct 4, 2024

Walkthrough

The changes in the pull request enhance the keys command set in the cmd/opinitd/key.go file by introducing a new --output flag for the keys add command. This flag allows users to specify the output format as either "plain" or "json". The command's execution logic is updated to generate a JSON representation of account details when the "json" format is selected. Additionally, the command now supports reading the mnemonic from a specified source file when using the --recover flag.

Changes

File Change Summary
cmd/opinitd/key.go Added --output flag to keys add command for output format ("plain" or "json"). Introduced keyJsonOutput and keyJsonOutputElem types for JSON handling. Updated RunE function to manage output format and added logic for mnemonic recovery.

Poem

In the land of keys, a change so bright,
A flag for output, plain or JSON light.
With mnemonics recovered, oh what a delight,
Tools can now dance, in data's sweet flight!
🐇✨


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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: 0

🧹 Outside diff range and nitpick comments (3)
cmd/opinitd/key.go (3)

44-49: LGTM: New types for JSON output structure.

The new types keyJsonOutput and keyJsonOutputElem are well-structured for the JSON output feature. The use of a map for keyJsonOutput allows for potential future expansion to multiple keys.

Consider adding a comment to explain the purpose of these types, especially if they might be used in other parts of the codebase in the future.


151-169: LGTM: Implementation of output format handling.

The new logic for handling different output formats is well-implemented. It correctly uses a switch statement to differentiate between JSON and plain text output, and properly marshals the data for JSON output.

Consider adding an else if clause for the "plain" format explicitly, rather than using it as the default case. This would make the code more robust against potential future additions of new output formats.

 switch outputFormat {
 case "json":
     // ... (existing JSON handling code)
+case "plain":
+    output = fmt.Sprintf("%s: %s\n%s", account.Name, addrString, mnemonic)
 default:
-    output = fmt.Sprintf("%s: %s\n%s", account.Name, addrString, mnemonic)
+    return fmt.Errorf("unsupported output format: %s", outputFormat)
 }

Line range hint 108-120: LGTM: Implementation of mnemonic recovery from file.

The new functionality to read a mnemonic from a file enhances the key recovery feature. The implementation is correct, with proper error handling and whitespace trimming.

Consider adding a check for the file size before reading its entire content to prevent potential issues with very large files:

 if mnemonicSrc != "" {
     file, err := os.Open(mnemonicSrc)
     if err != nil {
         return err
     }
+    fileInfo, err := file.Stat()
+    if err != nil {
+        file.Close()
+        return err
+    }
+    if fileInfo.Size() > 1024 { // 1KB limit, adjust as needed
+        file.Close()
+        return fmt.Errorf("mnemonic file too large: %d bytes", fileInfo.Size())
+    }
     bz, err := io.ReadAll(file)
     file.Close()
     if err != nil {
         return err
     }
     mnemonic = strings.TrimSpace(string(bz))
 } else {
     // ... (existing code for manual input)
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fd8a58c and a1bc543.

📒 Files selected for processing (1)
  • cmd/opinitd/key.go (4 hunks)
🔇 Additional comments (4)
cmd/opinitd/key.go (4)

21-21: LGTM: New import and constant for JSON output.

The addition of the encoding/json import and the flagOutput constant are appropriate for implementing the new JSON output feature.

Also applies to: 41-41


81-82: LGTM: Updated command examples.

The new examples for the keys add command clearly demonstrate the usage of the --recover flag with a source file and the new --output json option. These additions improve the command's documentation and usability.


176-176: LGTM: New flag for output format.

The addition of the --output flag is correct and consistent with the new functionality. The default value "plain" maintains backwards compatibility, and the description clearly indicates the available options.


Line range hint 1-420: Overall assessment: Well-implemented feature additions.

The changes to this file successfully implement JSON output capability for the keys add command and enhance the mnemonic recovery feature. The new code is well-integrated, maintains existing error handling patterns, and follows the established coding style. These additions improve the flexibility and usability of the key management commands without disrupting existing functionality.

To ensure the changes don't introduce any regressions, please run the following verification script:

This script tests the basic functionality of the keys add command with both default and JSON output, as well as the new mnemonic recovery from file feature.

Copy link
Member

@beer-1 beer-1 left a comment

Choose a reason for hiding this comment

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

LGTM

@beer-1 beer-1 merged commit 89a72c8 into main Oct 5, 2024
4 checks passed
@beer-1 beer-1 deleted the feat/output-json branch October 5, 2024 04:21
@coderabbitai coderabbitai bot mentioned this pull request Dec 24, 2024
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