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

feat(tools/cosmovisor): Add ShowUpgradeInfoCmd #21932

Merged
merged 10 commits into from
Sep 28, 2024
Merged

Conversation

Teyz
Copy link
Contributor

@Teyz Teyz commented Sep 26, 2024

Description

Closes: #21726


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

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

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

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a new command show-upgrade-info to display upgrade information from a specified JSON file.
    • Enhanced command-line interface functionality for better user experience.
  • Bug Fixes

    • Improved error handling for file reading to provide clearer feedback on issues.

@Teyz Teyz requested review from julienrbrt, facundomedica and a team as code owners September 26, 2024 13:25
Copy link
Contributor

coderabbitai bot commented Sep 26, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduce a new command, show-upgrade-info, to the Cosmovisor application, allowing users to display upgrade information from a specified JSON file. This command is implemented in two files: the root command file, which integrates the new command, and a dedicated file that defines its behavior, including argument validation and file reading.

Changes

File Change Summary
tools/cosmovisor/cmd/cosmovisor/root.go, tools/cosmovisor/cmd/cosmovisor/show_upgrade.go Added a new command NewShowUpgradeInfoCmd() for showing upgrade information, including argument handling and file reading logic.

Assessment against linked issues

Objective Addressed Explanation
1) Add command to read and display contents of upgrade-info.json (#[21726])
2) Ensure command handles file not found scenario (#[21726])

Possibly related PRs

Suggested reviewers

  • julienrbrt
  • tac0turtle
  • aaronc
  • sontrinh16

📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6bbf2ac and dd23fae.

📒 Files selected for processing (2)
  • tools/cosmovisor/CHANGELOG.md (1 hunks)
  • tools/cosmovisor/cmd/cosmovisor/show_upgrade.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tools/cosmovisor/CHANGELOG.md
  • tools/cosmovisor/cmd/cosmovisor/show_upgrade.go

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.

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.

@Teyz Teyz changed the title Feat #21726 DRAFT: Feat #21726 Sep 26, 2024
@Teyz Teyz changed the title DRAFT: Feat #21726 DRAFT: Chores #21726 Sep 26, 2024
@github-actions github-actions bot added the C:Cosmovisor Issues and PR related to Cosmovisor label Sep 26, 2024
@Teyz Teyz changed the title DRAFT: Chores #21726 DRAFT: feat(cosmovisor) #21726 Sep 26, 2024
@Teyz Teyz changed the title DRAFT: feat(cosmovisor) #21726 DRAFT: feat(cosmovisor) Add ShowUpgradeInfoCmd #21726 Sep 26, 2024
@Teyz Teyz changed the title DRAFT: feat(cosmovisor) Add ShowUpgradeInfoCmd #21726 feat(cosmovisor) Add ShowUpgradeInfoCmd #21726 Sep 26, 2024
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: 1

🧹 Outside diff range and nitpick comments (2)
tools/cosmovisor/cmd/cosmovisor/root.go (1)

22-22: LGTM! Consider grouping related commands.

The addition of NewShowUpgradeInfoCmd() is well-integrated and follows the existing naming conventions. It aligns with the PR's objective of introducing new features to the Cosmos SDK.

For better code organization, consider grouping related commands together. For instance, you might want to place NewShowUpgradeInfoCmd() next to NewAddUpgradeCmd() since they both deal with upgrades. This could improve readability and maintainability of the code.

 rootCmd.AddCommand(
 	NewIntCmd(),
 	runCmd,
 	configCmd,
 	NewVersionCmd(),
 	NewAddUpgradeCmd(),
+	NewShowUpgradeInfoCmd(),
 )
tools/cosmovisor/cmd/cosmovisor/show_upgrade.go (1)

10-20: LGTM: Well-structured Cobra command setup.

The NewShowUpgradeInfoCmd function is well-implemented and follows Golang and Cobra best practices. The command configuration is correct, including the use of SilenceUsage and Args.

Consider enhancing the Short description to provide more context:

-		Short:        "Show upgrade-info.json into stdout.",
+		Short:        "Display the contents of upgrade-info.json to stdout.",
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f927e9b and d258cff.

📒 Files selected for processing (2)
  • tools/cosmovisor/cmd/cosmovisor/root.go (1 hunks)
  • tools/cosmovisor/cmd/cosmovisor/show_upgrade.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
tools/cosmovisor/cmd/cosmovisor/root.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

tools/cosmovisor/cmd/cosmovisor/show_upgrade.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🔇 Additional comments (1)
tools/cosmovisor/cmd/cosmovisor/show_upgrade.go (1)

3-8: LGTM: Import statements are correct and well-organized.

The import statements are properly structured with standard library imports followed by third-party imports. All imported packages are used in the code.

Comment on lines 22 to 35
func showUpgradeInfoCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("expected exactly one argument")
}

