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(cosmovisor): fix show-upgrade info command #22010

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all 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
57 changes: 25 additions & 32 deletions tools/cosmovisor/cmd/cosmovisor/show_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,33 @@ import (
)

func NewShowUpgradeInfoCmd() *cobra.Command {
showUpgradeInfo := &cobra.Command{
return &cobra.Command{
Use: "show-upgrade-info",
Short: "Show upgrade-info.json into stdout.",
Short: "Display current upgrade-info.json from <app> data directory",
SilenceUsage: false,
Args: cobra.NoArgs,
RunE: showUpgradeInfoCmd,
RunE: func(cmd *cobra.Command, args []string) error {
configPath, err := cmd.Flags().GetString(cosmovisor.FlagCosmovisorConfig)
if err != nil {
return fmt.Errorf("failed to get config flag: %w", err)
}

cfg, err := cosmovisor.GetConfigFromFile(configPath)
if err != nil {
return err
}

data, err := os.ReadFile(cfg.UpgradeInfoFilePath())
if err != nil {
if os.IsNotExist(err) {
cmd.Printf("No upgrade info found at %s\n", cfg.UpgradeInfoFilePath())
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Suggestion: Use a constant for the file name

Consider defining a constant for the upgrade-info.json file name. This would improve maintainability and reduce the risk of typos if the file name is used in multiple places.

Here's a suggested change:

+const upgradeInfoFileName = "upgrade-info.json"

// In the NewShowUpgradeInfoCmd function
-cmd.Printf("No upgrade info found at %s\n", cfg.UpgradeInfoFilePath())
+cmd.Printf("No %s found at %s\n", upgradeInfoFileName, cfg.UpgradeInfoFilePath())

This change would require adding the constant at the package level.

Committable suggestion was skipped due to low confidence.

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

cmd.Println(string(data))
return nil
},
}

return showUpgradeInfo
}

func showUpgradeInfoCmd(cmd *cobra.Command, args []string) error {
configPath, err := cmd.Flags().GetString(cosmovisor.FlagCosmovisorConfig)
if err != nil {
return fmt.Errorf("failed to get config flag: %w", err)
}

cfg, err := cosmovisor.GetConfigFromFile(configPath)
if err != nil {
return err
}

data, err := os.ReadFile(cfg.UpgradeInfoFilePath())
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
}
Loading