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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/cosmovisor/cmd/cosmovisor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func NewRootCmd() *cobra.Command {
configCmd,
NewVersionCmd(),
NewAddUpgradeCmd(),
NewShowUpgradeInfoCmd(),
)

rootCmd.PersistentFlags().StringP(cosmovisor.FlagCosmovisorConfig, "c", "", "path to cosmovisor config file")
Expand Down
35 changes: 35 additions & 0 deletions tools/cosmovisor/cmd/cosmovisor/show_upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

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.

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

Args: cobra.ExactArgs(1),
RunE: showUpgradeInfoCmd,
}

return showUpgradeInfo
}

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


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.

🐰😊

Loading