main.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package main
  2. import (
  3. "bufio"
  4. "engine/common"
  5. "fmt"
  6. "github.com/go-yaml/yaml"
  7. "github.com/urfave/cli"
  8. "io/ioutil"
  9. "net/rpc"
  10. "os"
  11. "sort"
  12. "strings"
  13. "time"
  14. )
  15. type clientConf struct {
  16. Host string `yaml:"host"`
  17. Port int `yaml:"port"`
  18. }
  19. var clientYaml = "client.yaml"
  20. func streamProcess(client *rpc.Client, args string) {
  21. var reply string
  22. if args == ""{
  23. args = strings.Join(os.Args[1:], " ")
  24. }
  25. err := client.Call("Server.Stream", args, &reply)
  26. if err != nil{
  27. fmt.Println(err)
  28. }else{
  29. fmt.Println(reply)
  30. }
  31. }
  32. func main() {
  33. app := cli.NewApp()
  34. app.Version = "0.0.3"
  35. //nflag := []cli.Flag { cli.StringFlag{
  36. // Name: "name, n",
  37. // Usage: "the name of stream",
  38. // }}
  39. b := common.LoadConf(clientYaml)
  40. var cfg map[string]clientConf
  41. var config *clientConf
  42. if err := yaml.Unmarshal(b, &cfg); err != nil {
  43. fmt.Printf("Failed to load config file with error %s.\n", err)
  44. }else{
  45. c, ok := cfg["basic"]
  46. if !ok{
  47. fmt.Printf("No basic config in client.yaml, will use the default configuration.\n")
  48. }else{
  49. config = &c
  50. }
  51. }
  52. if config == nil {
  53. config = &clientConf{
  54. Host: "127.0.0.1",
  55. Port: 20498,
  56. }
  57. }
  58. fmt.Printf("Connecting to %s:%d... \n", config.Host, config.Port)
  59. // Create a TCP connection to localhost on port 1234
  60. client, err := rpc.DialHTTP("tcp", fmt.Sprintf("%s:%d", config.Host, config.Port))
  61. if err != nil {
  62. fmt.Printf("Failed to connect the server, please start the server.\n")
  63. return
  64. }
  65. app.Commands = []cli.Command{
  66. {
  67. Name: "query",
  68. Aliases: []string{"query"},
  69. Usage: "query command line",
  70. Action: func(c *cli.Context) error {
  71. reader := bufio.NewReader(os.Stdin)
  72. var inputs []string
  73. ticker := time.NewTicker(time.Millisecond * 300)
  74. defer ticker.Stop()
  75. for {
  76. fmt.Print("kuiper > ")
  77. text, _ := reader.ReadString('\n')
  78. inputs = append(inputs, text)
  79. // convert CRLF to LF
  80. text = strings.Replace(text, "\n", "", -1)
  81. if strings.ToLower(text) == "quit" || strings.ToLower(text) == "exit" {
  82. break
  83. } else if strings.Trim(text, " ") == "" {
  84. continue
  85. } else {
  86. var reply string
  87. err := client.Call("Server.CreateQuery", text, &reply)
  88. if err != nil{
  89. fmt.Println(err)
  90. return err
  91. } else {
  92. fmt.Println(reply)
  93. go func() {
  94. for {
  95. <-ticker.C
  96. var result string
  97. _ = client.Call("Server.GetQueryResult", "", &result)
  98. if result != "" {
  99. fmt.Println(result)
  100. }
  101. }
  102. }()
  103. }
  104. }
  105. }
  106. return nil
  107. },
  108. },
  109. {
  110. Name: "create",
  111. Aliases: []string{"create"},
  112. Usage: "create stream $stream_name | create stream $stream_name -f $stream_def_file | create rule $rule_name $rule_json | create rule $rule_name -f $rule_def_file",
  113. Subcommands: []cli.Command {
  114. {
  115. Name: "stream",
  116. Usage: "create stream $stream_name [-f stream_def_file]",
  117. Flags: []cli.Flag {
  118. cli.StringFlag{
  119. Name: "file, f",
  120. Usage: "the location of stream definition file",
  121. FilePath: "/home/mystream.txt",
  122. },
  123. },
  124. Action: func(c *cli.Context) error {
  125. sfile := c.String("file")
  126. if sfile != "" {
  127. if _, err := os.Stat(c.String("file")); os.IsNotExist(err) {
  128. fmt.Printf("The specified stream defintion file %s does not existed.\n", sfile)
  129. return nil
  130. }
  131. fmt.Printf("Creating a new stream from file %s.\n", sfile)
  132. if stream, err := ioutil.ReadFile(sfile); err != nil {
  133. fmt.Printf("Failed to read from stream definition file %s.\n", sfile)
  134. return nil
  135. } else {
  136. args := strings.Join([]string{"CREATE STREAM ", string(stream)}, " ")
  137. streamProcess(client, args)
  138. return nil
  139. }
  140. } else {
  141. streamProcess(client, "")
  142. return nil
  143. }
  144. },
  145. },
  146. {
  147. Name: "rule",
  148. Usage: "create rule $rule_name [$rule_json | -f rule_def_file]",
  149. Flags: []cli.Flag {
  150. cli.StringFlag{
  151. Name: "file, f",
  152. Usage: "the location of rule definition file",
  153. FilePath: "/home/myrule.txt",
  154. },
  155. },
  156. Action: func(c *cli.Context) error {
  157. sfile := c.String("file")
  158. if sfile != "" {
  159. if _, err := os.Stat(c.String("file")); os.IsNotExist(err) {
  160. fmt.Printf("The specified rule defenition file %s is not existed.\n", sfile)
  161. return nil
  162. }
  163. fmt.Printf("Creating a new rule from file %s.\n", sfile)
  164. if rule, err := ioutil.ReadFile(sfile); err != nil {
  165. fmt.Printf("Failed to read from rule definition file %s.\n", sfile)
  166. return nil
  167. } else {
  168. if len(c.Args()) != 1 {
  169. fmt.Printf("Expect rule name.\n")
  170. return nil
  171. }
  172. rname := c.Args()[0]
  173. var reply string
  174. args := &common.Rule{rname, string(rule)}
  175. err = client.Call("Server.CreateRule", args, &reply)
  176. if err != nil {
  177. fmt.Println(err)
  178. } else {
  179. fmt.Println(reply)
  180. }
  181. }
  182. return nil
  183. } else {
  184. if len(c.Args()) != 2 {
  185. fmt.Printf("Expect rule name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  186. return nil
  187. }
  188. rname := c.Args()[0]
  189. rjson := c.Args()[1]
  190. var reply string
  191. args := &common.Rule{rname, rjson}
  192. err = client.Call("Server.CreateRule", args, &reply)
  193. if err != nil {
  194. fmt.Println(err)
  195. } else {
  196. fmt.Println(reply)
  197. }
  198. return nil
  199. }
  200. },
  201. },
  202. },
  203. },
  204. {
  205. Name: "describe",
  206. Aliases: []string{"describe"},
  207. Usage: "describe stream $stream_name | describe rule $rule_name",
  208. Subcommands: []cli.Command{
  209. {
  210. Name: "stream",
  211. Usage: "describe stream $stream_name",
  212. //Flags: nflag,
  213. Action: func(c *cli.Context) error {
  214. streamProcess(client, "")
  215. return nil
  216. },
  217. },
  218. {
  219. Name: "rule",
  220. Usage: "describe rule $rule_name",
  221. Action: func(c *cli.Context) error {
  222. if len(c.Args()) != 1 {
  223. fmt.Printf("Expect rule name.\n")
  224. return nil
  225. }
  226. rname := c.Args()[0]
  227. var reply string
  228. err = client.Call("Server.DescRule", rname, &reply)
  229. if err != nil {
  230. fmt.Println(err)
  231. } else {
  232. fmt.Println(reply)
  233. }
  234. return nil
  235. },
  236. },
  237. },
  238. },
  239. {
  240. Name: "drop",
  241. Aliases: []string{"drop"},
  242. Usage: "drop stream $stream_name | drop rule $rule_name",
  243. Subcommands: []cli.Command{
  244. {
  245. Name: "stream",
  246. Usage: "drop stream $stream_name",
  247. //Flags: nflag,
  248. Action: func(c *cli.Context) error {
  249. streamProcess(client, "")
  250. return nil
  251. },
  252. },
  253. {
  254. Name: "rule",
  255. Usage: "drop rule $rule_name",
  256. //Flags: nflag,
  257. Action: func(c *cli.Context) error {
  258. if len(c.Args()) != 1 {
  259. fmt.Printf("Expect rule name.\n")
  260. return nil
  261. }
  262. rname := c.Args()[0]
  263. var reply string
  264. err = client.Call("Server.DropRule", rname, &reply)
  265. if err != nil {
  266. fmt.Println(err)
  267. } else {
  268. fmt.Println(reply)
  269. }
  270. return nil
  271. },
  272. },
  273. },
  274. },
  275. {
  276. Name: "show",
  277. Aliases: []string{"show"},
  278. Usage: "show streams | show rules",
  279. Subcommands: []cli.Command{
  280. {
  281. Name: "streams",
  282. Usage: "show streams",
  283. Action: func(c *cli.Context) error {
  284. streamProcess(client, "")
  285. return nil
  286. },
  287. },
  288. {
  289. Name: "rules",
  290. Usage: "show rules",
  291. Action: func(c *cli.Context) error {
  292. var reply string
  293. err = client.Call("Server.ShowRules", 0, &reply)
  294. if err != nil {
  295. fmt.Println(err)
  296. } else {
  297. fmt.Println(reply)
  298. }
  299. return nil
  300. },
  301. },
  302. },
  303. },
  304. {
  305. Name: "getstatus",
  306. Aliases: []string{"getstatus"},
  307. Usage: "getstatus rule $rule_name",
  308. Subcommands: []cli.Command{
  309. {
  310. Name: "rule",
  311. Usage: "getstatus rule $rule_name",
  312. //Flags: nflag,
  313. Action: func(c *cli.Context) error {
  314. if len(c.Args()) != 1 {
  315. fmt.Printf("Expect rule name.\n")
  316. return nil
  317. }
  318. rname := c.Args()[0]
  319. var reply string
  320. err = client.Call("Server.GetStatusRule", rname, &reply)
  321. if err != nil {
  322. fmt.Println(err)
  323. } else {
  324. fmt.Println(reply)
  325. }
  326. return nil
  327. },
  328. },
  329. },
  330. },
  331. {
  332. Name: "start",
  333. Aliases: []string{"start"},
  334. Usage: "start rule $rule_name",
  335. Subcommands: []cli.Command{
  336. {
  337. Name: "rule",
  338. Usage: "start rule $rule_name",
  339. //Flags: nflag,
  340. Action: func(c *cli.Context) error {
  341. if len(c.Args()) != 1 {
  342. fmt.Printf("Expect rule name.\n")
  343. return nil
  344. }
  345. rname := c.Args()[0]
  346. var reply string
  347. err = client.Call("Server.StartRule", rname, &reply)
  348. if err != nil {
  349. fmt.Println(err)
  350. } else {
  351. fmt.Println(reply)
  352. }
  353. return nil
  354. },
  355. },
  356. },
  357. },
  358. {
  359. Name: "stop",
  360. Aliases: []string{"stop"},
  361. Usage: "stop rule $rule_name",
  362. Subcommands: []cli.Command{
  363. {
  364. Name: "rule",
  365. Usage: "stop rule $rule_name",
  366. //Flags: nflag,
  367. Action: func(c *cli.Context) error {
  368. if len(c.Args()) != 1 {
  369. fmt.Printf("Expect rule name.\n")
  370. return nil
  371. }
  372. rname := c.Args()[0]
  373. var reply string
  374. err = client.Call("Server.StopRule", rname, &reply)
  375. if err != nil {
  376. fmt.Println(err)
  377. } else {
  378. fmt.Println(reply)
  379. }
  380. return nil
  381. },
  382. },
  383. },
  384. },
  385. {
  386. Name: "restart",
  387. Aliases: []string{"restart"},
  388. Usage: "restart rule $rule_name",
  389. Subcommands: []cli.Command{
  390. {
  391. Name: "rule",
  392. Usage: "restart rule $rule_name",
  393. //Flags: nflag,
  394. Action: func(c *cli.Context) error {
  395. if len(c.Args()) != 1 {
  396. fmt.Printf("Expect rule name.\n")
  397. return nil
  398. }
  399. rname := c.Args()[0]
  400. var reply string
  401. err = client.Call("Server.RestartRule", rname, &reply)
  402. if err != nil {
  403. fmt.Println(err)
  404. } else {
  405. fmt.Println(reply)
  406. }
  407. return nil
  408. },
  409. },
  410. },
  411. },
  412. }
  413. app.Name = "Kuiper"
  414. app.Usage = "The command line tool for EMQ X Kuiper."
  415. app.Action = func(c *cli.Context) error {
  416. cli.ShowSubcommandHelp(c)
  417. //cli.ShowVersion(c)
  418. return nil
  419. }
  420. sort.Sort(cli.FlagsByName(app.Flags))
  421. sort.Sort(cli.CommandsByName(app.Commands))
  422. err = app.Run(os.Args)
  423. if err != nil {
  424. fmt.Printf("%v", err)
  425. }
  426. }