cmd start allows plugin registration

This commit is contained in:
Ethan Frey 2017-01-30 17:16:00 +01:00
parent 53786ab4ce
commit 15904ea7a7
7 changed files with 44 additions and 28 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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

View File

@ -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())
}

View File

@ -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:]
}