response struct and persistent args

This commit is contained in:
Ethan Buchman 2017-01-12 01:03:51 -05:00
parent 81b2697cce
commit e6b9a2b6aa

View File

@ -15,6 +15,30 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
) )
//structure for data passed to print response
// variables must be exposed for JSON to read
type response struct {
Res types.Result
Data string
PrintCode bool
Code string
}
func newResponse(res types.Result, data string, printCode bool) *response {
rsp := &response{
Res: res,
Data: data,
PrintCode: printCode,
Code: "",
}
if printCode {
rsp.Code = res.Code.String()
}
return rsp
}
// client is a global variable so it can be reused by the console // client is a global variable so it can be reused by the console
var client tmspcli.Client var client tmspcli.Client
@ -135,6 +159,20 @@ func badCmd(c *cli.Context, cmd string) {
cli.DefaultAppComplete(c) cli.DefaultAppComplete(c)
} }
//Generates new Args array based off of previous call args to maintain flag persistence
func persistentArgs(line []byte) []string {
//generate the arguments to run from orginal os.Args
// to maintain flag arguments
args := os.Args
args = args[:len(args)-1] // remove the previous command argument
if len(line) > 0 { //prevents introduction of extra space leading to argument parse errors
args = append(args, strings.Split(string(line), " ")...)
}
return args
}
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
func cmdBatch(app *cli.App, c *cli.Context) error { func cmdBatch(app *cli.App, c *cli.Context) error {
@ -150,12 +188,9 @@ func cmdBatch(app *cli.App, c *cli.Context) error {
} else if err != nil { } else if err != nil {
return err return err
} }
args := []string{"tmsp-cli"}
if c.GlobalBool("verbose") { args := persistentArgs(line)
args = append(args, "--verbose") app.Run(args) //cli prints error within its func call
}
args = append(args, strings.Split(string(line), " ")...)
app.Run(args)
} }
return nil return nil
} }
@ -163,6 +198,7 @@ func cmdBatch(app *cli.App, c *cli.Context) error {
func cmdConsole(app *cli.App, c *cli.Context) error { func cmdConsole(app *cli.App, c *cli.Context) error {
// don't hard exit on mistyped commands (eg. check vs check_tx) // don't hard exit on mistyped commands (eg. check vs check_tx)
app.CommandNotFound = badCmd app.CommandNotFound = badCmd
for { for {
fmt.Printf("\n> ") fmt.Printf("\n> ")
bufReader := bufio.NewReader(os.Stdin) bufReader := bufio.NewReader(os.Stdin)
@ -173,9 +209,8 @@ func cmdConsole(app *cli.App, c *cli.Context) error {
return err return err
} }
args := []string{"tmsp-cli"} args := persistentArgs(line)
args = append(args, strings.Split(string(line), " ")...) app.Run(args) //cli prints error within its func call
app.Run(args) //cli already prints error within its func call
} }
} }
@ -186,14 +221,16 @@ func cmdEcho(c *cli.Context) error {
return errors.New("Command echo takes 1 argument") return errors.New("Command echo takes 1 argument")
} }
res := client.EchoSync(args[0]) res := client.EchoSync(args[0])
printResponse(c, res, string(res.Data), false) rsp := newResponse(res, string(res.Data), false)
printResponse(c, rsp)
return nil return nil
} }
// Get some info from the application // Get some info from the application
func cmdInfo(c *cli.Context) error { func cmdInfo(c *cli.Context) error {
res, _, _, _ := client.InfoSync() res, _, _, _ := client.InfoSync()
printResponse(c, res, string(res.Data), false) rsp := newResponse(res, string(res.Data), false)
printResponse(c, rsp)
return nil return nil
} }
@ -204,7 +241,8 @@ func cmdSetOption(c *cli.Context) error {
return errors.New("Command set_option takes 2 arguments (key, value)") return errors.New("Command set_option takes 2 arguments (key, value)")
} }
res := client.SetOptionSync(args[0], args[1]) res := client.SetOptionSync(args[0], args[1])
printResponse(c, res, Fmt("%s=%s", args[0], args[1]), false) rsp := newResponse(res, Fmt("%s=%s", args[0], args[1]), false)
printResponse(c, rsp)
return nil return nil
} }
@ -219,7 +257,8 @@ func cmdAppendTx(c *cli.Context) error {
return err return err
} }
res := client.AppendTxSync(txBytes) res := client.AppendTxSync(txBytes)
printResponse(c, res, string(res.Data), true) rsp := newResponse(res, string(res.Data), true)
printResponse(c, rsp)
return nil return nil
} }
@ -234,14 +273,16 @@ func cmdCheckTx(c *cli.Context) error {
return err return err
} }
res := client.CheckTxSync(txBytes) res := client.CheckTxSync(txBytes)
printResponse(c, res, string(res.Data), true) rsp := newResponse(res, string(res.Data), true)
printResponse(c, rsp)
return nil return nil
} }
// Get application Merkle root hash // Get application Merkle root hash
func cmdCommit(c *cli.Context) error { func cmdCommit(c *cli.Context) error {
res := client.CommitSync() res := client.CommitSync()
printResponse(c, res, Fmt("0x%X", res.Data), false) rsp := newResponse(res, Fmt("0x%X", res.Data), false)
printResponse(c, rsp)
return nil return nil
} }
@ -256,31 +297,37 @@ func cmdQuery(c *cli.Context) error {
return err return err
} }
res := client.QuerySync(queryBytes) res := client.QuerySync(queryBytes)
printResponse(c, res, string(res.Data), true) rsp := newResponse(res, string(res.Data), true)
printResponse(c, rsp)
return nil return nil
} }
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
func printResponse(c *cli.Context, res types.Result, s string, printCode bool) { func printResponse(c *cli.Context, rsp *response) {
if c.GlobalBool("verbose") {
verbose := c.GlobalBool("verbose")
if verbose {
fmt.Println(">", c.Command.Name, strings.Join(c.Args(), " ")) fmt.Println(">", c.Command.Name, strings.Join(c.Args(), " "))
} }
if printCode { if rsp.PrintCode {
fmt.Printf("-> code: %s\n", res.Code.String()) fmt.Printf("-> code: %s\n", rsp.Code)
}
/*if res.Error != "" {
fmt.Printf("-> error: %s\n", res.Error)
}*/
if s != "" {
fmt.Printf("-> data: %s\n", s)
}
if res.Log != "" {
fmt.Printf("-> log: %s\n", res.Log)
} }
if c.GlobalBool("verbose") { //if pr.res.Error != "" {
// fmt.Printf("-> error: %s\n", pr.res.Error)
//}
if rsp.Data != "" {
fmt.Printf("-> data: %s\n", rsp.Data)
}
if rsp.Res.Log != "" {
fmt.Printf("-> log: %s\n", rsp.Res.Log)
}
if verbose {
fmt.Println("") fmt.Println("")
} }