-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp(cli): convert to using Cobra CLI package (#28)
* convert to using Cobra CLI package * add changelog entry
- Loading branch information
1 parent
220ae8d
commit 6f5699a
Showing
12 changed files
with
247 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/MalteHerrmann/upgrade-local-node-go/gov" | ||
"github.com/MalteHerrmann/upgrade-local-node-go/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
//nolint:gochecknoglobals // required by cobra | ||
var depositCmd = &cobra.Command{ | ||
Use: "deposit", | ||
Short: "Deposit for a governance proposal", | ||
Long: `Deposit the minimum needed deposit for a given governance proposal. | ||
If no proposal ID is given by the user, the latest proposal is queried and deposited for.`, | ||
Args: cobra.RangeArgs(0, 1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
bin, err := utils.NewEvmosTestingBinary() | ||
if err != nil { | ||
log.Fatalf("error creating binary: %v", err) | ||
} | ||
|
||
if err = bin.GetAccounts(); err != nil { | ||
log.Fatalf("error getting accounts: %v", err) | ||
} | ||
|
||
err = gov.Deposit(bin, args) | ||
if err != nil { | ||
log.Fatalf("error depositing: %v", err) | ||
} | ||
}, | ||
} | ||
|
||
//nolint:gochecknoinits // required by cobra | ||
func init() { | ||
rootCmd.AddCommand(depositCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands. | ||
// | ||
//nolint:gochecknoglobals // required by cobra | ||
var rootCmd = &cobra.Command{ | ||
Use: "evmos-utils", | ||
Short: "A collection of utilities to interact with an Evmos node during development.", | ||
Long: `The evmos-utils collection offers helpers to interact with an Evmos node during development. | ||
It can be used to test upgrades, deposit or vote for specific or the latest proposals, etc.`, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"regexp" | ||
|
||
"github.com/MalteHerrmann/upgrade-local-node-go/gov" | ||
"github.com/MalteHerrmann/upgrade-local-node-go/utils" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
//nolint:gochecknoglobals // required by cobra | ||
var upgradeCmd = &cobra.Command{ | ||
Use: "upgrade", | ||
Short: "Prepare an upgrade of a node", | ||
Long: `Prepare an upgrade of a node by submitting a governance proposal, | ||
voting for it using all keys of in the keyring and having it pass.`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
bin, err := utils.NewEvmosTestingBinary() | ||
if err != nil { | ||
log.Fatalf("error creating binary: %v", err) | ||
} | ||
|
||
if err = bin.GetAccounts(); err != nil { | ||
log.Fatalf("error getting accounts: %v", err) | ||
} | ||
|
||
targetVersion := args[0] | ||
if matched, _ := regexp.MatchString(`v\d+\.\d+\.\d(-rc\d+)?`, targetVersion); !matched { | ||
log.Fatalf("invalid target version: %s; please use the format vX.Y.Z(-rc*).\n", targetVersion) | ||
} | ||
|
||
if err := upgradeLocalNode(bin, targetVersion); err != nil { | ||
log.Fatalf("error upgrading local node: %v", err) | ||
} | ||
}, | ||
} | ||
|
||
//nolint:gochecknoinits // required by cobra | ||
func init() { | ||
rootCmd.AddCommand(upgradeCmd) | ||
} | ||
|
||
// upgradeLocalNode prepares upgrading the local node to the target version | ||
// by submitting the upgrade proposal and voting on it using all testing accounts. | ||
func upgradeLocalNode(bin *utils.Binary, targetVersion string) error { | ||
currentHeight, err := utils.GetCurrentHeight(bin) | ||
if err != nil { | ||
return errors.Wrap(err, "error getting current height") | ||
} | ||
|
||
upgradeHeight := currentHeight + utils.DeltaHeight | ||
|
||
log.Println("Submitting upgrade proposal...") | ||
|
||
proposalID, err := gov.SubmitUpgradeProposal(bin, targetVersion, upgradeHeight) | ||
if err != nil { | ||
return errors.Wrap(err, "error executing upgrade proposal") | ||
} | ||
|
||
log.Printf("Scheduled upgrade to %s at height %d.\n", targetVersion, upgradeHeight) | ||
|
||
if err = gov.SubmitAllVotesForProposal(bin, proposalID); err != nil { | ||
return errors.Wrapf(err, "error submitting votes for proposal %d", proposalID) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/MalteHerrmann/upgrade-local-node-go/gov" | ||
"github.com/MalteHerrmann/upgrade-local-node-go/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
//nolint:gochecknoglobals // required by cobra | ||
var voteCmd = &cobra.Command{ | ||
Use: "vote", | ||
Short: "Vote for a governance proposal", | ||
Long: `Vote for a governance proposal with all keys in the keyring. | ||
If no proposal ID is passed, the latest proposal on chain is queried and used.`, | ||
Args: cobra.RangeArgs(0, 1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
bin, err := utils.NewEvmosTestingBinary() | ||
if err != nil { | ||
log.Fatalf("error creating binary: %v", err) | ||
} | ||
|
||
if err = bin.GetAccounts(); err != nil { | ||
log.Fatalf("error getting accounts: %v", err) | ||
} | ||
|
||
if err = gov.SubmitAllVotes(bin, args); err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
//nolint:gochecknoinits // required by cobra | ||
func init() { | ||
rootCmd.AddCommand(voteCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gov | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/MalteHerrmann/upgrade-local-node-go/utils" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// GetProposalIDFromInput gets the proposal ID from the command line arguments. | ||
func GetProposalIDFromInput(bin *utils.Binary, args []string) (int, error) { | ||
var ( | ||
err error | ||
proposalID int | ||
) | ||
|
||
switch len(args) { | ||
case 0: | ||
proposalID, err = QueryLatestProposalID(bin) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "error querying latest proposal ID") | ||
} | ||
case 1: | ||
proposalID, err = strconv.Atoi(args[2]) | ||
if err != nil { | ||
return 0, errors.Wrapf(err, "error converting proposal ID %s to integer", args[2]) | ||
} | ||
default: | ||
return 0, fmt.Errorf("invalid number of arguments; expected 0 or 1; got %d", len(args)) | ||
} | ||
|
||
return proposalID, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.