data, err := os.ReadFile(args[0])
if err != nil {
return fmt.Errorf("failed to read upgrade-info.json: %w", err)
}

fmt.Println(string(data))

return nil
}
Copy link
Contributor

@coderabbitai coderabbitai bot Sep 26, 2024

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Suggestions for improving error handling and output method.

The showUpgradeInfoCmd function is generally well-implemented, but there are a few areas for improvement:

  1. The argument check is redundant due to cobra.ExactArgs(1) in the command setup.
  2. Error handling could be more specific, especially for the "file not found" case.
  3. Directly printing to stdout might limit testability and reusability.

Consider applying these changes:

 func showUpgradeInfoCmd(cmd *cobra.Command, args []string) error {
-	if len(args) != 1 {
-		return fmt.Errorf("expected exactly one argument")
-	}
-
 	data, err := os.ReadFile(args[0])
 	if err != nil {
+		if os.IsNotExist(err) {
+			return fmt.Errorf("upgrade-info.json not found at %s: %w", args[0], err)
+		}
 		return fmt.Errorf("failed to read upgrade-info.json: %w", err)
 	}
 
-	fmt.Println(string(data))
+	_, err = fmt.Fprintln(cmd.OutOrStdout(), string(data))
+	if err != nil {
+		return fmt.Errorf("failed to write output: %w", err)
+	}
 
 	return nil
 }

These changes improve error specificity, remove redundant checks, and use cmd.OutOrStdout() for better testability.

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

Suggested change
func showUpgradeInfoCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("expected exactly one argument")
}
data, err := os.ReadFile(args[0])
if err != nil {
return fmt.Errorf("failed to read upgrade-info.json: %w", err)
}
fmt.Println(string(data))
return nil
}
func showUpgradeInfoCmd(cmd *cobra.Command, args []string) error {
data, err := os.ReadFile(args[0])
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("upgrade-info.json not found at %s: %w", args[0], err)
}
return fmt.Errorf("failed to read upgrade-info.json: %w", err)
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), string(data))
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
return nil
}

Copy link
Member

Choose a reason for hiding this comment

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

The bot has great suggestion, could you implement them?

Copy link
Contributor

Choose a reason for hiding this comment

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

@julienrbrt, certainly! I've implemented the bot's suggestions.

Here's the updated show_upgrade.go file with the improvements applied:

package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

func NewShowUpgradeInfoCmd() *cobra.Command {
	showUpgradeInfo := &cobra.Command{
		Use:          "show-upgrade-info <path to executable>",
		Short:        "Show upgrade-info.json into stdout.",
		SilenceUsage: true,
		Args:         cobra.ExactArgs(1),
		RunE:         showUpgradeInfoCmd,
	}

	return showUpgradeInfo
}

func showUpgradeInfoCmd(cmd *cobra.Command, args []string) error {
	data, err := os.ReadFile(args[0])
	if err != nil {
		if os.IsNotExist(err) {
			return fmt.Errorf("upgrade-info.json not found at %s: %w", args[0], err)
		}
		return fmt.Errorf("failed to read upgrade-info.json: %w", err)
	}

	_, err = fmt.Fprintln(cmd.OutOrStdout(), string(data))
	if err != nil {
		return fmt.Errorf("failed to write output: %w", err)
	}

	return nil
}

These changes improve error specificity, remove the redundant argument check, and use cmd.OutOrStdout() for better testability, as suggested.

🐰😊

@julienrbrt julienrbrt changed the title feat(cosmovisor) Add ShowUpgradeInfoCmd #21726 feat(cosmovisor) Add ShowUpgradeInfoCmd Sep 26, 2024
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

Thank you for this PR! I have left some comments.

Comment on lines 22 to 35
func showUpgradeInfoCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("expected exactly one argument")
}

