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.

310 lines
7.1 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 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, err := stringOrHexToBytes(c.Args()[0])
  202. if err != nil {
  203. fmt.Println(err.Error())
  204. return nil
  205. }
  206. res := client.AppendTxSync(txBytes)
  207. printResponse(c, res, string(res.Data), true)
  208. return nil
  209. }
  210. // Validate a tx
  211. func cmdCheckTx(c *cli.Context) error {
  212. args := c.Args()
  213. if len(args) != 1 {
  214. return errors.New("Command check_tx takes 1 argument")
  215. }
  216. txBytes, err := stringOrHexToBytes(c.Args()[0])
  217. if err != nil {
  218. fmt.Println(err.Error())
  219. return nil
  220. }
  221. res := client.CheckTxSync(txBytes)
  222. printResponse(c, res, string(res.Data), true)
  223. return nil
  224. }
  225. // Get application Merkle root hash
  226. func cmdCommit(c *cli.Context) error {
  227. res := client.CommitSync()
  228. printResponse(c, res, Fmt("0x%X", res.Data), false)
  229. return nil
  230. }
  231. // Query application state
  232. func cmdQuery(c *cli.Context) error {
  233. args := c.Args()
  234. if len(args) != 1 {
  235. return errors.New("Command query takes 1 argument")
  236. }
  237. queryBytes, err := stringOrHexToBytes(c.Args()[0])
  238. if err != nil {
  239. fmt.Println(err.Error())
  240. return nil
  241. }
  242. res := client.QuerySync(queryBytes)
  243. printResponse(c, res, string(res.Data), true)
  244. return nil
  245. }
  246. //--------------------------------------------------------------------------------
  247. func printResponse(c *cli.Context, res types.Result, s string, printCode bool) {
  248. if c.GlobalBool("verbose") {
  249. fmt.Println(">", c.Command.Name, strings.Join(c.Args(), " "))
  250. }
  251. if printCode {
  252. fmt.Printf("-> code: %s\n", res.Code.String())
  253. }
  254. /*if res.Error != "" {
  255. fmt.Printf("-> error: %s\n", res.Error)
  256. }*/
  257. if s != "" {
  258. fmt.Printf("-> data: %s\n", s)
  259. }
  260. if res.Log != "" {
  261. fmt.Printf("-> log: %s\n", res.Log)
  262. }
  263. if c.GlobalBool("verbose") {
  264. fmt.Println("")
  265. }
  266. }
  267. // NOTE: s is interpreted as a string unless prefixed with 0x
  268. func stringOrHexToBytes(s string) ([]byte, error) {
  269. fmt.Printf("string: %s %x\n", s, []byte(s))
  270. if len(s) > 2 && strings.ToLower(s[:2]) == "0x" {
  271. b, err := hex.DecodeString(s[2:])
  272. if err != nil {
  273. err = fmt.Errorf("Error decoding hex argument: %s", err.Error())
  274. return nil, err
  275. }
  276. return b, nil
  277. }
  278. if !strings.HasPrefix(s, "\"") || !strings.HasSuffix(s, "\"") {
  279. err := fmt.Errorf("Invalid string arg: \"%s\". Must be quoted or a \"0x\"-prefixed hex string", s)
  280. return nil, err
  281. }
  282. return []byte(s[1 : len(s)-1]), nil
  283. }