Conform to new codegangsta.cli ActionFunc interface

This commit is contained in:
Jae Kwon
2016-05-05 14:11:18 -07:00
parent 3910f871dd
commit 7ffd289909

View File

@ -33,69 +33,72 @@ func main() {
{ {
Name: "batch", Name: "batch",
Usage: "Run a batch of tmsp commands against an application", Usage: "Run a batch of tmsp commands against an application",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdBatch(app, c) return cmdBatch(app, c)
}, },
}, },
{ {
Name: "console", Name: "console",
Usage: "Start an interactive tmsp console for multiple commands", Usage: "Start an interactive tmsp console for multiple commands",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdConsole(app, c) return cmdConsole(app, c)
}, },
}, },
{ {
Name: "echo", Name: "echo",
Usage: "Have the application echo a message", Usage: "Have the application echo a message",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdEcho(c) return cmdEcho(c)
}, },
}, },
{ {
Name: "info", Name: "info",
Usage: "Get some info about the application", Usage: "Get some info about the application",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdInfo(c) return cmdInfo(c)
}, },
}, },
{ {
Name: "set_option", Name: "set_option",
Usage: "Set an option on the application", Usage: "Set an option on the application",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdSetOption(c) return cmdSetOption(c)
}, },
}, },
{ {
Name: "append_tx", Name: "append_tx",
Usage: "Append a new tx to application", Usage: "Append a new tx to application",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdAppendTx(c) return cmdAppendTx(c)
}, },
}, },
{ {
Name: "check_tx", Name: "check_tx",
Usage: "Validate a tx", Usage: "Validate a tx",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdCheckTx(c) return cmdCheckTx(c)
}, },
}, },
{ {
Name: "commit", Name: "commit",
Usage: "Commit the application state and return the Merkle root hash", Usage: "Commit the application state and return the Merkle root hash",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdCommit(c) return cmdCommit(c)
}, },
}, },
{ {
Name: "query", Name: "query",
Usage: "Query application state", Usage: "Query application state",
Action: func(c *cli.Context) { Action: func(c *cli.Context) error {
cmdQuery(c) return cmdQuery(c)
}, },
}, },
} }
app.Before = before app.Before = before
app.Run(os.Args) err := app.Run(os.Args)
if err != nil {
Exit(err.Error())
}
} }
@ -112,160 +115,150 @@ func before(c *cli.Context) error {
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
func cmdBatch(app *cli.App, c *cli.Context) { func cmdBatch(app *cli.App, c *cli.Context) error {
bufReader := bufio.NewReader(os.Stdin) bufReader := bufio.NewReader(os.Stdin)
for { for {
line, more, err := bufReader.ReadLine() line, more, err := bufReader.ReadLine()
if more { if more {
fmt.Println("input line is too long") return errors.New("Input line is too long")
return
} else if err == io.EOF { } else if err == io.EOF {
break break
} else if len(line) == 0 { } else if len(line) == 0 {
continue continue
} else if err != nil { } else if err != nil {
fmt.Println(err.Error()) return err
return
} }
args := []string{"tmsp"} args := []string{"tmsp"}
args = append(args, strings.Split(string(line), " ")...) args = append(args, strings.Split(string(line), " ")...)
app.Run(args) app.Run(args)
} }
return nil
} }
func cmdConsole(app *cli.App, c *cli.Context) { func cmdConsole(app *cli.App, c *cli.Context) error {
for { for {
fmt.Printf("\n> ") fmt.Printf("\n> ")
bufReader := bufio.NewReader(os.Stdin) bufReader := bufio.NewReader(os.Stdin)
line, more, err := bufReader.ReadLine() line, more, err := bufReader.ReadLine()
if more { if more {
fmt.Println("input is too long") return errors.New("Input is too long")
return
} else if err != nil { } else if err != nil {
fmt.Println(err.Error()) return err
return
} }
args := []string{"tmsp"} args := []string{"tmsp"}
args = append(args, strings.Split(string(line), " ")...) args = append(args, strings.Split(string(line), " ")...)
app.Run(args) app.Run(args)
} }
return nil
} }
// Have the application echo a message // Have the application echo a message
func cmdEcho(c *cli.Context) { func cmdEcho(c *cli.Context) error {
args := c.Args() args := c.Args()
if len(args) != 1 { if len(args) != 1 {
fmt.Println("echo takes 1 argument") return errors.New("Command echo takes 1 argument")
return
} }
res, err := makeRequest(conn, types.RequestEcho(args[0])) res, err := makeRequest(conn, types.RequestEcho(args[0]))
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
printResponse(res, string(res.Data)) printResponse(res, string(res.Data))
return nil
} }
// Get some info from the application // Get some info from the application
func cmdInfo(c *cli.Context) { func cmdInfo(c *cli.Context) error {
res, err := makeRequest(conn, types.RequestInfo()) res, err := makeRequest(conn, types.RequestInfo())
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
printResponse(res, string(res.Data)) printResponse(res, string(res.Data))
return nil
} }
// Set an option on the application // Set an option on the application
func cmdSetOption(c *cli.Context) { func cmdSetOption(c *cli.Context) error {
args := c.Args() args := c.Args()
if len(args) != 2 { if len(args) != 2 {
fmt.Println("set_option takes 2 arguments (key, value)") return errors.New("Command set_option takes 2 arguments (key, value)")
return
} }
res, err := makeRequest(conn, types.RequestSetOption(args[0], args[1])) res, err := makeRequest(conn, types.RequestSetOption(args[0], args[1]))
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
printResponse(res, Fmt("%s=%s", args[0], args[1])) printResponse(res, Fmt("%s=%s", args[0], args[1]))
return nil
} }
// Append a new tx to application // Append a new tx to application
func cmdAppendTx(c *cli.Context) { func cmdAppendTx(c *cli.Context) error {
args := c.Args() args := c.Args()
if len(args) != 1 { if len(args) != 1 {
fmt.Println("append_tx takes 1 argument") return errors.New("Command append_tx takes 1 argument")
return
} }
txExprString := c.Args()[0] txExprString := c.Args()[0]
txBytes, err := expr.Compile(txExprString) txBytes, err := expr.Compile(txExprString)
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
res, err := makeRequest(conn, types.RequestAppendTx(txBytes)) res, err := makeRequest(conn, types.RequestAppendTx(txBytes))
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
printResponse(res, string(res.Data)) printResponse(res, string(res.Data))
return nil
} }
// Validate a tx // Validate a tx
func cmdCheckTx(c *cli.Context) { func cmdCheckTx(c *cli.Context) error {
args := c.Args() args := c.Args()
if len(args) != 1 { if len(args) != 1 {
fmt.Println("check_tx takes 1 argument") return errors.New("Command check_tx takes 1 argument")
return
} }
txExprString := c.Args()[0] txExprString := c.Args()[0]
txBytes, err := expr.Compile(txExprString) txBytes, err := expr.Compile(txExprString)
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
res, err := makeRequest(conn, types.RequestCheckTx(txBytes)) res, err := makeRequest(conn, types.RequestCheckTx(txBytes))
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
printResponse(res, string(res.Data)) printResponse(res, string(res.Data))
return nil
} }
// Get application Merkle root hash // Get application Merkle root hash
func cmdCommit(c *cli.Context) { func cmdCommit(c *cli.Context) error {
res, err := makeRequest(conn, types.RequestCommit()) res, err := makeRequest(conn, types.RequestCommit())
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
printResponse(res, Fmt("%X", res.Data)) printResponse(res, Fmt("%X", res.Data))
return nil
} }
// Query application state // Query application state
func cmdQuery(c *cli.Context) { func cmdQuery(c *cli.Context) error {
args := c.Args() args := c.Args()
if len(args) != 1 { if len(args) != 1 {
fmt.Println("query takes 1 argument") return errors.New("Command query takes 1 argument")
return
} }
queryExprString := args[0] queryExprString := args[0]
queryBytes, err := expr.Compile(queryExprString) queryBytes, err := expr.Compile(queryExprString)
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
res, err := makeRequest(conn, types.RequestQuery(queryBytes)) res, err := makeRequest(conn, types.RequestQuery(queryBytes))
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
} }
printResponse(res, string(res.Data)) printResponse(res, string(res.Data))
return nil
} }
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------