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

fix(cmd): set defaults for command output #4071

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

rootulp
Copy link
Collaborator

@rootulp rootulp commented Dec 2, 2024

Closes #3823
Motivation celestiaorg/cosmos-sdk#420 (comment)

Testing

make build
./build/celestia-appd status | jq .

works

@rootulp rootulp self-assigned this Dec 2, 2024
@rootulp rootulp requested a review from a team as a code owner December 2, 2024 15:25
@rootulp rootulp requested review from rach-id and evan-forbes and removed request for a team December 2, 2024 15:25
@celestia-bot celestia-bot requested a review from a team December 2, 2024 15:25
Copy link
Contributor

coderabbitai bot commented Dec 2, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request modify the PersistentPreRunE function within the NewRootCmd function in cmd/root.go. Two lines have been added to set the output and error streams for the command, ensuring that output is directed to the appropriate streams (stdout and stderr). This adjustment aims to improve the usability of the command, particularly for output redirection.

Changes

File Path Change Summary
cmd/celestia-appd/cmd/root.go Added lines to set output and error streams using command.SetOut and command.SetErr.

Assessment against linked issues

Objective Addressed Explanation
Send celestia-appd status output to stdout instead of stderr (#3823)

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5463787 and a218f0d.

📒 Files selected for processing (1)
  • cmd/celestia-appd/cmd/root.go (1 hunks)
🔇 Additional comments (2)
cmd/celestia-appd/cmd/root.go (2)

59-61: Consider the impact on other commands.

Since this change is in PersistentPreRunE, it affects all subcommands. While this is generally safe as it uses standard output methods, verify that no subcommands rely on custom output handling.

✅ Verification successful

Let me analyze the results and generate the final response.

Setting command output is safe across all subcommands

The search results show that the only direct usage of command.SetOut/SetErr is in the root command's PersistentPreRunE. All other output handling in the codebase either:

  • Uses loggers in test utilities and benchmarks
  • Uses direct os.Stdout/Stderr for specific CLI feedback in payforblob command
  • Uses standard logging for verification tasks

There's no evidence of subcommands relying on custom output handling that would be affected by this change.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any commands that might override or depend on custom output handling

# Look for any custom output handling in commands
rg -A 3 'command\.(Out|Err|OutOrStdout|ErrOrStderr)' --type go

# Check for any direct writes to os.Stdout or os.Stderr
rg -A 3 'os\.(Stdout|Stderr)' --type go

Length of output: 3026


59-61: LGTM! The implementation correctly sets output streams.

The change appropriately uses Cobra's built-in methods to set output streams, which should fix the stderr redirection issue for all commands including status.

Let's verify that this change fixes the specific issue:

✅ Verification successful

The output stream configuration is correctly implemented and won't be overridden

The verification shows that:

  • The SetOut/SetErr usage in root.go correctly configures output streams at the root command level
  • The only other usage is in test code (addr_converter_test.go), which uses a buffer for test assertions and doesn't affect production behavior
  • The status command is properly registered and will inherit these stream settings
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the status command output goes to stdout
# Note: This script helps verify the fix by checking the command implementation

# Check if the status command is properly configured
ast-grep --pattern 'rpc.StatusCommand()' 

# Verify there are no other places overriding the output stream for status command
rg -l 'SetOut|SetErr' --type go

Length of output: 208


Script:

#!/bin/bash
# Let's check the content of addr_converter_test.go to ensure it doesn't override our settings
cat cmd/celestia-appd/cmd/addr_converter_test.go

# Also check the actual usage of SetOut/SetErr in root.go for completeness
rg 'SetOut|SetErr' --type go -A 2 -B 2

Length of output: 2359


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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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.

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
Member

@evan-forbes evan-forbes left a comment

Choose a reason for hiding this comment

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

I'm good with this change, but if this is across all outputs (such as logs etc) we need to document that in the upgrade notes for v4

Comment on lines +59 to +60
command.SetOut(command.OutOrStdout())
command.SetErr(command.ErrOrStderr())
Copy link
Member

Choose a reason for hiding this comment

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

does this change all logs to go to the output instead of the error? if so, is a breaking change for things that collect logs?

Copy link
Member

Choose a reason for hiding this comment

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

if I understand correctly, this is just for the prints. the logs are handled separatly:

	var logWriter io.Writer
	if strings.ToLower(serverCtx.Viper.GetString(flags.FlagLogFormat)) == tmcfg.LogFormatPlain {
		logWriter = zerolog.ConsoleWriter{Out: os.Stderr}
	} else {
		logWriter = os.Stderr
	}

	logLvlStr := serverCtx.Viper.GetString(flags.FlagLogLevel)
	logLvl, err := zerolog.ParseLevel(logLvlStr)
	if err != nil {
		return fmt.Errorf("failed to parse log level (%s): %w", logLvlStr, err)
	}

	serverCtx.Logger = ZeroLogWrapper{zerolog.New(logWriter).Level(logLvl).With().Timestamp().Logger()}

Rootul can correct me

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

does this change all logs to go to the output instead of the error?

Output that targets stderr will still go to stderr. This fixes an issue where commands that used Cobra's

// Println is a convenience method to Println to the defined output, fallback to Stderr if not set.
func (c *Command) Println(i ...interface{}) {
	c.Print(fmt.Sprintln(i...))
}

would previously go to stderr because stdout wasn't configured for the command. The status command is an example that used this Println method

Copy link
Member

Choose a reason for hiding this comment

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

nice, makes sense

it might still be a bit confusing to anyone reading the code, as the logs are set to go to stderr in the sdk

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

as the logs are set to go to stderr in the sdk

huh that seems unexpected. I'll try running a node pre / post this change to double check that I didn't break something

Copy link
Collaborator Author

@rootulp rootulp Dec 3, 2024

Choose a reason for hiding this comment

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

wow you're right. the SDK does send logs to STDERR. Before (and after) this change, error.txt contains logs from the node:

./scripts/single-node.sh > output.txt 2> error.txt
# output.txt contains output from the script 
# error.txt contains logs from the node which is unexpected

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Given that behavior is related but existed before this change, I'm inclined to merge this as a fix for the linked issue and create a new one for this weird behavior.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Created #4076

@evan-forbes evan-forbes added the backport:v3.x PR will be backported automatically to the v3.x branch upon merging label Dec 2, 2024
@rootulp rootulp merged commit 615f1dc into celestiaorg:main Dec 3, 2024
32 checks passed
@rootulp rootulp deleted the rp/set-cmd-output branch December 3, 2024 14:22
mergify bot pushed a commit that referenced this pull request Dec 3, 2024
Closes #3823
Motivation
celestiaorg/cosmos-sdk#420 (comment)

## Testing

```bash
make build
./build/celestia-appd status | jq .
```
works

(cherry picked from commit 615f1dc)
rootulp added a commit that referenced this pull request Dec 4, 2024
Closes #3823
Motivation
celestiaorg/cosmos-sdk#420 (comment)

## Testing

```bash
make build
./build/celestia-appd status | jq .
``` 
works

<hr>This is an automatic backport of pull request #4071 done by
[Mergify](https://mergify.com).

Co-authored-by: Rootul P <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:v3.x PR will be backported automatically to the v3.x branch upon merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

celestia-appd status sends output to stderr
3 participants