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

expose cobra command vars + commands #16

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions cmd/check_quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ var CheckQuorumCmd = &cobra.Command{
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
// Load proposal
proposal, err := LoadProposal(proposalType, proposalPath)
proposal, err := LoadProposal(ProposalType, ProposalPath)
if err != nil {
return err
}

// Dial the RPC
clientBackend, err := ethclient.Dial(rpc)
clientBackend, err := ethclient.Dial(Rpc)
if err != nil {
return err
}
Expand All @@ -34,7 +34,7 @@ var CheckQuorumCmd = &cobra.Command{
}

// Check quorum
quorumMet, err := e.CheckQuorum(clientBackend, mcms.ChainIdentifier(chainSelector))
quorumMet, err := e.CheckQuorum(clientBackend, mcms.ChainIdentifier(ChainSelector))
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/check_quorum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

func Test_CheckQuorumCommand(t *testing.T) {
actual := new(bytes.Buffer)
rootCmd.SetOut(actual)
rootCmd.SetErr(actual)
rootCmd.SetArgs([]string{"check-quorum", "--rpc", "http://localhost:8545", "--proposal", "./proposal.json", "--selector", "1"})
rootCmd.Execute()
RootCmd.SetOut(actual)
RootCmd.SetErr(actual)
RootCmd.SetArgs([]string{"check-quorum", "--rpc", "http://localhost:8545", "--proposal", "./proposal.json", "--selector", "1"})
RootCmd.Execute()

assert.Equal(t, "http://localhost:8545", rpc)
assert.Equal(t, "./proposal.json", proposalPath)
assert.Equal(t, uint64(1), chainSelector)
assert.Equal(t, "http://localhost:8545", Rpc)
assert.Equal(t, "./proposal.json", ProposalPath)
assert.Equal(t, uint64(1), ChainSelector)

expectedDescription := "help for check-quorum"
assert.Containsf(t, actual.String(), expectedDescription, "expected description to contain '%s'", expectedDescription)
Expand Down
8 changes: 4 additions & 4 deletions cmd/execute_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ var ExecuteChainCmd = &cobra.Command{
}

// Load proposal
proposal, err := LoadProposal(proposalType, proposalPath)
proposal, err := LoadProposal(ProposalType, ProposalPath)
if err != nil {
return err
}

// Dial the RPC
clientBackend, err := ethclient.Dial(rpc)
clientBackend, err := ethclient.Dial(Rpc)
if err != nil {
return err
}
Expand All @@ -44,7 +44,7 @@ var ExecuteChainCmd = &cobra.Command{
}

// Get EVM chain ID
chain, exists := chain_selectors.ChainBySelector(chainSelector)
chain, exists := chain_selectors.ChainBySelector(ChainSelector)
if !exists {
return errors.New("chain not found")
}
Expand All @@ -56,7 +56,7 @@ var ExecuteChainCmd = &cobra.Command{
}

for i, op := range e.Proposal.Transactions {
if op.ChainIdentifier == mcms.ChainIdentifier(chainSelector) {
if op.ChainIdentifier == mcms.ChainIdentifier(ChainSelector) {
transaction, err := e.ExecuteOnChain(clientBackend, auth, i)
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions cmd/execute_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

func Test_ExecuteChainCommand(t *testing.T) {
actual := new(bytes.Buffer)
rootCmd.SetOut(actual)
rootCmd.SetErr(actual)
rootCmd.SetArgs([]string{"execute-chain", "--rpc", "http://localhost:8545", "--proposal", "./proposal.json", "--selector", "1"})
rootCmd.Execute()
RootCmd.SetOut(actual)
RootCmd.SetErr(actual)
RootCmd.SetArgs([]string{"execute-chain", "--rpc", "http://localhost:8545", "--proposal", "./proposal.json", "--selector", "1"})
RootCmd.Execute()

assert.Equal(t, "http://localhost:8545", rpc)
assert.Equal(t, "./proposal.json", proposalPath)
assert.Equal(t, uint64(1), chainSelector)
assert.Equal(t, "http://localhost:8545", Rpc)
assert.Equal(t, "./proposal.json", ProposalPath)
assert.Equal(t, uint64(1), ChainSelector)

expectedDescription := "help for execute"
assert.Containsf(t, actual.String(), expectedDescription, "expected description to contain '%s'", expectedDescription)
Expand Down
8 changes: 4 additions & 4 deletions cmd/execute_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ var ExecuteOperationCmd = &cobra.Command{
}

// Load proposal
proposal, err := LoadProposal(proposalType, proposalPath)
proposal, err := LoadProposal(ProposalType, ProposalPath)
if err != nil {
return err
}

// Dial the RPC
clientBackend, err := ethclient.Dial(rpc)
clientBackend, err := ethclient.Dial(Rpc)
if err != nil {
return err
}
Expand All @@ -46,7 +46,7 @@ var ExecuteOperationCmd = &cobra.Command{
}

// Get EVM chain ID
chain, exists := chain_selectors.ChainBySelector(chainSelector)
chain, exists := chain_selectors.ChainBySelector(ChainSelector)
if !exists {
return errors.New("chain not found")
}
Expand All @@ -58,7 +58,7 @@ var ExecuteOperationCmd = &cobra.Command{
}

for i, op := range e.Proposal.Transactions {
if op.ChainIdentifier == mcms.ChainIdentifier(chainSelector) {
if op.ChainIdentifier == mcms.ChainIdentifier(ChainSelector) {
transaction, err := e.ExecuteOnChain(clientBackend, auth, i)
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions cmd/execute_operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

func Test_ExecuteCommand(t *testing.T) {
actual := new(bytes.Buffer)
rootCmd.SetOut(actual)
rootCmd.SetErr(actual)
rootCmd.SetArgs([]string{"execute-operation", "--rpc", "http://localhost:8545", "--proposal", "./proposal.json", "--selector", "1", "--index", "3"})
rootCmd.Execute()
RootCmd.SetOut(actual)
RootCmd.SetErr(actual)
RootCmd.SetArgs([]string{"execute-operation", "--rpc", "http://localhost:8545", "--proposal", "./proposal.json", "--selector", "1", "--index", "3"})
RootCmd.Execute()

assert.Equal(t, "http://localhost:8545", rpc)
assert.Equal(t, "./proposal.json", proposalPath)
assert.Equal(t, uint64(1), chainSelector)
assert.Equal(t, "http://localhost:8545", Rpc)
assert.Equal(t, "./proposal.json", ProposalPath)
assert.Equal(t, uint64(1), ChainSelector)
assert.Equal(t, uint64(3), index)

expectedDescription := "help for execute"
Expand Down
34 changes: 18 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,38 @@ import (
"github.com/spf13/cobra"
)

var rpc string
var proposalPath string
var proposalType proposal.ProposalType
var chainSelector uint64
var (
Rpc string
ProposalPath string
ProposalType proposal.ProposalType
ChainSelector uint64
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "mcms",
Short: "Tools for on-chain interactions with the MCMS ",
Long: ``,
}

func Execute() {
err := rootCmd.Execute()
err := RootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
rootCmd.AddCommand(CheckQuorumCmd)
rootCmd.AddCommand(ExecuteOperationCmd)
rootCmd.AddCommand(SetMerkleCmd)
rootCmd.AddCommand(ExecuteChainCmd)
RootCmd.AddCommand(CheckQuorumCmd)
RootCmd.AddCommand(ExecuteOperationCmd)
RootCmd.AddCommand(SetMerkleCmd)
RootCmd.AddCommand(ExecuteChainCmd)

rootCmd.PersistentFlags().StringVar(&rpc, "rpc", "", "rpc to be used in the proposal")
rootCmd.PersistentFlags().StringVar(&proposalPath, "proposal", "p", "Absolute file path containing the proposal to be submitted")
rootCmd.PersistentFlags().Uint64Var(&chainSelector, "selector", 0, "Chain selector for the command to connect to")
RootCmd.PersistentFlags().StringVar(&Rpc, "rpc", "", "rpc to be used in the proposal")
RootCmd.PersistentFlags().StringVar(&ProposalPath, "proposal", "p", "Absolute file path containing the proposal to be submitted")
RootCmd.PersistentFlags().Uint64Var(&ChainSelector, "selector", 0, "Chain selector for the command to connect to")

var proposalTypeStr string
rootCmd.PersistentFlags().StringVar(&proposalTypeStr, "proposalType", string(proposal.MCMS), "The type of proposal being ingested")
proposalType = proposal.StringToProposalType[proposalTypeStr]
RootCmd.PersistentFlags().StringVar(&proposalTypeStr, "proposalType", string(proposal.MCMS), "The type of proposal being ingested")
ProposalType = proposal.StringToProposalType[proposalTypeStr]
}
8 changes: 4 additions & 4 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
func Test_cliRootCommand(t *testing.T) {

actual := new(bytes.Buffer)
rootCmd.SetOut(actual)
rootCmd.SetErr(actual)
rootCmd.SetArgs([]string{})
rootCmd.Execute()
RootCmd.SetOut(actual)
RootCmd.SetErr(actual)
RootCmd.SetArgs([]string{})
RootCmd.Execute()

expectedDescription := "Tools for on-chain interactions with the MCMS"
assert.Containsf(t, actual.String(), expectedDescription, "expected description to contain '%s'", expectedDescription)
Expand Down
8 changes: 4 additions & 4 deletions cmd/set_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ var SetMerkleCmd = &cobra.Command{
}

// Load proposal
proposal, err := LoadProposal(proposalType, proposalPath)
proposal, err := LoadProposal(ProposalType, ProposalPath)
if err != nil {
return err
}

// Dial the RPC
clientBackend, err := ethclient.Dial(rpc)
clientBackend, err := ethclient.Dial(Rpc)
if err != nil {
return err
}
Expand All @@ -45,7 +45,7 @@ var SetMerkleCmd = &cobra.Command{
}

// Get EVM chain ID
chain, exists := chain_selectors.ChainBySelector(chainSelector)
chain, exists := chain_selectors.ChainBySelector(ChainSelector)
if !exists {
return errors.New("chain not found")
}
Expand All @@ -57,7 +57,7 @@ var SetMerkleCmd = &cobra.Command{
}

// Set the root on chain
transaction, err := e.SetRootOnChain(clientBackend, auth, mcms.ChainIdentifier(chainSelector))
transaction, err := e.SetRootOnChain(clientBackend, auth, mcms.ChainIdentifier(ChainSelector))
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/set_root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

func Test_SetRootCommand(t *testing.T) {
actual := new(bytes.Buffer)
rootCmd.SetOut(actual)
rootCmd.SetErr(actual)
rootCmd.SetArgs([]string{"set-root", "--rpc", "http://localhost:8545", "--proposal", "./proposal.json", "--selector", "1"})
rootCmd.Execute()
RootCmd.SetOut(actual)
RootCmd.SetErr(actual)
RootCmd.SetArgs([]string{"set-root", "--rpc", "http://localhost:8545", "--proposal", "./proposal.json", "--selector", "1"})
RootCmd.Execute()

assert.Equal(t, "http://localhost:8545", rpc)
assert.Equal(t, "./proposal.json", proposalPath)
assert.Equal(t, uint64(1), chainSelector)
assert.Equal(t, "http://localhost:8545", Rpc)
assert.Equal(t, "./proposal.json", ProposalPath)
assert.Equal(t, uint64(1), ChainSelector)

expectedDescription := "no such file or directory"
assert.Containsf(t, actual.String(), expectedDescription, "expected description to contain '%s'", expectedDescription)
Expand Down
4 changes: 2 additions & 2 deletions cmd/sign_private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var SignPrivateKeyCmd = &cobra.Command{
}

// Load proposal
proposal, err := LoadProposal(proposalType, proposalPath)
proposal, err := LoadProposal(ProposalType, ProposalPath)
if err != nil {
return err
}
Expand All @@ -28,7 +28,7 @@ var SignPrivateKeyCmd = &cobra.Command{
}

// Write proposal to file
err = WriteProposalToFile(proposal, proposalPath)
err = WriteProposalToFile(proposal, ProposalPath)
if err != nil {
return err
}
Expand Down
Loading