data, err := os.ReadFile(args[0])
if err != nil {
return fmt.Errorf("failed to read upgrade-info.json: %w", err)
}

fmt.Println(string(data))

return nil
}
Copy link
Member

Choose a reason for hiding this comment

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

The bot has great suggestion, could you implement them?

return fmt.Errorf("failed to read upgrade-info.json: %w", err)
}

fmt.Println(string(data))
Copy link
Member

Choose a reason for hiding this comment

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

Let's use cmd.Println(string(data)) instead


func NewShowUpgradeInfoCmd() *cobra.Command {
showUpgradeInfo := &cobra.Command{
Use: "show-upgrade-info <path to executable>",
Copy link
Member

Choose a reason for hiding this comment

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

The goal of the issue wasn't to pass an upgrade-info location, but to get it from the cosmovisor node config. We have the root of the node, so we can know the path of the executable. Taking an argument doesn't close the issue.

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

utACK!

showUpgradeInfo := &cobra.Command{
Use: "show-upgrade-info",
Short: "Show upgrade-info.json into stdout.",
SilenceUsage: true,
Copy link
Member

Choose a reason for hiding this comment

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

Let's not silence usage.
And let's add cobra.NoArgs

@julienrbrt
Copy link
Member

The linter complains, you need to run make lint-fix as wll

@julienrbrt
Copy link
Member

could you add a changelog under tools/cosmovisor/changelog.md

@@ -67,6 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#16550](https://github.com/cosmos/cosmos-sdk/pull/16550) Add COSMOVISOR_CUSTOM_PREUPGRADE to cosmovisor to execute custom pre-upgrade scripts (separate from daemon `pre-upgrade` command).
* [#16963](https://github.com/cosmos/cosmos-sdk/pull/16963) Add DAEMON_SHUTDOWN_GRACE to send interrupt and wait before sending kill.
* [#15361](https://github.com/cosmos/cosmos-sdk/pull/15361) Add `cosmovisor config` command to display the configuration used by cosmovisor.
* [#21932](https://github.com/cosmos/cosmos-sdk/pull/21932) Add `cosmovisor show-upgrade-info` command to display the upgrade-info.json into stdout.
Copy link
Member

Choose a reason for hiding this comment

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

the changelog should be under unreleased features.

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 (1)
tools/cosmovisor/CHANGELOG.md (1)

70-71: LGTM with a minor suggestion.

The new changelog entry is correctly placed and formatted. It accurately describes the new feature added in PR #21932. However, to improve clarity, consider rewording slightly:

Consider this minor rewording for improved clarity:

-* [#21932](https://github.com/cosmos/cosmos-sdk/pull/21932) Add `cosmovisor show-upgrade-info` command to display the upgrade-info.json into stdout.
+* [#21932](https://github.com/cosmos/cosmos-sdk/pull/21932) Add `cosmovisor show-upgrade-info` command to display the contents of upgrade-info.json to stdout.

This change more explicitly states that the command displays the contents of the file, which aligns better with the PR objectives.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e20269d and 6bbf2ac.

📒 Files selected for processing (1)
  • tools/cosmovisor/CHANGELOG.md (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
tools/cosmovisor/CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

@julienrbrt
Copy link
Member

there are still build issues here, could you fix?

@Teyz
Copy link
Contributor Author

Teyz commented Sep 28, 2024

@julienrbrt I would like to fix but I still have errors when I want to run : go mod tidy and make lint ...

@julienrbrt
Copy link
Member

@julienrbrt I would like to fix but I still have errors when I want to run : go mod tidy and make lint ...

Maybe you can try to use GitHub codespaces? It should work from there.

@Teyz
Copy link
Contributor Author

Teyz commented Sep 28, 2024

Should be good @julienrbrt

@Teyz Teyz changed the title feat(cosmovisor) Add ShowUpgradeInfoCmd feat(tools/cosmovisor): Add ShowUpgradeInfoCmd Sep 28, 2024
@julienrbrt julienrbrt added this pull request to the merge queue Sep 28, 2024
Merged via the queue into cosmos:main with commit e82949d Sep 28, 2024
68 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:Cosmovisor Issues and PR related to Cosmovisor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Cosmovisor: add a command to show current upgrade-info
3 participants