From e82949d57dc1e26e59d7ecba99983f4a4f91ce65 Mon Sep 17 00:00:00 2001 From: "Bastien R." Date: Sat, 28 Sep 2024 21:23:11 +0200 Subject: [PATCH] feat(tools/cosmovisor): Add ShowUpgradeInfoCmd (#21932) Co-authored-by: Bastien Rigaud --- tools/cosmovisor/CHANGELOG.md | 4 ++ tools/cosmovisor/cmd/cosmovisor/root.go | 1 + .../cosmovisor/cmd/cosmovisor/show_upgrade.go | 49 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 tools/cosmovisor/cmd/cosmovisor/show_upgrade.go diff --git a/tools/cosmovisor/CHANGELOG.md b/tools/cosmovisor/CHANGELOG.md index a4c78e469c45..ae0de6b86ffe 100644 --- a/tools/cosmovisor/CHANGELOG.md +++ b/tools/cosmovisor/CHANGELOG.md @@ -40,6 +40,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#21462](https://github.com/cosmos/cosmos-sdk/pull/21462) Pass `stdin` to binary. +### Features + +* [#21932](https://github.com/cosmos/cosmos-sdk/pull/21932) Add `cosmovisor show-upgrade-info` command to display the upgrade-info.json into stdout. + ## v1.6.0 - 2024-08-12 ## Improvements diff --git a/tools/cosmovisor/cmd/cosmovisor/root.go b/tools/cosmovisor/cmd/cosmovisor/root.go index d9f6094d593c..d8c351f4c143 100644 --- a/tools/cosmovisor/cmd/cosmovisor/root.go +++ b/tools/cosmovisor/cmd/cosmovisor/root.go @@ -19,6 +19,7 @@ func NewRootCmd() *cobra.Command { configCmd, NewVersionCmd(), NewAddUpgradeCmd(), + NewShowUpgradeInfoCmd(), ) rootCmd.PersistentFlags().StringP(cosmovisor.FlagCosmovisorConfig, "c", "", "path to cosmovisor config file") diff --git a/tools/cosmovisor/cmd/cosmovisor/show_upgrade.go b/tools/cosmovisor/cmd/cosmovisor/show_upgrade.go new file mode 100644 index 000000000000..07da3e08894d --- /dev/null +++ b/tools/cosmovisor/cmd/cosmovisor/show_upgrade.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" + + "cosmossdk.io/tools/cosmovisor" +) + +func NewShowUpgradeInfoCmd() *cobra.Command { + showUpgradeInfo := &cobra.Command{ + Use: "show-upgrade-info", + Short: "Show upgrade-info.json into stdout.", + SilenceUsage: false, + Args: cobra.NoArgs, + RunE: showUpgradeInfoCmd, + } + + 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 +}