From 66e6677281280757175c4bca6802e083d96a747b Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 22 Feb 2018 15:33:34 +0100 Subject: [PATCH] Copy over gaiacli skeleton to basecli --- Makefile | 3 +- examples/basecoin/cmd/basecli/client.go | 131 ++++++++++++++++++++++ examples/basecoin/cmd/basecli/commands.go | 23 ++++ examples/basecoin/cmd/basecli/key.go | 77 +++++++++++++ examples/basecoin/cmd/basecli/main.go | 59 ++++++++++ 5 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 examples/basecoin/cmd/basecli/client.go create mode 100644 examples/basecoin/cmd/basecli/commands.go create mode 100644 examples/basecoin/cmd/basecli/key.go create mode 100644 examples/basecoin/cmd/basecli/main.go diff --git a/Makefile b/Makefile index 6c86730d0b..395af306ac 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,8 @@ gaia: build: @rm -rf examples/basecoin/vendor/ - go build $(BUILD_FLAGS) -o build/basecoind ./examples/basecoin/cmd/basecoind/... + go build $(BUILD_FLAGS) -o build/basecoind ./examples/basecoin/cmd/basecoind + go build $(BUILD_FLAGS) -o build/basecli ./examples/basecoin/cmd/basecli dist: @bash publish/dist.sh diff --git a/examples/basecoin/cmd/basecli/client.go b/examples/basecoin/cmd/basecli/client.go new file mode 100644 index 0000000000..682a571e6e --- /dev/null +++ b/examples/basecoin/cmd/basecli/client.go @@ -0,0 +1,131 @@ +package main + +import "github.com/spf13/cobra" + +const ( + // these are needed for every init + flagChainID = "chain-id" + flagNode = "node" + + // one of the following should be provided to verify the connection + flagGenesis = "genesis" + flagCommit = "commit" + flagValHash = "validator-set" + + flagSelect = "select" + flagTags = "tag" + flagAny = "any" + + flagBind = "bind" + flagCORS = "cors" + flagTrustNode = "trust-node" + + // this is for signing + flagName = "name" +) + +var ( + statusCmd = &cobra.Command{ + Use: "status", + Short: "Query remote node for status", + RunE: todoNotImplemented, + } +) + +// 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) { + cmd.AddCommand( + initClientCommand(), + statusCmd, + blockCommand(), + validatorCommand(), + lineBreak, + txSearchCommand(), + txCommand(), + lineBreak, + ) +} + +// GetCommands adds common flags to query commands +func GetCommands(cmds ...*cobra.Command) []*cobra.Command { + for _, c := range cmds { + c.Flags().Bool(flagTrustNode, false, "Don't verify proofs for responses") + } + return cmds +} + +// PostCommands adds common flags for commands to post tx +func PostCommands(cmds ...*cobra.Command) []*cobra.Command { + for _, c := range cmds { + c.Flags().String(flagName, "", "Name of private key with which to sign") + c.Flags().String(flagPassword, "", "Password to use the named private key") + } + return cmds +} + +func initClientCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "init", + Short: "Initialize light client", + RunE: todoNotImplemented, + } + cmd.Flags().StringP(flagChainID, "c", "", "ID of chain we connect to") + cmd.Flags().StringP(flagNode, "n", "tcp://localhost:46657", "Node to connect to") + cmd.Flags().String(flagGenesis, "", "Genesis file to verify header validity") + cmd.Flags().String(flagCommit, "", "File with trusted and signed header") + cmd.Flags().String(flagValHash, "", "Hash of trusted validator set (hex-encoded)") + return cmd +} + +func blockCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "block ", + Short: "Get verified data for a the block at given height", + RunE: todoNotImplemented, + } + cmd.Flags().StringSlice(flagSelect, []string{"header", "tx"}, "Fields to return (header|txs|results)") + return cmd +} + +func validatorCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "validatorset ", + Short: "Get the full validator set at given height", + RunE: todoNotImplemented, + } + 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/examples/basecoin/cmd/basecli/commands.go b/examples/basecoin/cmd/basecli/commands.go new file mode 100644 index 0000000000..2af60010d2 --- /dev/null +++ b/examples/basecoin/cmd/basecli/commands.go @@ -0,0 +1,23 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +const ( + flagTo = "to" + flagAmount = "amount" + flagFee = "fee" +) + +func postSendCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "send", + Short: "Create and sign a send tx", + RunE: todoNotImplemented, + } + cmd.Flags().String(flagTo, "", "Address to send coins") + cmd.Flags().String(flagAmount, "", "Amount of coins to send") + cmd.Flags().String(flagFee, "", "Fee to pay along with transaction") + return cmd +} diff --git a/examples/basecoin/cmd/basecli/key.go b/examples/basecoin/cmd/basecli/key.go new file mode 100644 index 0000000000..b3ffa323ae --- /dev/null +++ b/examples/basecoin/cmd/basecli/key.go @@ -0,0 +1,77 @@ +package main + +import "github.com/spf13/cobra" + +const ( + flagPassword = "password" + flagNewPassword = "new-password" + flagType = "type" + flagSeed = "seed" + flagDryRun = "dry-run" +) + +var ( + listKeysCmd = &cobra.Command{ + Use: "list", + Short: "List all locally availably keys", + RunE: todoNotImplemented, + } + + showKeysCmd = &cobra.Command{ + Use: "show ", + Short: "Show key info for the given name", + RunE: todoNotImplemented, + } +) + +// KeyCommands registers a sub-tree of commands to interact with +// local private key storage. +func KeyCommands() *cobra.Command { + cmd := &cobra.Command{ + Use: "keys", + Short: "Add or view local private keys", + } + cmd.AddCommand( + addKeyCommand(), + listKeysCmd, + showKeysCmd, + lineBreak, + deleteKeyCommand(), + updateKeyCommand(), + ) + return cmd +} + +func addKeyCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "add ", + Short: "Create a new key, or import from seed", + RunE: todoNotImplemented, + } + cmd.Flags().StringP(flagPassword, "p", "", "Password to encrypt private key") + cmd.Flags().StringP(flagType, "t", "ed25519", "Type of private key (ed25519|secp256k1|ledger)") + cmd.Flags().StringP(flagSeed, "s", "", "Provide seed phrase to recover existing key instead of creating") + cmd.Flags().Bool(flagDryRun, false, "Perform action, but don't add key to local keystore") + return cmd +} + +func updateKeyCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "update ", + Short: "Change the password used to protect private key", + RunE: todoNotImplemented, + } + cmd.Flags().StringP(flagPassword, "p", "", "Current password to decrypt key") + cmd.Flags().String(flagNewPassword, "", "New password to use to protect key") + return cmd +} + +func deleteKeyCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete ", + Short: "Delete the given key", + RunE: todoNotImplemented, + } + cmd.Flags().StringP(flagPassword, "p", "", "Password of existing key to delete") + return cmd +} diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go new file mode 100644 index 0000000000..1989d1b38d --- /dev/null +++ b/examples/basecoin/cmd/basecli/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "errors" + "os" + + "github.com/spf13/cobra" + + "github.com/tendermint/tmlibs/cli" + + "github.com/cosmos/cosmos-sdk/version" +) + +// gaiacliCmd is the entry point for this binary +var ( + basecliCmd = &cobra.Command{ + Use: "basecli", + Short: "Basecoin light-client", + } + + lineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}} + + getAccountCmd = &cobra.Command{ + Use: "account
", + Short: "Query account balance", + RunE: todoNotImplemented, + } +) + +func todoNotImplemented(_ *cobra.Command, _ []string) error { + return errors.New("TODO: Command not yet implemented") +} + +func main() { + // disable sorting + cobra.EnableCommandSorting = false + + // generic client commands + AddClientCommands(basecliCmd) + // query commands (custom to binary) + basecliCmd.AddCommand( + GetCommands(getAccountCmd)...) + // post tx commands (custom to binary) + basecliCmd.AddCommand( + PostCommands(postSendCommand())...) + + // add proxy, version and key info + basecliCmd.AddCommand( + lineBreak, + serveCommand(), + KeyCommands(), + lineBreak, + version.VersionCmd, + ) + + // prepare and add flags + executor := cli.PrepareBaseCmd(basecliCmd, "GA", os.ExpandEnv("$HOME/.basecli")) + executor.Execute() +}