diff --git a/cmd/basecoin/commands/counter.go b/cmd/basecoin/commands/counter.go index d05d08c424..83f98218cf 100644 --- a/cmd/basecoin/commands/counter.go +++ b/cmd/basecoin/commands/counter.go @@ -20,10 +20,17 @@ var ( ValidFlag, }, } + + CounterPluginFlag = cli.BoolFlag{ + Name: "counter-plugin", + Usage: "Enable the counter plugin", + } ) func init() { - RegisterPlugin(CounterTxCmd) + RegisterTxPlugin(CounterTxCmd) + RegisterStartPlugin(CounterPluginFlag, + func() types.Plugin { return counter.New("counter") }) } func cmdCounterTx(c *cli.Context) error { diff --git a/cmd/basecoin/commands/flags.go b/cmd/basecoin/commands/flags.go index 6bbf69e7bb..0ca74962ce 100644 --- a/cmd/basecoin/commands/flags.go +++ b/cmd/basecoin/commands/flags.go @@ -36,11 +36,6 @@ var ( Name: "ibc-plugin", Usage: "Enable the ibc plugin", } - - CounterPluginFlag = cli.BoolFlag{ - Name: "counter-plugin", - Usage: "Enable the counter plugin", - } ) // tx flags diff --git a/cmd/basecoin/commands/ibc.go b/cmd/basecoin/commands/ibc.go index bdb7ccc0e7..39f5aef8d4 100644 --- a/cmd/basecoin/commands/ibc.go +++ b/cmd/basecoin/commands/ibc.go @@ -136,11 +136,11 @@ func cmdIBCRegisterTx(c *cli.Context) error { } func cmdIBCUpdateTx(c *cli.Context) error { - headerBytes, err := hex.DecodeString(stripHex(c.String("header"))) + headerBytes, err := hex.DecodeString(StripHex(c.String("header"))) if err != nil { return errors.New(cmn.Fmt("Header (%v) is invalid hex: %v", c.String("header"), err)) } - commitBytes, err := hex.DecodeString(stripHex(c.String("commit"))) + commitBytes, err := hex.DecodeString(StripHex(c.String("commit"))) if err != nil { return errors.New(cmn.Fmt("Commit (%v) is invalid hex: %v", c.String("commit"), err)) } @@ -174,7 +174,7 @@ func cmdIBCPacketCreateTx(c *cli.Context) error { fromChain, toChain := c.String("from"), c.String("to") packetType := c.String("type") - payloadBytes, err := hex.DecodeString(stripHex(c.String("payload"))) + payloadBytes, err := hex.DecodeString(StripHex(c.String("payload"))) if err != nil { return errors.New(cmn.Fmt("Payload (%v) is invalid hex: %v", c.String("payload"), err)) } @@ -206,11 +206,11 @@ func cmdIBCPacketCreateTx(c *cli.Context) error { func cmdIBCPacketPostTx(c *cli.Context) error { fromChain, fromHeight := c.String("from"), c.Int("height") - packetBytes, err := hex.DecodeString(stripHex(c.String("packet"))) + packetBytes, err := hex.DecodeString(StripHex(c.String("packet"))) if err != nil { return errors.New(cmn.Fmt("Packet (%v) is invalid hex: %v", c.String("packet"), err)) } - proofBytes, err := hex.DecodeString(stripHex(c.String("proof"))) + proofBytes, err := hex.DecodeString(StripHex(c.String("proof"))) if err != nil { return errors.New(cmn.Fmt("Proof (%v) is invalid hex: %v", c.String("proof"), err)) } diff --git a/cmd/basecoin/commands/query.go b/cmd/basecoin/commands/query.go index 3c12e04107..4237075267 100644 --- a/cmd/basecoin/commands/query.go +++ b/cmd/basecoin/commands/query.go @@ -75,7 +75,7 @@ func cmdQuery(c *cli.Context) error { if isHex(keyString) { // convert key to bytes var err error - key, err = hex.DecodeString(stripHex(keyString)) + key, err = hex.DecodeString(StripHex(keyString)) if err != nil { return errors.New(cmn.Fmt("Query key (%v) is invalid hex: %v", keyString, err)) } @@ -107,7 +107,7 @@ func cmdAccount(c *cli.Context) error { if len(c.Args()) != 1 { return errors.New("account command requires an argument ([address])") } - addrHex := stripHex(c.Args()[0]) + addrHex := StripHex(c.Args()[0]) // convert destination address to bytes addr, err := hex.DecodeString(addrHex) @@ -175,7 +175,7 @@ func cmdVerify(c *cli.Context) error { var err error key := []byte(keyString) if isHex(keyString) { - key, err = hex.DecodeString(stripHex(keyString)) + key, err = hex.DecodeString(StripHex(keyString)) if err != nil { return errors.New(cmn.Fmt("Key (%v) is invalid hex: %v", keyString, err)) } @@ -183,18 +183,18 @@ func cmdVerify(c *cli.Context) error { value := []byte(valueString) if isHex(valueString) { - value, err = hex.DecodeString(stripHex(valueString)) + value, err = hex.DecodeString(StripHex(valueString)) if err != nil { return errors.New(cmn.Fmt("Value (%v) is invalid hex: %v", valueString, err)) } } - root, err := hex.DecodeString(stripHex(c.String("root"))) + root, err := hex.DecodeString(StripHex(c.String("root"))) if err != nil { return errors.New(cmn.Fmt("Root (%v) is invalid hex: %v", c.String("root"), err)) } - proofBytes, err := hex.DecodeString(stripHex(c.String("proof"))) + proofBytes, err := hex.DecodeString(StripHex(c.String("proof"))) if err != nil { return errors.New(cmn.Fmt("Proof (%v) is invalid hex: %v", c.String("proof"), err)) } diff --git a/cmd/basecoin/commands/start.go b/cmd/basecoin/commands/start.go index 52238f797e..90e701afd8 100644 --- a/cmd/basecoin/commands/start.go +++ b/cmd/basecoin/commands/start.go @@ -19,8 +19,8 @@ import ( tmtypes "github.com/tendermint/tendermint/types" "github.com/tendermint/basecoin/app" - "github.com/tendermint/basecoin/plugins/counter" "github.com/tendermint/basecoin/plugins/ibc" + "github.com/tendermint/basecoin/types" ) var config cfg.Config @@ -41,10 +41,23 @@ var StartCmd = cli.Command{ InProcTMFlag, ChainIDFlag, IbcPluginFlag, - CounterPluginFlag, + // CounterPluginFlag, }, } +type plugin struct { + name string + init func() types.Plugin +} + +var plugins = []plugin{} + +// RegisterStartPlugin is used to add another +func RegisterStartPlugin(flag cli.BoolFlag, init func() types.Plugin) { + StartCmd.Flags = append(StartCmd.Flags, flag) + plugins = append(plugins, plugin{name: flag.GetName(), init: init}) +} + func cmdStart(c *cli.Context) error { // Connect to MerkleEyes @@ -61,14 +74,15 @@ func cmdStart(c *cli.Context) error { // Create Basecoin app basecoinApp := app.NewBasecoin(eyesCli) - - if c.Bool("counter-plugin") { - basecoinApp.RegisterPlugin(counter.New("counter")) - } - if c.Bool("ibc-plugin") { basecoinApp.RegisterPlugin(ibc.New()) + } + // loop through all registered plugins and enable if desired + for _, p := range plugins { + if c.Bool(p.name) { + basecoinApp.RegisterPlugin(p.init()) + } } // If genesis file exists, set key-value options diff --git a/cmd/basecoin/commands/tx.go b/cmd/basecoin/commands/tx.go index 745c450323..43d36c81d7 100644 --- a/cmd/basecoin/commands/tx.go +++ b/cmd/basecoin/commands/tx.go @@ -67,9 +67,9 @@ var ( } ) -// RegisterPlugin is used to add another subcommand and create a custom +// RegisterTxPlugin is used to add another subcommand and create a custom // apptx encoding. Look at counter.go for an example -func RegisterPlugin(cmd cli.Command) { +func RegisterTxPlugin(cmd cli.Command) { AppTxCmd.Subcommands = append(AppTxCmd.Subcommands, cmd) } @@ -82,7 +82,7 @@ func cmdSendTx(c *cli.Context) error { chainID := c.String("chain_id") // convert destination address to bytes - to, err := hex.DecodeString(stripHex(toHex)) + to, err := hex.DecodeString(StripHex(toHex)) if err != nil { return errors.New("To address is invalid hex: " + err.Error()) } diff --git a/cmd/basecoin/commands/utils.go b/cmd/basecoin/commands/utils.go index db9f2821c4..04ac83ae26 100644 --- a/cmd/basecoin/commands/utils.go +++ b/cmd/basecoin/commands/utils.go @@ -28,7 +28,7 @@ func isHex(s string) bool { return false } -func stripHex(s string) string { +func StripHex(s string) string { if isHex(s) { return s[2:] }