You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
5.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strings"
  9. "github.com/codegangsta/cli"
  10. . "github.com/tendermint/go-common"
  11. "github.com/tendermint/go-wire/expr"
  12. "github.com/tendermint/tmsp/client"
  13. "github.com/tendermint/tmsp/types"
  14. )
  15. // clientection is a global variable so it can be reused by the console
  16. var client tmspcli.Client
  17. func main() {
  18. app := cli.NewApp()
  19. app.Name = "tmsp-cli"
  20. app.Usage = "tmsp-cli [command] [args...]"
  21. app.Flags = []cli.Flag{
  22. cli.StringFlag{
  23. Name: "address",
  24. Value: "tcp://127.0.0.1:46658",
  25. Usage: "address of application socket",
  26. },
  27. }
  28. app.Commands = []cli.Command{
  29. {
  30. Name: "batch",
  31. Usage: "Run a batch of tmsp commands against an application",
  32. Action: func(c *cli.Context) error {
  33. return cmdBatch(app, c)
  34. },
  35. },
  36. {
  37. Name: "console",
  38. Usage: "Start an interactive tmsp console for multiple commands",
  39. Action: func(c *cli.Context) error {
  40. return cmdConsole(app, c)
  41. },
  42. },
  43. {
  44. Name: "echo",
  45. Usage: "Have the application echo a message",
  46. Action: func(c *cli.Context) error {
  47. return cmdEcho(c)
  48. },
  49. },
  50. {
  51. Name: "info",
  52. Usage: "Get some info about the application",
  53. Action: func(c *cli.Context) error {
  54. return cmdInfo(c)
  55. },
  56. },
  57. {
  58. Name: "set_option",
  59. Usage: "Set an option on the application",
  60. Action: func(c *cli.Context) error {
  61. return cmdSetOption(c)
  62. },
  63. },
  64. {
  65. Name: "append_tx",
  66. Usage: "Append a new tx to application",
  67. Action: func(c *cli.Context) error {
  68. return cmdAppendTx(c)
  69. },
  70. },
  71. {
  72. Name: "check_tx",
  73. Usage: "Validate a tx",
  74. Action: func(c *cli.Context) error {
  75. return cmdCheckTx(c)
  76. },
  77. },
  78. {
  79. Name: "commit",
  80. Usage: "Commit the application state and return the Merkle root hash",
  81. Action: func(c *cli.Context) error {
  82. return cmdCommit(c)
  83. },
  84. },
  85. {
  86. Name: "query",
  87. Usage: "Query application state",
  88. Action: func(c *cli.Context) error {
  89. return cmdQuery(c)
  90. },
  91. },
  92. }
  93. app.Before = before
  94. err := app.Run(os.Args)
  95. if err != nil {
  96. Exit(err.Error())
  97. }
  98. }
  99. func before(c *cli.Context) error {
  100. if client == nil {
  101. var err error
  102. client, err = tmspcli.NewClient(c.GlobalString("address"), false)
  103. if err != nil {
  104. Exit(err.Error())
  105. }
  106. }
  107. return nil
  108. }
  109. //--------------------------------------------------------------------------------
  110. func cmdBatch(app *cli.App, c *cli.Context) error {
  111. bufReader := bufio.NewReader(os.Stdin)
  112. for {
  113. line, more, err := bufReader.ReadLine()
  114. if more {
  115. return errors.New("Input line is too long")
  116. } else if err == io.EOF {
  117. break
  118. } else if len(line) == 0 {
  119. continue
  120. } else if err != nil {
  121. return err
  122. }
  123. args := []string{"tmsp"}
  124. args = append(args, strings.Split(string(line), " ")...)
  125. app.Run(args)
  126. }
  127. return nil
  128. }
  129. func cmdConsole(app *cli.App, c *cli.Context) error {
  130. for {
  131. fmt.Printf("\n> ")
  132. bufReader := bufio.NewReader(os.Stdin)
  133. line, more, err := bufReader.ReadLine()
  134. if more {
  135. return errors.New("Input is too long")
  136. } else if err != nil {
  137. return err
  138. }
  139. args := []string{"tmsp"}
  140. args = append(args, strings.Split(string(line), " ")...)
  141. if err := app.Run(args); err != nil {
  142. return err
  143. }
  144. }
  145. return nil
  146. }
  147. // Have the application echo a message
  148. func cmdEcho(c *cli.Context) error {
  149. args := c.Args()
  150. if len(args) != 1 {
  151. return errors.New("Command echo takes 1 argument")
  152. }
  153. res := client.EchoSync(args[0])
  154. printResponse(res, string(res.Data), false)
  155. return nil
  156. }
  157. // Get some info from the application
  158. func cmdInfo(c *cli.Context) error {
  159. res := client.InfoSync()
  160. printResponse(res, string(res.Data), false)
  161. return nil
  162. }
  163. // Set an option on the application
  164. func cmdSetOption(c *cli.Context) error {
  165. args := c.Args()
  166. if len(args) != 2 {
  167. return errors.New("Command set_option takes 2 arguments (key, value)")
  168. }
  169. res := client.SetOptionSync(args[0], args[1])
  170. printResponse(res, Fmt("%s=%s", args[0], args[1]), false)
  171. return nil
  172. }
  173. // Append a new tx to application
  174. func cmdAppendTx(c *cli.Context) error {
  175. args := c.Args()
  176. if len(args) != 1 {
  177. return errors.New("Command append_tx takes 1 argument")
  178. }
  179. txExprString := c.Args()[0]
  180. txBytes, err := expr.Compile(txExprString)
  181. if err != nil {
  182. return err
  183. }
  184. res := client.AppendTxSync(txBytes)
  185. printResponse(res, string(res.Data), true)
  186. return nil
  187. }
  188. // Validate a tx
  189. func cmdCheckTx(c *cli.Context) error {
  190. args := c.Args()
  191. if len(args) != 1 {
  192. return errors.New("Command check_tx takes 1 argument")
  193. }
  194. txExprString := c.Args()[0]
  195. txBytes, err := expr.Compile(txExprString)
  196. if err != nil {
  197. return err
  198. }
  199. res := client.CheckTxSync(txBytes)
  200. printResponse(res, string(res.Data), true)
  201. return nil
  202. }
  203. // Get application Merkle root hash
  204. func cmdCommit(c *cli.Context) error {
  205. res := client.CommitSync()
  206. printResponse(res, Fmt("%X", res.Data), false)
  207. return nil
  208. }
  209. // Query application state
  210. func cmdQuery(c *cli.Context) error {
  211. args := c.Args()
  212. if len(args) != 1 {
  213. return errors.New("Command query takes 1 argument")
  214. }
  215. queryExprString := args[0]
  216. queryBytes, err := expr.Compile(queryExprString)
  217. if err != nil {
  218. return err
  219. }
  220. res := client.QuerySync(queryBytes)
  221. printResponse(res, string(res.Data), true)
  222. return nil
  223. }
  224. //--------------------------------------------------------------------------------
  225. func printResponse(res types.Result, s string, printCode bool) {
  226. if printCode {
  227. fmt.Printf("-> code: %s\n", res.Code.String())
  228. }
  229. /*if res.Error != "" {
  230. fmt.Printf("-> error: %s\n", res.Error)
  231. }*/
  232. if s != "" {
  233. fmt.Printf("-> data: {%s}\n", s)
  234. }
  235. if res.Log != "" {
  236. fmt.Printf("-> log: %s\n", res.Log)
  237. }
  238. }