basecli refactor

This commit is contained in:
rigelrozanski
2018-03-01 02:36:57 +00:00
parent 94948746a0
commit 65f27f2daa
12 changed files with 123 additions and 80 deletions
+1
View File
@@ -2,6 +2,7 @@ package client
import "github.com/spf13/cobra"
// nolint
const (
FlagChainID = "chain-id"
FlagNode = "node"
+71
View File
@@ -0,0 +1,71 @@
package client
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/viper"
rpcclient "github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
cmn "github.com/tendermint/tmlibs/common"
)
// GetNode prepares a simple rpc.Client from the flags
func GetNode() (rpcclient.Client, error) {
uri := viper.GetString(FlagNode)
if uri == "" {
return nil, errors.New("Must define node using --node")
}
return rpcclient.NewHTTP(uri, "/websocket"), nil
}
// Broadcast the transaction bytes to Tendermint
func BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
node, err := GetNode()
if err != nil {
return nil, err
}
res, err := node.BroadcastTxCommit(tx)
if err != nil {
return res, err
}
if res.CheckTx.Code != uint32(0) {
return res, errors.Errorf("CheckTx failed: (%d) %s",
res.CheckTx.Code,
res.CheckTx.Log)
}
if res.DeliverTx.Code != uint32(0) {
return res, errors.Errorf("DeliverTx failed: (%d) %s",
res.DeliverTx.Code,
res.DeliverTx.Log)
}
return res, err
}
// Query from Tendermint with the provided key and storename
func Query(key cmn.HexBytes, storeName string) (res []byte, err error) {
path := fmt.Sprintf("/%s/key", storeName)
node, err := GetNode()
if err != nil {
return res, err
}
opts := rpcclient.ABCIQueryOptions{
Height: viper.GetInt64(FlagHeight),
Trusted: viper.GetBool(FlagTrustNode),
}
result, err := node.ABCIQueryWithOptions(path, key, opts)
if err != nil {
return res, err
}
resp := result.Response
if resp.Code != uint32(0) {
return res, errors.Errorf("Query failed: (%d) %s", resp.Code, resp.Log)
}
return resp.Value, nil
}
+2 -1
View File
@@ -102,7 +102,8 @@ func printCreate(info keys.Info, seed string) {
// print seed unless requested not to.
if !viper.GetBool(flagNoBackup) {
fmt.Println("**Important** write this seed phrase in a safe place.")
fmt.Println("It is the only way to recover your account if you ever forget your password.\n")
fmt.Println("It is the only way to recover your account if you ever forget your password.")
fmt.Println()
fmt.Println(seed)
}
case "json":
-8
View File
@@ -1,8 +0,0 @@
package client
import rpcclient "github.com/tendermint/tendermint/rpc/client"
// GetNode prepares a simple rpc.Client from the flags
func GetNode(uri string) rpcclient.Client {
return rpcclient.NewHTTP(uri, "/websocket")
}
+4 -3
View File
@@ -5,7 +5,6 @@ import (
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
tmwire "github.com/tendermint/tendermint/wire"
@@ -43,8 +42,10 @@ func getBlock(cmd *cobra.Command, args []string) error {
}
// get the node
uri := viper.GetString(client.FlagNode)
node := client.GetNode(uri)
node, err := client.GetNode()
if err != nil {
return err
}
// TODO: actually honor the --select flag!
// header -> BlockchainInfo
+4 -3
View File
@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
tmwire "github.com/tendermint/tendermint/wire"
@@ -22,8 +21,10 @@ func statusCommand() *cobra.Command {
func checkStatus(cmd *cobra.Command, args []string) error {
// get the node
uri := viper.GetString(client.FlagNode)
node := client.GetNode(uri)
node, err := client.GetNode()
if err != nil {
return err
}
res, err := node.Status()
if err != nil {
return err
+4 -3
View File
@@ -5,7 +5,6 @@ import (
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
tmwire "github.com/tendermint/tendermint/wire"
@@ -38,8 +37,10 @@ func getValidators(cmd *cobra.Command, args []string) error {
}
// get the node
uri := viper.GetString(client.FlagNode)
node := client.GetNode(uri)
node, err := client.GetNode()
if err != nil {
return err
}
res, err := node.Validators(height)
if err != nil {
+3 -4
View File
@@ -41,11 +41,10 @@ func searchTx(cmd *cobra.Command, args []string) error {
query := strings.Join(tags, " AND ")
// get the node
uri := viper.GetString(client.FlagNode)
if uri == "" {
return errors.New("Must define which node to query with --node")
node, err := client.GetNode()
if err != nil {
return err
}
node := client.GetNode(uri)
prove := !viper.GetBool(client.FlagTrustNode)
res, err := node.TxSearch(query, prove)
+4 -4
View File
@@ -28,6 +28,7 @@ func txCommand() *cobra.Command {
return cmd
}
// command to query for a transaction
func queryTx(cmd *cobra.Command, args []string) error {
if len(args) != 1 || len(args[0]) == 0 {
return errors.New("You must provide a tx hash")
@@ -41,11 +42,10 @@ func queryTx(cmd *cobra.Command, args []string) error {
}
// get the node
uri := viper.GetString(client.FlagNode)
if uri == "" {
return errors.New("Must define which node to query with --node")
node, err := client.GetNode()
if err != nil {
return err
}
node := client.GetNode(uri)
prove := !viper.GetBool(client.FlagTrustNode)
res, err := node.Tx(hash, prove)