-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
(serverv2/cometbft) Commands support both json and yaml output #20670
Conversation
WalkthroughThe refactoring in Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CometBFTServer
participant cobra.Command
User ->> CometBFTServer: Invoke statusCommand()
CometBFTServer ->> cobra.Command: cmd.ExecuteC()
cobra.Command ->> CometBFTServer: Run cmd function (statusCommand)
CometBFTServer ->> CometBFTServer: Fetch status data
CometBFTServer ->> CometBFTServer: printOutput(cmd, data)
CometBFTServer ->> cobra.Command: Write output based on format
cobra.Command ->> User: Display results
Assessment against linked issues
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
@hieuvubk your pull request is missing a changelog! |
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.
Actionable comments posted: 2
Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (1)
- server/v2/cometbft/commands.go (6 hunks)
Additional context used
Path-based instructions (1)
server/v2/cometbft/commands.go (1)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
|
||
// TODO: figure out yaml and json output | ||
return nil | ||
return printOutput(cmd, output) |
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.
Tip
Codebase Verification
Ensure consistent output handling by replacing cmd.Println
with printOutput
.
- server/v2/cometbft/commands.go:
- Line 75:
cmd.Println(nodeKey.ID())
- Line 220:
cmd.Println(sdkPK)
- Line 284:
cmd.Println(valConsAddr.String())
- Line 307:
cmd.Println(string(bs))
- Line 75:
Analysis chain
Ensure consistent output handling across commands.
This script checks if any command directly prints the output instead of using the printOutput
function, which could lead to inconsistencies in how outputs are handled across different commands.
Also applies to: 220-220, 284-284, 307-307, 363-363
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify that all command functions use `printOutput` consistently for handling outputs.
# Test: Search for inconsistent usage of output handling in command functions. Expect: Uniform usage of `printOutput`.
rg --type go "cmd.Println" -g "commands.go"
Length of output: 405
Script:
#!/bin/bash
# Extract relevant lines and surrounding context from commands.go to review the usage of `cmd.Println`.
# Get the lines around each usage of `cmd.Println` to understand the context.
rg --type go --context 5 "cmd.Println" -g "commands.go"
Length of output: 2640
func printOutput(cmd *cobra.Command, out []byte) error { | ||
// Get flags output | ||
outFlag, err := cmd.Flags().GetString(flags.FlagOutput) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if outFlag == "text" { | ||
out, err = yaml.JSONToYAML(out) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
writer := cmd.OutOrStdout() | ||
_, err = writer.Write(out) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if outFlag != "text" { | ||
// append new-line for formats besides YAML | ||
_, err = writer.Write([]byte("\n")) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return 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.
Refactor error handling to streamline the function.
func printOutput(cmd *cobra.Command, out []byte) error {
outFlag, err := cmd.Flags().GetString(flags.FlagOutput)
if err != nil {
return err
}
writer := cmd.OutOrStdout()
if outFlag == "text" {
out, err = yaml.JSONToYAML(out)
if err != nil {
return err
}
_, err = writer.Write(out)
} else {
_, err = writer.Write(out)
if err == nil {
_, err = writer.Write([]byte("\n"))
}
}
return err
}
This refactor simplifies the branching logic by handling the write operation in a single block and ensuring the new line is added only in non-YAML formats.
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. Thoroughly test & benchmark the code to ensure it meets the requirements.
func printOutput(cmd *cobra.Command, out []byte) error { | |
// Get flags output | |
outFlag, err := cmd.Flags().GetString(flags.FlagOutput) | |
if err != nil { | |
return err | |
} | |
if outFlag == "text" { | |
out, err = yaml.JSONToYAML(out) | |
if err != nil { | |
return err | |
} | |
} | |
writer := cmd.OutOrStdout() | |
_, err = writer.Write(out) | |
if err != nil { | |
return err | |
} | |
if outFlag != "text" { | |
// append new-line for formats besides YAML | |
_, err = writer.Write([]byte("\n")) | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
func printOutput(cmd *cobra.Command, out []byte) error { | |
outFlag, err := cmd.Flags().GetString(flags.FlagOutput) | |
if err != nil { | |
return err | |
} | |
writer := cmd.OutOrStdout() | |
if outFlag == "text" { | |
out, err = yaml.JSONToYAML(out) | |
if err != nil { | |
return err | |
} | |
_, err = writer.Write(out) | |
} else { | |
_, err = writer.Write(out) | |
if err == nil { | |
_, err = writer.Write([]byte("\n")) | |
} | |
} | |
return err | |
} |
* main: feat(tx): port simappv2 changes (#20648) build(deps): Bump github.com/spf13/cobra from 1.8.0 to 1.8.1 (#20682) chore(proto): fix comments (#20679) (serverv2/cometbft) Commands support both json and yaml output (#20670) fix(x/staking,x/auth): regenerate mock to fix test (#20684) docs: ADR 074: Msg v2 (#20618) fix: nested multisig signatures using CLI (#20438) chore: fix spelling errors (#20674) fix: align Dockerfile for build-push-action (#20672) fix: avoid build fail when make localnet-build-env (#20671) build(deps): Bump bufbuild/buf-setup-action from 1.32.2 to 1.33.0 (#20669) chore: make function comment match function names (#20666) chore(consensus): add cometInfo to consensus (#20615) chore: fix typos (#20662) fix: Properly parse json in the wait-tx command. (#20631) fix(sims): check before sending RotateConsPubKey (#20659) test(types/address): add unit tests for the file types/address.go (#20237)
Description
Closes: #20495
Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
!
in the type prefix if API or client breaking changeCHANGELOG.md
Reviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.
I have...
Summary by CodeRabbit
printOutput
function.