2015-11-02 07:39:53 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-11-30 20:56:36 -05:00
|
|
|
"bufio"
|
2016-07-01 20:22:58 -04:00
|
|
|
"encoding/hex"
|
2015-12-21 16:18:41 -08:00
|
|
|
"errors"
|
2015-11-02 07:39:53 -08:00
|
|
|
"fmt"
|
2015-12-01 01:49:54 -05:00
|
|
|
"io"
|
2015-11-02 07:39:53 -08:00
|
|
|
"os"
|
2015-11-30 20:56:36 -05:00
|
|
|
"strings"
|
2015-11-02 07:39:53 -08:00
|
|
|
|
2016-01-30 19:36:33 -08:00
|
|
|
"github.com/codegangsta/cli"
|
2015-11-02 07:39:53 -08:00
|
|
|
. "github.com/tendermint/go-common"
|
2016-05-14 03:09:47 -04:00
|
|
|
"github.com/tendermint/tmsp/client"
|
2015-11-02 07:39:53 -08:00
|
|
|
"github.com/tendermint/tmsp/types"
|
|
|
|
)
|
|
|
|
|
2016-05-24 21:51:28 -04:00
|
|
|
// client is a global variable so it can be reused by the console
|
2016-05-14 03:09:47 -04:00
|
|
|
var client tmspcli.Client
|
2015-11-30 20:56:36 -05:00
|
|
|
|
2015-11-02 07:39:53 -08:00
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
2016-01-22 15:50:11 -08:00
|
|
|
app.Name = "tmsp-cli"
|
|
|
|
app.Usage = "tmsp-cli [command] [args...]"
|
2016-07-23 17:37:09 -04:00
|
|
|
app.Version = "0.2"
|
2015-11-02 07:39:53 -08:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "address",
|
2015-12-01 15:27:50 -08:00
|
|
|
Value: "tcp://127.0.0.1:46658",
|
2015-11-02 07:39:53 -08:00
|
|
|
Usage: "address of application socket",
|
|
|
|
},
|
2016-05-18 18:30:38 -04:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "tmsp",
|
|
|
|
Value: "socket",
|
|
|
|
Usage: "socket or grpc",
|
|
|
|
},
|
2016-07-23 17:37:09 -04:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "verbose",
|
|
|
|
Usage: "print the command and results as if it were a console session",
|
|
|
|
},
|
2016-05-18 18:30:38 -04:00
|
|
|
}
|
2015-11-02 07:39:53 -08:00
|
|
|
app.Commands = []cli.Command{
|
2015-12-01 01:49:54 -05:00
|
|
|
{
|
|
|
|
Name: "batch",
|
|
|
|
Usage: "Run a batch of tmsp commands against an application",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdBatch(app, c)
|
2015-12-01 01:49:54 -05:00
|
|
|
},
|
|
|
|
},
|
2015-11-30 20:56:36 -05:00
|
|
|
{
|
|
|
|
Name: "console",
|
|
|
|
Usage: "Start an interactive tmsp console for multiple commands",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdConsole(app, c)
|
2015-11-30 20:56:36 -05:00
|
|
|
},
|
|
|
|
},
|
2015-12-06 18:18:13 -05:00
|
|
|
{
|
|
|
|
Name: "echo",
|
|
|
|
Usage: "Have the application echo a message",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdEcho(c)
|
2015-12-06 18:18:13 -05:00
|
|
|
},
|
|
|
|
},
|
2015-11-30 23:49:18 -05:00
|
|
|
{
|
|
|
|
Name: "info",
|
|
|
|
Usage: "Get some info about the application",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdInfo(c)
|
2015-11-30 23:49:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "set_option",
|
|
|
|
Usage: "Set an option on the application",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdSetOption(c)
|
2015-11-30 23:49:18 -05:00
|
|
|
},
|
|
|
|
},
|
2015-11-02 07:39:53 -08:00
|
|
|
{
|
|
|
|
Name: "append_tx",
|
|
|
|
Usage: "Append a new tx to application",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdAppendTx(c)
|
2015-11-02 07:39:53 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-01-08 16:52:02 -08:00
|
|
|
Name: "check_tx",
|
|
|
|
Usage: "Validate a tx",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdCheckTx(c)
|
2015-11-02 07:39:53 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-02-23 13:36:18 -05:00
|
|
|
Name: "commit",
|
|
|
|
Usage: "Commit the application state and return the Merkle root hash",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdCommit(c)
|
2015-11-02 07:39:53 -08:00
|
|
|
},
|
|
|
|
},
|
2016-01-22 15:50:11 -08:00
|
|
|
{
|
|
|
|
Name: "query",
|
|
|
|
Usage: "Query application state",
|
2016-05-05 14:11:18 -07:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return cmdQuery(c)
|
2016-01-22 15:50:11 -08:00
|
|
|
},
|
|
|
|
},
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|
2015-11-30 20:56:36 -05:00
|
|
|
app.Before = before
|
2016-05-05 14:11:18 -07:00
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
2015-11-02 07:39:53 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-30 20:56:36 -05:00
|
|
|
func before(c *cli.Context) error {
|
2016-05-14 03:09:47 -04:00
|
|
|
if client == nil {
|
2015-11-30 20:56:36 -05:00
|
|
|
var err error
|
2016-05-18 18:30:38 -04:00
|
|
|
client, err = tmspcli.NewClient(c.GlobalString("address"), c.GlobalString("tmsp"), false)
|
2015-11-30 20:56:36 -05:00
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-02 07:39:53 -08:00
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdBatch(app *cli.App, c *cli.Context) error {
|
2015-12-01 01:49:54 -05:00
|
|
|
bufReader := bufio.NewReader(os.Stdin)
|
|
|
|
for {
|
|
|
|
line, more, err := bufReader.ReadLine()
|
|
|
|
if more {
|
2016-05-05 14:11:18 -07:00
|
|
|
return errors.New("Input line is too long")
|
2015-12-01 01:49:54 -05:00
|
|
|
} else if err == io.EOF {
|
|
|
|
break
|
2015-12-14 18:00:18 -05:00
|
|
|
} else if len(line) == 0 {
|
|
|
|
continue
|
2015-12-01 01:49:54 -05:00
|
|
|
} else if err != nil {
|
2016-05-05 14:11:18 -07:00
|
|
|
return err
|
2015-12-01 01:49:54 -05:00
|
|
|
}
|
2016-07-23 17:37:09 -04:00
|
|
|
args := []string{"tmsp-cli"}
|
|
|
|
if c.GlobalBool("verbose") {
|
|
|
|
args = append(args, "--verbose")
|
|
|
|
}
|
2015-12-01 01:49:54 -05:00
|
|
|
args = append(args, strings.Split(string(line), " ")...)
|
|
|
|
app.Run(args)
|
|
|
|
}
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2015-12-01 01:49:54 -05:00
|
|
|
}
|
|
|
|
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdConsole(app *cli.App, c *cli.Context) error {
|
2015-11-30 20:56:36 -05:00
|
|
|
for {
|
2016-01-12 14:04:24 -08:00
|
|
|
fmt.Printf("\n> ")
|
2015-11-30 20:56:36 -05:00
|
|
|
bufReader := bufio.NewReader(os.Stdin)
|
|
|
|
line, more, err := bufReader.ReadLine()
|
|
|
|
if more {
|
2016-05-05 14:11:18 -07:00
|
|
|
return errors.New("Input is too long")
|
2015-11-30 20:56:36 -05:00
|
|
|
} else if err != nil {
|
2016-05-05 14:11:18 -07:00
|
|
|
return err
|
2015-11-30 20:56:36 -05:00
|
|
|
}
|
|
|
|
|
2016-07-23 17:37:09 -04:00
|
|
|
args := []string{"tmsp-cli"}
|
2015-11-30 20:56:36 -05:00
|
|
|
args = append(args, strings.Split(string(line), " ")...)
|
2016-05-14 03:09:47 -04:00
|
|
|
if err := app.Run(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-30 20:56:36 -05:00
|
|
|
}
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2015-11-30 20:56:36 -05:00
|
|
|
}
|
|
|
|
|
2015-12-06 18:18:13 -05:00
|
|
|
// Have the application echo a message
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdEcho(c *cli.Context) error {
|
2015-12-06 18:18:13 -05:00
|
|
|
args := c.Args()
|
|
|
|
if len(args) != 1 {
|
2016-05-05 14:11:18 -07:00
|
|
|
return errors.New("Command echo takes 1 argument")
|
2015-12-06 18:18:13 -05:00
|
|
|
}
|
2016-05-14 03:09:47 -04:00
|
|
|
res := client.EchoSync(args[0])
|
2016-07-23 17:37:09 -04:00
|
|
|
printResponse(c, res, string(res.Data), false)
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2015-12-06 18:18:13 -05:00
|
|
|
}
|
|
|
|
|
2015-11-30 23:49:18 -05:00
|
|
|
// Get some info from the application
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdInfo(c *cli.Context) error {
|
2016-05-14 03:09:47 -04:00
|
|
|
res := client.InfoSync()
|
2016-07-23 17:37:09 -04:00
|
|
|
printResponse(c, res, string(res.Data), false)
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2015-11-30 23:49:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set an option on the application
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdSetOption(c *cli.Context) error {
|
2015-11-30 23:49:18 -05:00
|
|
|
args := c.Args()
|
|
|
|
if len(args) != 2 {
|
2016-05-05 14:11:18 -07:00
|
|
|
return errors.New("Command set_option takes 2 arguments (key, value)")
|
2015-11-30 23:49:18 -05:00
|
|
|
}
|
2016-05-14 03:09:47 -04:00
|
|
|
res := client.SetOptionSync(args[0], args[1])
|
2016-07-23 17:37:09 -04:00
|
|
|
printResponse(c, res, Fmt("%s=%s", args[0], args[1]), false)
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2015-11-30 23:49:18 -05:00
|
|
|
}
|
|
|
|
|
2015-11-02 07:39:53 -08:00
|
|
|
// Append a new tx to application
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdAppendTx(c *cli.Context) error {
|
2015-11-30 23:49:18 -05:00
|
|
|
args := c.Args()
|
|
|
|
if len(args) != 1 {
|
2016-05-05 14:11:18 -07:00
|
|
|
return errors.New("Command append_tx takes 1 argument")
|
2015-11-30 23:49:18 -05:00
|
|
|
}
|
2016-07-01 20:22:58 -04:00
|
|
|
txBytes := stringOrHexToBytes(c.Args()[0])
|
2016-05-14 03:09:47 -04:00
|
|
|
res := client.AppendTxSync(txBytes)
|
2016-07-23 17:37:09 -04:00
|
|
|
printResponse(c, res, string(res.Data), true)
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
// Validate a tx
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdCheckTx(c *cli.Context) error {
|
2016-01-08 16:52:02 -08:00
|
|
|
args := c.Args()
|
|
|
|
if len(args) != 1 {
|
2016-05-05 14:11:18 -07:00
|
|
|
return errors.New("Command check_tx takes 1 argument")
|
2016-01-08 16:52:02 -08:00
|
|
|
}
|
2016-07-01 20:22:58 -04:00
|
|
|
txBytes := stringOrHexToBytes(c.Args()[0])
|
2016-05-14 03:09:47 -04:00
|
|
|
res := client.CheckTxSync(txBytes)
|
2016-07-23 17:37:09 -04:00
|
|
|
printResponse(c, res, string(res.Data), true)
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
// Get application Merkle root hash
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdCommit(c *cli.Context) error {
|
2016-05-14 03:09:47 -04:00
|
|
|
res := client.CommitSync()
|
2016-07-23 17:37:09 -04:00
|
|
|
printResponse(c, res, Fmt("%X", res.Data), false)
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|
|
|
|
|
2016-01-22 15:50:11 -08:00
|
|
|
// Query application state
|
2016-05-05 14:11:18 -07:00
|
|
|
func cmdQuery(c *cli.Context) error {
|
2016-01-22 15:50:11 -08:00
|
|
|
args := c.Args()
|
|
|
|
if len(args) != 1 {
|
2016-05-05 14:11:18 -07:00
|
|
|
return errors.New("Command query takes 1 argument")
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
2016-07-01 20:22:58 -04:00
|
|
|
queryBytes := stringOrHexToBytes(c.Args()[0])
|
2016-05-14 03:09:47 -04:00
|
|
|
res := client.QuerySync(queryBytes)
|
2016-07-23 17:37:09 -04:00
|
|
|
printResponse(c, res, string(res.Data), true)
|
2016-05-05 14:11:18 -07:00
|
|
|
return nil
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2015-11-02 07:39:53 -08:00
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
2016-07-23 17:37:09 -04:00
|
|
|
func printResponse(c *cli.Context, res types.Result, s string, printCode bool) {
|
|
|
|
if c.GlobalBool("verbose") {
|
|
|
|
fmt.Println(">", c.Command.Name, strings.Join(c.Args(), " "))
|
|
|
|
}
|
|
|
|
|
2016-05-14 03:09:47 -04:00
|
|
|
if printCode {
|
2016-02-23 13:36:18 -05:00
|
|
|
fmt.Printf("-> code: %s\n", res.Code.String())
|
2016-02-07 19:59:19 -08:00
|
|
|
}
|
2016-05-14 03:09:47 -04:00
|
|
|
/*if res.Error != "" {
|
2016-02-23 13:36:18 -05:00
|
|
|
fmt.Printf("-> error: %s\n", res.Error)
|
2016-05-14 03:09:47 -04:00
|
|
|
}*/
|
2016-02-07 19:59:19 -08:00
|
|
|
if s != "" {
|
2016-02-23 13:36:18 -05:00
|
|
|
fmt.Printf("-> data: {%s}\n", s)
|
2016-02-07 19:59:19 -08:00
|
|
|
}
|
2016-02-08 13:47:47 -08:00
|
|
|
if res.Log != "" {
|
2016-02-23 13:36:18 -05:00
|
|
|
fmt.Printf("-> log: %s\n", res.Log)
|
2016-02-08 13:47:47 -08:00
|
|
|
}
|
2016-02-23 13:36:18 -05:00
|
|
|
|
2016-07-23 17:37:09 -04:00
|
|
|
if c.GlobalBool("verbose") {
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
|
2016-02-07 19:59:19 -08:00
|
|
|
}
|
2016-07-01 20:22:58 -04:00
|
|
|
|
|
|
|
// NOTE: s is interpreted as a string unless prefixed with 0x
|
|
|
|
func stringOrHexToBytes(s string) []byte {
|
|
|
|
if len(s) > 2 && s[:2] == "0x" {
|
|
|
|
b, err := hex.DecodeString(s[2:])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error decoding hex argument:", err.Error())
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return []byte(s)
|
|
|
|
}
|