From bae7cec3fa97e5672884263676d0eb46ab50df8c Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 23 Feb 2018 11:25:25 +0100 Subject: [PATCH] Move all subcommands out of main into proper folders --- client/flags.go | 4 ++ client/keys/root.go | 5 +- client/lcd/root.go | 36 ++++++++++++ .../basecli/client.go => client/rpc/root.go | 55 +++---------------- client/tx/root.go | 45 +++++++++++++++ examples/basecoin/cmd/basecli/main.go | 20 ++++--- 6 files changed, 108 insertions(+), 57 deletions(-) create mode 100644 client/lcd/root.go rename examples/basecoin/cmd/basecli/client.go => client/rpc/root.go (54%) create mode 100644 client/tx/root.go diff --git a/client/flags.go b/client/flags.go index e89c49cf8..db35ad06a 100644 --- a/client/flags.go +++ b/client/flags.go @@ -10,6 +10,10 @@ const ( FlagName = "name" ) +// LineBreak can be included in a command list to provide a blank line +// to help with readability +var LineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}} + // GetCommands adds common flags to query commands func GetCommands(cmds ...*cobra.Command) []*cobra.Command { for _, c := range cmds { diff --git a/client/keys/root.go b/client/keys/root.go index 0dcf05040..d067d1515 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -15,11 +15,10 @@ package keys import ( + "github.com/cosmos/cosmos-sdk/client" "github.com/spf13/cobra" ) -var lineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}} - // Commands registers a sub-tree of commands to interact with // local private key storage. func Commands() *cobra.Command { @@ -36,7 +35,7 @@ func Commands() *cobra.Command { addKeyCommand(), listKeysCmd, showKeysCmd, - lineBreak, + client.LineBreak, deleteKeyCommand(), updateKeyCommand(), ) diff --git a/client/lcd/root.go b/client/lcd/root.go new file mode 100644 index 000000000..9187af9a1 --- /dev/null +++ b/client/lcd/root.go @@ -0,0 +1,36 @@ +package lcd + +import ( + "errors" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" +) + +const ( + flagBind = "bind" + flagCORS = "cors" +) + +// XXX: remove this when not needed +func todoNotImplemented(_ *cobra.Command, _ []string) error { + return errors.New("TODO: Command not yet implemented") +} + +// ServeCommand will generate a long-running rest server +// (aka Light Client Daemon) that exposes functionality similar +// to the cli, but over rest +func ServeCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "serve", + Short: "Start LCD (light-client daemon), a local REST server", + RunE: todoNotImplemented, + } + // TODO: handle unix sockets also? + cmd.Flags().StringP(flagBind, "b", "localhost:1317", "Interface and port that server binds to") + cmd.Flags().String(flagCORS, "", "Set to domains that can make CORS requests (* for all)") + cmd.Flags().StringP(client.FlagChainID, "c", "", "ID of chain we connect to") + cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") + return cmd +} diff --git a/examples/basecoin/cmd/basecli/client.go b/client/rpc/root.go similarity index 54% rename from examples/basecoin/cmd/basecli/client.go rename to client/rpc/root.go index 26ee87375..a65a9959e 100644 --- a/examples/basecoin/cmd/basecli/client.go +++ b/client/rpc/root.go @@ -1,11 +1,17 @@ -package main +package rpc import ( + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" ) +// XXX: remove this when not needed +func todoNotImplemented(_ *cobra.Command, _ []string) error { + return errors.New("TODO: Command not yet implemented") +} + const ( // one of the following should be provided to verify the connection flagGenesis = "genesis" @@ -13,11 +19,6 @@ const ( flagValHash = "validator-set" flagSelect = "select" - flagTags = "tag" - flagAny = "any" - - flagBind = "bind" - flagCORS = "cors" ) var ( @@ -28,19 +29,13 @@ var ( } ) -// AddClientCommands returns a sub-tree of all basic client commands -// -// Call AddGetCommand and AddPostCommand to add custom txs and queries -func AddClientCommands(cmd *cobra.Command) { +// AddCommands adds a number of rpc-related subcommands +func AddCommands(cmd *cobra.Command) { cmd.AddCommand( initClientCommand(), statusCmd, blockCommand(), validatorCommand(), - lineBreak, - txSearchCommand(), - txCommand(), - lineBreak, ) } @@ -76,35 +71,3 @@ func validatorCommand() *cobra.Command { } return cmd } - -func serveCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "serve", - Short: "Start LCD (light-client daemon), a local REST server", - RunE: todoNotImplemented, - } - // TODO: handle unix sockets also? - cmd.Flags().StringP(flagBind, "b", "localhost:1317", "Interface and port that server binds to") - cmd.Flags().String(flagCORS, "", "Set to domains that can make CORS requests (* for all)") - return cmd -} - -func txSearchCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "txs", - Short: "Search for all transactions that match the given tags", - RunE: todoNotImplemented, - } - cmd.Flags().StringSlice(flagTags, nil, "Tags that must match (may provide multiple)") - cmd.Flags().Bool(flagAny, false, "Return transactions that match ANY tag, rather than ALL") - return cmd -} - -func txCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "tx ", - Short: "Matches this txhash over all committed blocks", - RunE: todoNotImplemented, - } - return cmd -} diff --git a/client/tx/root.go b/client/tx/root.go new file mode 100644 index 000000000..1d2f33d9e --- /dev/null +++ b/client/tx/root.go @@ -0,0 +1,45 @@ +package tx + +import ( + "errors" + + "github.com/spf13/cobra" +) + +const ( + flagTags = "tag" + flagAny = "any" +) + +// XXX: remove this when not needed +func todoNotImplemented(_ *cobra.Command, _ []string) error { + return errors.New("TODO: Command not yet implemented") +} + +// AddCommands adds a number of tx-query related subcommands +func AddCommands(cmd *cobra.Command) { + cmd.AddCommand( + txSearchCommand(), + txCommand(), + ) +} + +func txSearchCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "txs", + Short: "Search for all transactions that match the given tags", + RunE: todoNotImplemented, + } + cmd.Flags().StringSlice(flagTags, nil, "Tags that must match (may provide multiple)") + cmd.Flags().Bool(flagAny, false, "Return transactions that match ANY tag, rather than ALL") + return cmd +} + +func txCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "tx ", + Short: "Matches this txhash over all committed blocks", + RunE: todoNotImplemented, + } + return cmd +} diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 8dbdad04d..6cabc2cef 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -10,6 +10,9 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/keys" + "github.com/cosmos/cosmos-sdk/client/lcd" + "github.com/cosmos/cosmos-sdk/client/rpc" + "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/version" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands" ) @@ -20,8 +23,6 @@ var ( Use: "basecli", Short: "Basecoin light-client", } - - lineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}} ) func todoNotImplemented(_ *cobra.Command, _ []string) error { @@ -32,10 +33,13 @@ func main() { // disable sorting cobra.EnableCommandSorting = false - // generic client commands - AddClientCommands(basecliCmd) + // add standard rpc, and tx commands + rpc.AddCommands(basecliCmd) + basecliCmd.AddCommand(client.LineBreak) + tx.AddCommands(basecliCmd) + basecliCmd.AddCommand(client.LineBreak) - // query/post commands (custom to binary) + // add query/post commands (custom to binary) basecliCmd.AddCommand( client.GetCommands( bankcmd.GetAccountCmd("main"), @@ -47,10 +51,10 @@ func main() { // add proxy, version and key info basecliCmd.AddCommand( - lineBreak, - serveCommand(), + client.LineBreak, + lcd.ServeCommand(), keys.Commands(), - lineBreak, + client.LineBreak, version.VersionCmd, )