main.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bufio"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/internal/pkg/model"
  20. "github.com/urfave/cli"
  21. "io/ioutil"
  22. "net/rpc"
  23. "os"
  24. "sort"
  25. "strings"
  26. "time"
  27. )
  28. type clientConf struct {
  29. Host string `yaml:"host"`
  30. Port int `yaml:"port"`
  31. }
  32. const ClientYaml = "client.yaml"
  33. func streamProcess(client *rpc.Client, args string) {
  34. var reply string
  35. if args == "" {
  36. args = strings.Join(os.Args[1:], " ")
  37. }
  38. err := client.Call("Server.Stream", args, &reply)
  39. if err != nil {
  40. fmt.Println(err)
  41. } else {
  42. fmt.Println(reply)
  43. }
  44. }
  45. var (
  46. Version = "unknown"
  47. LoadFileType = "relative"
  48. )
  49. func main() {
  50. conf.LoadFileType = LoadFileType
  51. app := cli.NewApp()
  52. app.Version = Version
  53. //nflag := []cli.Flag { cli.StringFlag{
  54. // Name: "name, n",
  55. // Usage: "the name of stream",
  56. // }}
  57. var cfg map[string]clientConf
  58. err := conf.LoadConfigByName(ClientYaml, &cfg)
  59. if err != nil {
  60. conf.Log.Fatal(err)
  61. fmt.Printf("Failed to load config file with error %s.\n", err)
  62. }
  63. var config *clientConf
  64. c, ok := cfg["basic"]
  65. if !ok {
  66. fmt.Printf("No basic config in client.yaml, will use the default configuration.\n")
  67. } else {
  68. config = &c
  69. }
  70. if config == nil {
  71. config = &clientConf{
  72. Host: "127.0.0.1",
  73. Port: 20498,
  74. }
  75. }
  76. fmt.Printf("Connecting to %s:%d... \n", config.Host, config.Port)
  77. // Create a TCP connection to localhost on port 1234
  78. client, err := rpc.DialHTTP("tcp", fmt.Sprintf("%s:%d", config.Host, config.Port))
  79. if err != nil {
  80. fmt.Printf("Failed to connect the server, please start the server.\n")
  81. return
  82. }
  83. app.Commands = []cli.Command{
  84. {
  85. Name: "query",
  86. Aliases: []string{"query"},
  87. Usage: "query command line",
  88. Action: func(c *cli.Context) error {
  89. reader := bufio.NewReader(os.Stdin)
  90. var inputs []string
  91. ticker := time.NewTicker(time.Millisecond * 300)
  92. defer ticker.Stop()
  93. for {
  94. fmt.Print("kuiper > ")
  95. text, _ := reader.ReadString('\n')
  96. inputs = append(inputs, text)
  97. // convert CRLF to LF
  98. text = strings.Replace(text, "\n", "", -1)
  99. if strings.ToLower(text) == "quit" || strings.ToLower(text) == "exit" {
  100. break
  101. } else if strings.Trim(text, " ") == "" {
  102. continue
  103. } else {
  104. var reply string
  105. err := client.Call("Server.CreateQuery", text, &reply)
  106. if err != nil {
  107. fmt.Println(err)
  108. continue
  109. } else {
  110. fmt.Println(reply)
  111. go func() {
  112. for {
  113. <-ticker.C
  114. var result string
  115. e := client.Call("Server.GetQueryResult", "", &result)
  116. if e != nil {
  117. fmt.Println(e)
  118. fmt.Print("kuiper > ")
  119. return
  120. }
  121. if result != "" {
  122. fmt.Println(result)
  123. }
  124. }
  125. }()
  126. }
  127. }
  128. }
  129. return nil
  130. },
  131. },
  132. {
  133. Name: "create",
  134. Aliases: []string{"create"},
  135. Usage: "create stream $stream_name | create stream $stream_name -f $stream_def_file | create table $table_name | create table $table_name -f $table_def_file| create rule $rule_name $rule_json | create rule $rule_name -f $rule_def_file | create plugin $plugin_type $plugin_name $plugin_json | create plugin $plugin_type $plugin_name -f $plugin_def_file | create service $service_name $service_json ",
  136. Subcommands: []cli.Command{
  137. {
  138. Name: "stream",
  139. Usage: "create stream $stream_name [-f stream_def_file]",
  140. Flags: []cli.Flag{
  141. cli.StringFlag{
  142. Name: "file, f",
  143. Usage: "the location of stream definition file",
  144. FilePath: "/home/mystream.txt",
  145. },
  146. },
  147. Action: func(c *cli.Context) error {
  148. sfile := c.String("file")
  149. if sfile != "" {
  150. if stream, err := readDef(sfile, "stream"); err != nil {
  151. fmt.Printf("%s", err)
  152. return nil
  153. } else {
  154. args := strings.Join([]string{"CREATE STREAM ", string(stream)}, " ")
  155. streamProcess(client, args)
  156. return nil
  157. }
  158. } else {
  159. streamProcess(client, "")
  160. return nil
  161. }
  162. },
  163. },
  164. {
  165. Name: "table",
  166. Usage: "create table $table_name [-f table_def_file]",
  167. Flags: []cli.Flag{
  168. cli.StringFlag{
  169. Name: "file, f",
  170. Usage: "the location of table definition file",
  171. FilePath: "/home/mytable.txt",
  172. },
  173. },
  174. Action: func(c *cli.Context) error {
  175. sfile := c.String("file")
  176. if sfile != "" {
  177. if stream, err := readDef(sfile, "table"); err != nil {
  178. fmt.Printf("%s", err)
  179. return nil
  180. } else {
  181. args := strings.Join([]string{"CREATE TABLE ", string(stream)}, " ")
  182. streamProcess(client, args)
  183. return nil
  184. }
  185. } else {
  186. streamProcess(client, "")
  187. return nil
  188. }
  189. },
  190. },
  191. {
  192. Name: "rule",
  193. Usage: "create rule $rule_name [$rule_json | -f rule_def_file]",
  194. Flags: []cli.Flag{
  195. cli.StringFlag{
  196. Name: "file, f",
  197. Usage: "the location of rule definition file",
  198. FilePath: "/home/myrule.txt",
  199. },
  200. },
  201. Action: func(c *cli.Context) error {
  202. sfile := c.String("file")
  203. if sfile != "" {
  204. if rule, err := readDef(sfile, "rule"); err != nil {
  205. fmt.Printf("%s", err)
  206. return nil
  207. } else {
  208. if len(c.Args()) != 1 {
  209. fmt.Printf("Expect rule name.\n")
  210. return nil
  211. }
  212. rname := c.Args()[0]
  213. var reply string
  214. args := &model.RPCArgDesc{Name: rname, Json: string(rule)}
  215. err = client.Call("Server.CreateRule", args, &reply)
  216. if err != nil {
  217. fmt.Println(err)
  218. } else {
  219. fmt.Println(reply)
  220. }
  221. }
  222. return nil
  223. } else {
  224. if len(c.Args()) != 2 {
  225. fmt.Printf("Expect rule name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  226. return nil
  227. }
  228. rname := c.Args()[0]
  229. rjson := c.Args()[1]
  230. var reply string
  231. args := &model.RPCArgDesc{Name: rname, Json: rjson}
  232. err = client.Call("Server.CreateRule", args, &reply)
  233. if err != nil {
  234. fmt.Println(err)
  235. } else {
  236. fmt.Println(reply)
  237. }
  238. return nil
  239. }
  240. },
  241. },
  242. {
  243. Name: "plugin",
  244. Usage: "create plugin $plugin_type $plugin_name [$plugin_json | -f plugin_def_file]",
  245. Flags: []cli.Flag{
  246. cli.StringFlag{
  247. Name: "file, f",
  248. Usage: "the location of plugin definition file",
  249. FilePath: "/home/myplugin.txt",
  250. },
  251. },
  252. Action: func(c *cli.Context) error {
  253. if len(c.Args()) < 2 {
  254. fmt.Printf("Expect plugin type and name.\n")
  255. return nil
  256. }
  257. ptype, err := getPluginType(c.Args()[0])
  258. if err != nil {
  259. fmt.Printf("%s\n", err)
  260. return nil
  261. }
  262. pname := c.Args()[1]
  263. sfile := c.String("file")
  264. args := &model.PluginDesc{
  265. RPCArgDesc: model.RPCArgDesc{
  266. Name: pname,
  267. },
  268. Type: ptype,
  269. }
  270. if sfile != "" {
  271. if len(c.Args()) != 2 {
  272. fmt.Printf("Expect plugin type, name.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  273. return nil
  274. }
  275. if p, err := readDef(sfile, "plugin"); err != nil {
  276. fmt.Printf("%s", err)
  277. return nil
  278. } else {
  279. args.Json = string(p)
  280. }
  281. } else {
  282. if len(c.Args()) != 3 {
  283. fmt.Printf("Expect plugin type, name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  284. return nil
  285. }
  286. args.Json = c.Args()[2]
  287. }
  288. var reply string
  289. err = client.Call("Server.CreatePlugin", args, &reply)
  290. if err != nil {
  291. fmt.Println(err)
  292. } else {
  293. fmt.Println(reply)
  294. }
  295. return nil
  296. },
  297. },
  298. {
  299. Name: "service",
  300. Usage: "create service $service_name $service_json",
  301. Action: func(c *cli.Context) error {
  302. if len(c.Args()) < 2 {
  303. fmt.Printf("Expect service name and json.\n")
  304. return nil
  305. }
  306. var reply string
  307. err = client.Call("Server.CreateService", &model.RPCArgDesc{
  308. Name: c.Args()[0],
  309. Json: c.Args()[1],
  310. }, &reply)
  311. if err != nil {
  312. fmt.Println(err)
  313. } else {
  314. fmt.Println(reply)
  315. }
  316. return nil
  317. },
  318. },
  319. },
  320. },
  321. {
  322. Name: "describe",
  323. Aliases: []string{"describe"},
  324. Usage: "describe stream $stream_name | describe table $table_name | describe rule $rule_name | describe plugin $plugin_type $plugin_name | describe udf $udf_name | describe service $service_name | describe service_func $service_func_name",
  325. Subcommands: []cli.Command{
  326. {
  327. Name: "stream",
  328. Usage: "describe stream $stream_name",
  329. //Flags: nflag,
  330. Action: func(c *cli.Context) error {
  331. streamProcess(client, "")
  332. return nil
  333. },
  334. },
  335. {
  336. Name: "table",
  337. Usage: "describe table $table_name",
  338. //Flags: nflag,
  339. Action: func(c *cli.Context) error {
  340. streamProcess(client, "")
  341. return nil
  342. },
  343. },
  344. {
  345. Name: "rule",
  346. Usage: "describe rule $rule_name",
  347. Action: func(c *cli.Context) error {
  348. if len(c.Args()) != 1 {
  349. fmt.Printf("Expect rule name.\n")
  350. return nil
  351. }
  352. rname := c.Args()[0]
  353. var reply string
  354. err = client.Call("Server.DescRule", rname, &reply)
  355. if err != nil {
  356. fmt.Println(err)
  357. } else {
  358. fmt.Println(reply)
  359. }
  360. return nil
  361. },
  362. },
  363. {
  364. Name: "plugin",
  365. Usage: "describe plugin $plugin_type $plugin_name",
  366. //Flags: nflag,
  367. Action: func(c *cli.Context) error {
  368. ptype, err := getPluginType(c.Args()[0])
  369. if err != nil {
  370. fmt.Printf("%s\n", err)
  371. return nil
  372. }
  373. if len(c.Args()) != 2 {
  374. fmt.Printf("Expect plugin name.\n")
  375. return nil
  376. }
  377. pname := c.Args()[1]
  378. args := &model.PluginDesc{
  379. RPCArgDesc: model.RPCArgDesc{
  380. Name: pname,
  381. },
  382. Type: ptype,
  383. }
  384. var reply string
  385. err = client.Call("Server.DescPlugin", args, &reply)
  386. if err != nil {
  387. fmt.Println(err)
  388. } else {
  389. fmt.Println(reply)
  390. }
  391. return nil
  392. },
  393. },
  394. {
  395. Name: "udf",
  396. Usage: "describe udf $udf_name",
  397. //Flags: nflag,
  398. Action: func(c *cli.Context) error {
  399. if len(c.Args()) != 1 {
  400. fmt.Printf("Expect udf name.\n")
  401. return nil
  402. }
  403. pname := c.Args()[0]
  404. var reply string
  405. err = client.Call("Server.DescUdf", pname, &reply)
  406. if err != nil {
  407. fmt.Println(err)
  408. } else {
  409. fmt.Println(reply)
  410. }
  411. return nil
  412. },
  413. },
  414. {
  415. Name: "service",
  416. Usage: "describe service $service_name",
  417. Action: func(c *cli.Context) error {
  418. if len(c.Args()) != 1 {
  419. fmt.Printf("Expect service name.\n")
  420. return nil
  421. }
  422. name := c.Args()[0]
  423. var reply string
  424. err = client.Call("Server.DescService", name, &reply)
  425. if err != nil {
  426. fmt.Println(err)
  427. } else {
  428. fmt.Println(reply)
  429. }
  430. return nil
  431. },
  432. },
  433. {
  434. Name: "service_func",
  435. Usage: "describe service_func $service_func_name",
  436. Action: func(c *cli.Context) error {
  437. if len(c.Args()) != 1 {
  438. fmt.Printf("Expect service func name.\n")
  439. return nil
  440. }
  441. name := c.Args()[0]
  442. var reply string
  443. err = client.Call("Server.DescServiceFunc", name, &reply)
  444. if err != nil {
  445. fmt.Println(err)
  446. } else {
  447. fmt.Println(reply)
  448. }
  449. return nil
  450. },
  451. },
  452. },
  453. },
  454. {
  455. Name: "drop",
  456. Aliases: []string{"drop"},
  457. Usage: "drop stream $stream_name | drop table $table_name |drop rule $rule_name | drop plugin $plugin_type $plugin_name -r $stop | drop service $service_name",
  458. Subcommands: []cli.Command{
  459. {
  460. Name: "stream",
  461. Usage: "drop stream $stream_name",
  462. //Flags: nflag,
  463. Action: func(c *cli.Context) error {
  464. streamProcess(client, "")
  465. return nil
  466. },
  467. },
  468. {
  469. Name: "table",
  470. Usage: "drop table $table_name",
  471. //Flags: nflag,
  472. Action: func(c *cli.Context) error {
  473. streamProcess(client, "")
  474. return nil
  475. },
  476. },
  477. {
  478. Name: "rule",
  479. Usage: "drop rule $rule_name",
  480. //Flags: nflag,
  481. Action: func(c *cli.Context) error {
  482. if len(c.Args()) != 1 {
  483. fmt.Printf("Expect rule name.\n")
  484. return nil
  485. }
  486. rname := c.Args()[0]
  487. var reply string
  488. err = client.Call("Server.DropRule", rname, &reply)
  489. if err != nil {
  490. fmt.Println(err)
  491. } else {
  492. fmt.Println(reply)
  493. }
  494. return nil
  495. },
  496. },
  497. {
  498. Name: "plugin",
  499. Usage: "drop plugin $plugin_type $plugin_name -s stop",
  500. Flags: []cli.Flag{
  501. cli.StringFlag{
  502. Name: "stop, s",
  503. Usage: "stop kuiper after the action",
  504. },
  505. },
  506. Action: func(c *cli.Context) error {
  507. r := c.String("stop")
  508. if r != "true" && r != "false" {
  509. fmt.Printf("Expect s flag to be a boolean value.\n")
  510. return nil
  511. }
  512. if len(c.Args()) < 2 || len(c.Args()) > 3 {
  513. fmt.Printf("Expect plugin type and name.\n")
  514. return nil
  515. }
  516. ptype, err := getPluginType(c.Args()[0])
  517. if err != nil {
  518. fmt.Printf("%s\n", err)
  519. return nil
  520. }
  521. pname := c.Args()[1]
  522. args := &model.PluginDesc{
  523. RPCArgDesc: model.RPCArgDesc{
  524. Name: pname,
  525. },
  526. Type: ptype,
  527. Stop: r == "true",
  528. }
  529. var reply string
  530. err = client.Call("Server.DropPlugin", args, &reply)
  531. if err != nil {
  532. fmt.Println(err)
  533. } else {
  534. fmt.Println(reply)
  535. }
  536. return nil
  537. },
  538. },
  539. {
  540. Name: "service",
  541. Usage: "drop service $service_name",
  542. Action: func(c *cli.Context) error {
  543. if len(c.Args()) != 1 {
  544. fmt.Printf("Expect service name.\n")
  545. return nil
  546. }
  547. name := c.Args()[0]
  548. var reply string
  549. err = client.Call("Server.DropService", name, &reply)
  550. if err != nil {
  551. fmt.Println(err)
  552. } else {
  553. fmt.Println(reply)
  554. }
  555. return nil
  556. },
  557. },
  558. },
  559. },
  560. {
  561. Name: "show",
  562. Aliases: []string{"show"},
  563. Usage: "show streams | show tables | show rules | show plugins $plugin_type | show services | show service_funcs",
  564. Subcommands: []cli.Command{
  565. {
  566. Name: "streams",
  567. Usage: "show streams",
  568. Action: func(c *cli.Context) error {
  569. streamProcess(client, "")
  570. return nil
  571. },
  572. },
  573. {
  574. Name: "tables",
  575. Usage: "show tables",
  576. Action: func(c *cli.Context) error {
  577. streamProcess(client, "")
  578. return nil
  579. },
  580. },
  581. {
  582. Name: "rules",
  583. Usage: "show rules",
  584. Action: func(c *cli.Context) error {
  585. var reply string
  586. err = client.Call("Server.ShowRules", 0, &reply)
  587. if err != nil {
  588. fmt.Println(err)
  589. } else {
  590. fmt.Println(reply)
  591. }
  592. return nil
  593. },
  594. },
  595. {
  596. Name: "plugins",
  597. Usage: "show plugins $plugin_type",
  598. Action: func(c *cli.Context) error {
  599. if len(c.Args()) != 1 {
  600. fmt.Printf("Expect plugin type.\n")
  601. return nil
  602. }
  603. ptype, err := getPluginType(c.Args()[0])
  604. if err != nil {
  605. fmt.Printf("%s\n", err)
  606. return nil
  607. }
  608. var reply string
  609. err = client.Call("Server.ShowPlugins", ptype, &reply)
  610. if err != nil {
  611. fmt.Println(err)
  612. } else {
  613. fmt.Println(reply)
  614. }
  615. return nil
  616. },
  617. },
  618. {
  619. Name: "udfs",
  620. Usage: "show udfs",
  621. Action: func(c *cli.Context) error {
  622. var reply string
  623. err = client.Call("Server.ShowUdfs", 0, &reply)
  624. if err != nil {
  625. fmt.Println(err)
  626. } else {
  627. fmt.Println(reply)
  628. }
  629. return nil
  630. },
  631. }, {
  632. Name: "services",
  633. Usage: "show services",
  634. Action: func(c *cli.Context) error {
  635. var reply string
  636. err = client.Call("Server.ShowServices", 0, &reply)
  637. if err != nil {
  638. fmt.Println(err)
  639. } else {
  640. fmt.Println(reply)
  641. }
  642. return nil
  643. },
  644. }, {
  645. Name: "service_funcs",
  646. Usage: "show service_funcs",
  647. Action: func(c *cli.Context) error {
  648. var reply string
  649. err = client.Call("Server.ShowServiceFuncs", 0, &reply)
  650. if err != nil {
  651. fmt.Println(err)
  652. } else {
  653. fmt.Println(reply)
  654. }
  655. return nil
  656. },
  657. },
  658. },
  659. },
  660. {
  661. Name: "getstatus",
  662. Aliases: []string{"getstatus"},
  663. Usage: "getstatus rule $rule_name",
  664. Subcommands: []cli.Command{
  665. {
  666. Name: "rule",
  667. Usage: "getstatus rule $rule_name",
  668. //Flags: nflag,
  669. Action: func(c *cli.Context) error {
  670. if len(c.Args()) != 1 {
  671. fmt.Printf("Expect rule name.\n")
  672. return nil
  673. }
  674. rname := c.Args()[0]
  675. var reply string
  676. err = client.Call("Server.GetStatusRule", rname, &reply)
  677. if err != nil {
  678. fmt.Println(err)
  679. } else {
  680. fmt.Println(reply)
  681. }
  682. return nil
  683. },
  684. },
  685. },
  686. },
  687. {
  688. Name: "gettopo",
  689. Aliases: []string{"gettopo"},
  690. Usage: "gettopo rule $rule_name",
  691. Subcommands: []cli.Command{
  692. {
  693. Name: "rule",
  694. Usage: "getstopo rule $rule_name",
  695. //Flags: nflag,
  696. Action: func(c *cli.Context) error {
  697. if len(c.Args()) != 1 {
  698. fmt.Printf("Expect rule name.\n")
  699. return nil
  700. }
  701. rname := c.Args()[0]
  702. var reply string
  703. err = client.Call("Server.GetTopoRule", rname, &reply)
  704. if err != nil {
  705. fmt.Println(err)
  706. } else {
  707. fmt.Println(reply)
  708. }
  709. return nil
  710. },
  711. },
  712. },
  713. },
  714. {
  715. Name: "start",
  716. Aliases: []string{"start"},
  717. Usage: "start rule $rule_name",
  718. Subcommands: []cli.Command{
  719. {
  720. Name: "rule",
  721. Usage: "start rule $rule_name",
  722. //Flags: nflag,
  723. Action: func(c *cli.Context) error {
  724. if len(c.Args()) != 1 {
  725. fmt.Printf("Expect rule name.\n")
  726. return nil
  727. }
  728. rname := c.Args()[0]
  729. var reply string
  730. err = client.Call("Server.StartRule", rname, &reply)
  731. if err != nil {
  732. fmt.Println(err)
  733. } else {
  734. fmt.Println(reply)
  735. }
  736. return nil
  737. },
  738. },
  739. },
  740. },
  741. {
  742. Name: "stop",
  743. Aliases: []string{"stop"},
  744. Usage: "stop rule $rule_name",
  745. Subcommands: []cli.Command{
  746. {
  747. Name: "rule",
  748. Usage: "stop rule $rule_name",
  749. //Flags: nflag,
  750. Action: func(c *cli.Context) error {
  751. if len(c.Args()) != 1 {
  752. fmt.Printf("Expect rule name.\n")
  753. return nil
  754. }
  755. rname := c.Args()[0]
  756. var reply string
  757. err = client.Call("Server.StopRule", rname, &reply)
  758. if err != nil {
  759. fmt.Println(err)
  760. } else {
  761. fmt.Println(reply)
  762. }
  763. return nil
  764. },
  765. },
  766. },
  767. },
  768. {
  769. Name: "restart",
  770. Aliases: []string{"restart"},
  771. Usage: "restart rule $rule_name",
  772. Subcommands: []cli.Command{
  773. {
  774. Name: "rule",
  775. Usage: "restart rule $rule_name",
  776. //Flags: nflag,
  777. Action: func(c *cli.Context) error {
  778. if len(c.Args()) != 1 {
  779. fmt.Printf("Expect rule name.\n")
  780. return nil
  781. }
  782. rname := c.Args()[0]
  783. var reply string
  784. err = client.Call("Server.RestartRule", rname, &reply)
  785. if err != nil {
  786. fmt.Println(err)
  787. } else {
  788. fmt.Println(reply)
  789. }
  790. return nil
  791. },
  792. },
  793. },
  794. },
  795. {
  796. Name: "register",
  797. Aliases: []string{"register"},
  798. Usage: "register plugin function $plugin_name [$plugin_json | -f plugin_def_file]",
  799. Subcommands: []cli.Command{
  800. {
  801. Name: "plugin",
  802. Usage: "register plugin $plugin_type $plugin_name [$plugin_json | -f plugin_def_file]",
  803. Flags: []cli.Flag{
  804. cli.StringFlag{
  805. Name: "file, f",
  806. Usage: "the location of plugin functions definition file",
  807. FilePath: "/home/myplugin.txt",
  808. },
  809. },
  810. Action: func(c *cli.Context) error {
  811. if len(c.Args()) < 2 {
  812. fmt.Printf("Expect plugin type and name.\n")
  813. return nil
  814. }
  815. ptype := c.Args()[0]
  816. if strings.ToLower(ptype) != "function" {
  817. fmt.Printf("Plugin type must be function.\n")
  818. return nil
  819. }
  820. pname := c.Args()[1]
  821. sfile := c.String("file")
  822. args := &model.PluginDesc{
  823. RPCArgDesc: model.RPCArgDesc{
  824. Name: pname,
  825. },
  826. }
  827. if sfile != "" {
  828. if len(c.Args()) != 2 {
  829. fmt.Printf("Expect plugin type, name.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  830. return nil
  831. }
  832. if p, err := readDef(sfile, "plugin"); err != nil {
  833. fmt.Printf("%s", err)
  834. return nil
  835. } else {
  836. args.Json = string(p)
  837. }
  838. } else {
  839. if len(c.Args()) != 3 {
  840. fmt.Printf("Expect plugin type, name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  841. return nil
  842. }
  843. args.Json = c.Args()[2]
  844. }
  845. var reply string
  846. err = client.Call("Server.RegisterPlugin", args, &reply)
  847. if err != nil {
  848. fmt.Println(err)
  849. } else {
  850. fmt.Println(reply)
  851. }
  852. return nil
  853. },
  854. },
  855. },
  856. },
  857. }
  858. app.Name = "Kuiper"
  859. app.Usage = "The command line tool for EMQ X Kuiper."
  860. app.Action = func(c *cli.Context) error {
  861. cli.ShowSubcommandHelp(c)
  862. //cli.ShowVersion(c)
  863. return nil
  864. }
  865. sort.Sort(cli.FlagsByName(app.Flags))
  866. sort.Sort(cli.CommandsByName(app.Commands))
  867. err = app.Run(os.Args)
  868. if err != nil {
  869. fmt.Printf("%v", err)
  870. }
  871. }
  872. func getPluginType(arg string) (ptype int, err error) {
  873. switch arg {
  874. case "source":
  875. ptype = 0
  876. case "sink":
  877. ptype = 1
  878. case "function":
  879. ptype = 2
  880. default:
  881. err = fmt.Errorf("Invalid plugin type %s, should be \"source\", \"sink\" or \"function\".\n", arg)
  882. }
  883. return
  884. }
  885. func readDef(sfile string, t string) ([]byte, error) {
  886. if _, err := os.Stat(sfile); os.IsNotExist(err) {
  887. return nil, fmt.Errorf("The specified %s defenition file %s is not existed.\n", t, sfile)
  888. }
  889. fmt.Printf("Creating a new %s from file %s.\n", t, sfile)
  890. if rule, err := ioutil.ReadFile(sfile); err != nil {
  891. return nil, fmt.Errorf("Failed to read from %s definition file %s.\n", t, sfile)
  892. } else {
  893. return rule, nil
  894. }
  895. }