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.

289 lines
6.6 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
8 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. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "os"
  9. "strings"
  10. . "github.com/tendermint/go-common"
  11. "github.com/tendermint/tmsp/client"
  12. "github.com/tendermint/tmsp/types"
  13. "github.com/urfave/cli"
  14. )
  15. // client 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.Version = "0.2.1" // better error handling in console
  22. app.Flags = []cli.Flag{
  23. cli.StringFlag{
  24. Name: "address",
  25. Value: "tcp://127.0.0.1:46658",
  26. Usage: "address of application socket",
  27. },
  28. cli.StringFlag{
  29. Name: "tmsp",
  30. Value: "socket",
  31. Usage: "socket or grpc",
  32. },
  33. cli.BoolFlag{
  34. Name: "verbose",
  35. Usage: "print the command and results as if it were a console session",
  36. },
  37. }
  38. app.Commands = []cli.Command{
  39. {
  40. Name: "batch",
  41. Usage: "Run a batch of tmsp commands against an application",
  42. Action: func(c *cli.Context) error {
  43. return cmdBatch(app, c)
  44. },
  45. },
  46. {
  47. Name: "console",
  48. Usage: "Start an interactive tmsp console for multiple commands",
  49. Action: func(c *cli.Context) error {
  50. return cmdConsole(app, c)
  51. },
  52. },
  53. {
  54. Name: "echo",
  55. Usage: "Have the application echo a message",
  56. Action: func(c *cli.Context) error {
  57. return cmdEcho(c)
  58. },
  59. },
  60. {
  61. Name: "info",
  62. Usage: "Get some info about the application",
  63. Action: func(c *cli.Context) error {
  64. return cmdInfo(c)
  65. },
  66. },
  67. {
  68. Name: "set_option",
  69. Usage: "Set an option on the application",
  70. Action: func(c *cli.Context) error {
  71. return cmdSetOption(c)
  72. },
  73. },
  74. {
  75. Name: "append_tx",
  76. Usage: "Append a new tx to application",
  77. Action: func(c *cli.Context) error {
  78. return cmdAppendTx(c)
  79. },
  80. },
  81. {
  82. Name: "check_tx",
  83. Usage: "Validate a tx",
  84. Action: func(c *cli.Context) error {
  85. return cmdCheckTx(c)
  86. },
  87. },
  88. {
  89. Name: "commit",
  90. Usage: "Commit the application state and return the Merkle root hash",
  91. Action: func(c *cli.Context) error {
  92. return cmdCommit(c)
  93. },
  94. },
  95. {
  96. Name: "query",
  97. Usage: "Query application state",
  98. Action: func(c *cli.Context) error {
  99. return cmdQuery(c)
  100. },
  101. },
  102. }
  103. app.Before = before
  104. err := app.Run(os.Args)
  105. if err != nil {
  106. Exit(err.Error())
  107. }
  108. }
  109. func before(c *cli.Context) error {
  110. if client == nil {
  111. var err error
  112. client, err = tmspcli.NewClient(c.GlobalString("address"), c.GlobalString("tmsp"), false)
  113. if err != nil {
  114. Exit(err.Error())
  115. }
  116. }
  117. return nil
  118. }
  119. // badCmd is called when we invoke with an invalid first argument (just for console for now)
  120. func badCmd(c *cli.Context, cmd string) {
  121. fmt.Println("Unknown command:", cmd)
  122. fmt.Println("Please try one of the following:")
  123. fmt.Println("")
  124. cli.DefaultAppComplete(c)
  125. }
  126. //--------------------------------------------------------------------------------
  127. func cmdBatch(app *cli.App, c *cli.Context) error {
  128. bufReader := bufio.NewReader(os.Stdin)
  129. for {
  130. line, more, err := bufReader.ReadLine()
  131. if more {
  132. return errors.New("Input line is too long")
  133. } else if err == io.EOF {
  134. break
  135. } else if len(line) == 0 {
  136. continue
  137. } else if err != nil {
  138. return err
  139. }
  140. args := []string{"tmsp-cli"}
  141. if c.GlobalBool("verbose") {
  142. args = append(args, "--verbose")
  143. }
  144. args = append(args, strings.Split(string(line), " ")...)
  145. app.Run(args)
  146. }
  147. return nil
  148. }
  149. func cmdConsole(app *cli.App, c *cli.Context) error {
  150. // don't hard exit on mistyped commands (eg. check vs check_tx)
  151. app.CommandNotFound = badCmd
  152. for {
  153. fmt.Printf("\n> ")
  154. bufReader := bufio.NewReader(os.Stdin)
  155. line, more, err := bufReader.ReadLine()
  156. if more {
  157. return errors.New("Input is too long")
  158. } else if err != nil {
  159. return err
  160. }
  161. args := []string{"tmsp-cli"}
  162. args = append(args, strings.Split(string(line), " ")...)
  163. if err := app.Run(args); err != nil {
  164. // if the command doesn't succeed, inform the user without exiting
  165. fmt.Println("Error:", err.Error())
  166. }
  167. }
  168. }
  169. // Have the application echo a message
  170. func cmdEcho(c *cli.Context) error {
  171. args := c.Args()
  172. if len(args) != 1 {
  173. return errors.New("Command echo takes 1 argument")
  174. }
  175. res := client.EchoSync(args[0])
  176. printResponse(c, res, string(res.Data), false)
  177. return nil
  178. }
  179. // Get some info from the application
  180. func cmdInfo(c *cli.Context) error {
  181. res, _, _, _ := client.InfoSync()
  182. printResponse(c, res, string(res.Data), false)
  183. return nil
  184. }
  185. // Set an option on the application
  186. func cmdSetOption(c *cli.Context) error {
  187. args := c.Args()
  188. if len(args) != 2 {
  189. return errors.New("Command set_option takes 2 arguments (key, value)")
  190. }
  191. res := client.SetOptionSync(args[0], args[1])
  192. printResponse(c, res, Fmt("%s=%s", args[0], args[1]), false)
  193. return nil
  194. }
  195. // Append a new tx to application
  196. func cmdAppendTx(c *cli.Context) error {
  197. args := c.Args()
  198. if len(args) != 1 {
  199. return errors.New("Command append_tx takes 1 argument")
  200. }
  201. txBytes := stringOrHexToBytes(c.Args()[0])
  202. res := client.AppendTxSync(txBytes)
  203. printResponse(c, res, string(res.Data), true)
  204. return nil
  205. }
  206. // Validate a tx
  207. func cmdCheckTx(c *cli.Context) error {
  208. args := c.Args()
  209. if len(args) != 1 {
  210. return errors.New("Command check_tx takes 1 argument")
  211. }
  212. txBytes := stringOrHexToBytes(c.Args()[0])
  213. res := client.CheckTxSync(txBytes)
  214. printResponse(c, res, string(res.Data), true)
  215. return nil
  216. }
  217. // Get application Merkle root hash
  218. func cmdCommit(c *cli.Context) error {
  219. res := client.CommitSync()
  220. printResponse(c, res, Fmt("%X", res.Data), false)
  221. return nil
  222. }
  223. // Query application state
  224. func cmdQuery(c *cli.Context) error {
  225. args := c.Args()
  226. if len(args) != 1 {
  227. return errors.New("Command query takes 1 argument")
  228. }
  229. queryBytes := stringOrHexToBytes(c.Args()[0])
  230. res := client.QuerySync(queryBytes)
  231. printResponse(c, res, string(res.Data), true)
  232. return nil
  233. }
  234. //--------------------------------------------------------------------------------
  235. func printResponse(c *cli.Context, res types.Result, s string, printCode bool) {
  236. if c.GlobalBool("verbose") {
  237. fmt.Println(">", c.Command.Name, strings.Join(c.Args(), " "))
  238. }
  239. if printCode {
  240. fmt.Printf("-> code: %s\n", res.Code.String())
  241. }
  242. /*if res.Error != "" {
  243. fmt.Printf("-> error: %s\n", res.Error)
  244. }*/
  245. if s != "" {
  246. fmt.Printf("-> data: {%s}\n", s)
  247. }
  248. if res.Log != "" {
  249. fmt.Printf("-> log: %s\n", res.Log)
  250. }
  251. if c.GlobalBool("verbose") {
  252. fmt.Println("")
  253. }
  254. }
  255. // NOTE: s is interpreted as a string unless prefixed with 0x
  256. func stringOrHexToBytes(s string) []byte {
  257. if len(s) > 2 && s[:2] == "0x" {
  258. b, err := hex.DecodeString(s[2:])
  259. if err != nil {
  260. fmt.Println("Error decoding hex argument:", err.Error())
  261. }
  262. return b
  263. }
  264. return []byte(s)
  265. }