2015-11-02 07:39:53 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-11-30 20:56:36 -05:00
|
|
|
"bufio"
|
2015-11-30 23:49:18 -05:00
|
|
|
"encoding/hex"
|
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
|
|
|
|
|
|
|
. "github.com/tendermint/go-common"
|
|
|
|
"github.com/tendermint/go-wire"
|
|
|
|
"github.com/tendermint/tmsp/types"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
app.Name = "cli"
|
|
|
|
app.Usage = "cli [command] [args...]"
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
},
|
2015-11-30 23:49:18 -05:00
|
|
|
{
|
|
|
|
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: "get_hash",
|
|
|
|
Usage: "Get application Merkle root hash",
|
|
|
|
Action: func(c *cli.Context) {
|
|
|
|
cmdGetHash(c)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "commit",
|
|
|
|
Usage: "Commit the application state",
|
|
|
|
Action: func(c *cli.Context) {
|
|
|
|
cmdCommit(c)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "rollback",
|
|
|
|
Usage: "Roll back the application state to the latest commit",
|
|
|
|
Action: func(c *cli.Context) {
|
|
|
|
cmdRollback(c)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
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 {
|
|
|
|
Exit("input line is too long")
|
|
|
|
} 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 {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
|
|
|
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("> ")
|
|
|
|
bufReader := bufio.NewReader(os.Stdin)
|
|
|
|
line, more, err := bufReader.ReadLine()
|
|
|
|
if more {
|
|
|
|
Exit("input is too long")
|
|
|
|
} else if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Exit("echo takes 1 argument")
|
|
|
|
}
|
|
|
|
res, err := makeRequest(conn, types.RequestEcho{args[0]})
|
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
|
|
|
fmt.Println(res)
|
|
|
|
}
|
|
|
|
|
2015-11-30 23:49:18 -05:00
|
|
|
// Get some info from the application
|
|
|
|
func cmdInfo(c *cli.Context) {
|
|
|
|
res, err := makeRequest(conn, types.RequestInfo{})
|
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
|
|
|
fmt.Println(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set an option on the application
|
|
|
|
func cmdSetOption(c *cli.Context) {
|
|
|
|
args := c.Args()
|
|
|
|
if len(args) != 2 {
|
|
|
|
Exit("set_option takes 2 arguments (key, value)")
|
|
|
|
}
|
|
|
|
_, err := makeRequest(conn, types.RequestSetOption{args[0], args[1]})
|
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
2015-12-01 01:49:54 -05:00
|
|
|
fmt.Printf("%s=%s\n", args[0], args[1])
|
2015-11-30 23:49:18 -05:00
|
|
|
}
|
|
|
|
|
2015-11-02 07:39:53 -08:00
|
|
|
// Append a new tx to application
|
|
|
|
func cmdAppendTx(c *cli.Context) {
|
2015-11-30 23:49:18 -05:00
|
|
|
args := c.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
Exit("append_tx takes 1 argument")
|
|
|
|
}
|
|
|
|
txString := args[0]
|
|
|
|
tx := []byte(txString)
|
|
|
|
if len(txString) > 2 && strings.HasPrefix(txString, "0x") {
|
|
|
|
var err error
|
|
|
|
tx, err = hex.DecodeString(txString[2:])
|
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := makeRequest(conn, types.RequestAppendTx{tx})
|
2015-11-02 07:39:53 -08:00
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
2015-12-07 18:16:03 -05:00
|
|
|
fmt.Println("Response:", res)
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get application Merkle root hash
|
|
|
|
func cmdGetHash(c *cli.Context) {
|
2015-11-29 14:22:30 -08:00
|
|
|
res, err := makeRequest(conn, types.RequestGetHash{})
|
2015-11-02 07:39:53 -08:00
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
2015-12-01 01:49:54 -05:00
|
|
|
fmt.Printf("%X\n", res.(types.ResponseGetHash).Hash)
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the application state
|
|
|
|
func cmdCommit(c *cli.Context) {
|
2015-11-30 20:56:36 -05:00
|
|
|
_, err := makeRequest(conn, types.RequestCommit{})
|
2015-11-02 07:39:53 -08:00
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
|
|
|
fmt.Println("Committed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Roll back the application state to the latest commit
|
|
|
|
func cmdRollback(c *cli.Context) {
|
2015-11-30 20:56:36 -05:00
|
|
|
_, err := makeRequest(conn, types.RequestRollback{})
|
2015-11-02 07:39:53 -08:00
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
|
|
|
}
|
|
|
|
fmt.Println("Rolled back.")
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
2015-11-29 14:22:30 -08:00
|
|
|
func makeRequest(conn net.Conn, req types.Request) (types.Response, error) {
|
2015-11-10 12:49:07 -08:00
|
|
|
var n int
|
2015-11-02 07:39:53 -08:00
|
|
|
var err error
|
2015-11-29 14:22:30 -08:00
|
|
|
|
|
|
|
// Write desired request
|
2015-12-20 09:16:05 -08:00
|
|
|
wire.WriteBinaryLengthPrefixed(req, conn, &n, &err)
|
2015-11-02 07:39:53 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-27 18:42:00 -05:00
|
|
|
|
2015-11-29 14:22:30 -08:00
|
|
|
// Write flush request
|
2015-12-20 09:16:05 -08:00
|
|
|
wire.WriteBinaryLengthPrefixed(types.RequestFlush{}, conn, &n, &err)
|
2015-11-27 18:42:00 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-29 14:22:30 -08:00
|
|
|
// Read desired response
|
2015-11-02 07:39:53 -08:00
|
|
|
var res types.Response
|
2015-12-20 09:16:05 -08:00
|
|
|
wire.ReadBinaryPtrLengthPrefixed(&res, conn, 0, &n, &err)
|
2015-11-29 14:22:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read flush response
|
|
|
|
var resFlush types.ResponseFlush
|
2015-12-20 09:16:05 -08:00
|
|
|
wire.ReadBinaryPtrLengthPrefixed(&resFlush, conn, 0, &n, &err)
|
2015-11-29 14:22:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|