tendermint/cmd/tmsp-cli/tmsp-cli.go

327 lines
6.6 KiB
Go
Raw Normal View History

2015-11-02 07:39:53 -08:00
package main
import (
2015-11-30 20:56:36 -05:00
"bufio"
"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
"net"
"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-03-22 15:18:03 -07:00
"github.com/tendermint/go-wire/expr"
2015-11-02 07:39:53 -08:00
"github.com/tendermint/tmsp/types"
)
2015-11-30 20:56:36 -05:00
// connection is a global variable so it can be reused by the console
var conn net.Conn
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...]"
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",
},
}
app.Commands = []cli.Command{
2015-12-01 01:49:54 -05:00
{
Name: "batch",
Usage: "Run a batch of tmsp commands against an application",
Action: func(c *cli.Context) {
cmdBatch(app, c)
},
},
2015-11-30 20:56:36 -05:00
{
Name: "console",
Usage: "Start an interactive tmsp console for multiple commands",
Action: func(c *cli.Context) {
cmdConsole(app, c)
},
},
2015-12-06 18:18:13 -05:00
{
Name: "echo",
Usage: "Have the application echo a message",
Action: func(c *cli.Context) {
cmdEcho(c)
},
},
{
Name: "info",
Usage: "Get some info about the application",
Action: func(c *cli.Context) {
cmdInfo(c)
},
},
{
Name: "set_option",
Usage: "Set an option on the application",
Action: func(c *cli.Context) {
cmdSetOption(c)
},
},
2015-11-02 07:39:53 -08:00
{
Name: "append_tx",
Usage: "Append a new tx to application",
Action: func(c *cli.Context) {
cmdAppendTx(c)
},
},
{
Name: "check_tx",
Usage: "Validate a tx",
2015-11-02 07:39:53 -08:00
Action: func(c *cli.Context) {
cmdCheckTx(c)
2015-11-02 07:39:53 -08:00
},
},
{
Name: "commit",
Usage: "Commit the application state and return the Merkle root hash",
2015-11-02 07:39:53 -08:00
Action: func(c *cli.Context) {
2016-02-14 13:11:06 -08:00
cmdCommit(c)
2015-11-02 07:39:53 -08:00
},
},
2016-01-22 15:50:11 -08:00
{
Name: "query",
Usage: "Query application state",
Action: func(c *cli.Context) {
cmdQuery(c)
},
},
2015-11-02 07:39:53 -08:00
}
2015-11-30 20:56:36 -05:00
app.Before = before
2015-11-02 07:39:53 -08:00
app.Run(os.Args)
}
2015-11-30 20:56:36 -05:00
func before(c *cli.Context) error {
if conn == nil {
var err error
conn, err = Connect(c.GlobalString("address"))
if err != nil {
Exit(err.Error())
}
}
return nil
}
2015-11-02 07:39:53 -08:00
//--------------------------------------------------------------------------------
2015-12-01 01:49:54 -05:00
func cmdBatch(app *cli.App, c *cli.Context) {
bufReader := bufio.NewReader(os.Stdin)
for {
line, more, err := bufReader.ReadLine()
if more {
fmt.Println("input line is too long")
return
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 {
fmt.Println(err.Error())
return
2015-12-01 01:49:54 -05:00
}
args := []string{"tmsp"}
args = append(args, strings.Split(string(line), " ")...)
app.Run(args)
}
}
2015-11-30 20:56:36 -05:00
func cmdConsole(app *cli.App, c *cli.Context) {
for {
fmt.Printf("\n> ")
2015-11-30 20:56:36 -05:00
bufReader := bufio.NewReader(os.Stdin)
line, more, err := bufReader.ReadLine()
if more {
fmt.Println("input is too long")
return
2015-11-30 20:56:36 -05:00
} else if err != nil {
fmt.Println(err.Error())
return
2015-11-30 20:56:36 -05:00
}
args := []string{"tmsp"}
args = append(args, strings.Split(string(line), " ")...)
app.Run(args)
}
}
2015-12-06 18:18:13 -05:00
// Have the application echo a message
func cmdEcho(c *cli.Context) {
args := c.Args()
if len(args) != 1 {
fmt.Println("echo takes 1 argument")
return
2015-12-06 18:18:13 -05:00
}
2016-01-30 19:36:33 -08:00
res, err := makeRequest(conn, types.RequestEcho(args[0]))
2015-12-06 18:18:13 -05:00
if err != nil {
fmt.Println(err.Error())
return
2015-12-06 18:18:13 -05:00
}
2016-02-07 19:59:19 -08:00
printResponse(res, string(res.Data))
2015-12-06 18:18:13 -05:00
}
// Get some info from the application
func cmdInfo(c *cli.Context) {
2016-01-30 19:36:33 -08:00
res, err := makeRequest(conn, types.RequestInfo())
if err != nil {
fmt.Println(err.Error())
return
}
2016-02-07 19:59:19 -08:00
printResponse(res, string(res.Data))
}
// Set an option on the application
func cmdSetOption(c *cli.Context) {
args := c.Args()
if len(args) != 2 {
fmt.Println("set_option takes 2 arguments (key, value)")
return
}
2016-02-07 19:59:19 -08:00
res, err := makeRequest(conn, types.RequestSetOption(args[0], args[1]))
if err != nil {
fmt.Println(err.Error())
return
}
2016-02-07 19:59:19 -08:00
printResponse(res, Fmt("%s=%s", args[0], args[1]))
}
2015-11-02 07:39:53 -08:00
// Append a new tx to application
func cmdAppendTx(c *cli.Context) {
args := c.Args()
if len(args) != 1 {
fmt.Println("append_tx takes 1 argument")
return
}
2016-03-22 15:18:03 -07:00
txExprString := c.Args()[0]
txBytes, err := expr.Compile(txExprString)
if err != nil {
fmt.Println(err.Error())
return
}
2016-03-22 15:18:03 -07:00
res, err := makeRequest(conn, types.RequestAppendTx(txBytes))
2015-11-02 07:39:53 -08:00
if err != nil {
fmt.Println(err.Error())
return
2015-11-02 07:39:53 -08:00
}
2016-02-07 19:59:19 -08:00
printResponse(res, string(res.Data))
2015-11-02 07:39:53 -08:00
}
// Validate a tx
func cmdCheckTx(c *cli.Context) {
args := c.Args()
if len(args) != 1 {
2016-02-08 13:47:47 -08:00
fmt.Println("check_tx takes 1 argument")
return
}
2016-03-22 15:18:03 -07:00
txExprString := c.Args()[0]
txBytes, err := expr.Compile(txExprString)
if err != nil {
fmt.Println(err.Error())
return
2015-11-02 07:39:53 -08:00
}
2016-03-22 15:18:03 -07:00
res, err := makeRequest(conn, types.RequestCheckTx(txBytes))
2015-11-02 07:39:53 -08:00
if err != nil {
fmt.Println(err.Error())
return
2015-11-02 07:39:53 -08:00
}
2016-02-07 19:59:19 -08:00
printResponse(res, string(res.Data))
2015-11-02 07:39:53 -08:00
}
// Get application Merkle root hash
2016-02-14 13:11:06 -08:00
func cmdCommit(c *cli.Context) {
res, err := makeRequest(conn, types.RequestCommit())
2015-11-02 07:39:53 -08:00
if err != nil {
fmt.Println(err.Error())
return
2015-11-02 07:39:53 -08:00
}
2016-02-07 19:59:19 -08:00
printResponse(res, Fmt("%X", res.Data))
2015-11-02 07:39:53 -08:00
}
2016-01-22 15:50:11 -08:00
// Query application state
func cmdQuery(c *cli.Context) {
args := c.Args()
if len(args) != 1 {
2016-02-08 13:47:47 -08:00
fmt.Println("query takes 1 argument")
return
2016-01-22 15:50:11 -08:00
}
2016-03-22 15:18:03 -07:00
queryExprString := args[0]
queryBytes, err := expr.Compile(queryExprString)
if err != nil {
fmt.Println(err.Error())
return
2016-01-22 15:50:11 -08:00
}
2016-03-22 15:18:03 -07:00
res, err := makeRequest(conn, types.RequestQuery(queryBytes))
2016-01-22 15:50:11 -08:00
if err != nil {
fmt.Println(err.Error())
return
2016-01-22 15:50:11 -08:00
}
2016-02-07 19:59:19 -08:00
printResponse(res, string(res.Data))
2016-01-22 15:50:11 -08:00
}
2015-11-02 07:39:53 -08:00
//--------------------------------------------------------------------------------
2016-02-07 19:59:19 -08:00
func printResponse(res *types.Response, s string) {
switch res.Type {
case types.MessageType_AppendTx, types.MessageType_CheckTx, types.MessageType_Query:
fmt.Printf("-> code: %s\n", res.Code.String())
2016-02-07 19:59:19 -08:00
}
if res.Error != "" {
fmt.Printf("-> error: %s\n", res.Error)
2016-02-07 19:59:19 -08:00
}
if s != "" {
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 != "" {
fmt.Printf("-> log: %s\n", res.Log)
2016-02-08 13:47:47 -08:00
}
2016-02-07 19:59:19 -08:00
}
func responseString(res *types.Response) string {
return Fmt("type: %v\tdata: %v\tcode: %v", res.Type, res.Data, res.Code)
}
2016-01-30 19:36:33 -08:00
func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) {
// Write desired request
2016-01-30 19:36:33 -08:00
err := types.WriteMessage(req, conn)
2015-11-02 07:39:53 -08:00
if err != nil {
return nil, err
}
2015-11-27 18:42:00 -05:00
// Write flush request
2016-01-30 19:36:33 -08:00
err = types.WriteMessage(types.RequestFlush(), conn)
2015-11-27 18:42:00 -05:00
if err != nil {
return nil, err
}
// Read desired response
2016-01-30 19:36:33 -08:00
var res = &types.Response{}
err = types.ReadMessage(conn, res)
if err != nil {
return nil, err
}
// Read flush response
2016-01-30 19:36:33 -08:00
var resFlush = &types.Response{}
err = types.ReadMessage(conn, resFlush)
if err != nil {
return nil, err
}
2016-01-31 19:56:02 -08:00
if resFlush.Type != types.MessageType_Flush {
return nil, errors.New(Fmt("Expected types.MessageType_Flush but got %v instead", resFlush.Type))
}
return res, nil
2015-11-02 07:39:53 -08:00
}