main.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. // Copyright 2021-2022 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. "net/rpc"
  19. "os"
  20. "sort"
  21. "strings"
  22. "time"
  23. "github.com/lf-edge/ekuiper/internal/conf"
  24. "github.com/lf-edge/ekuiper/internal/pkg/model"
  25. "github.com/lf-edge/ekuiper/pkg/infra"
  26. "github.com/urfave/cli"
  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.EqualFold(text, "quit") || strings.EqualFold(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. err := infra.SafeRun(func() error {
  113. for {
  114. <-ticker.C
  115. var result string
  116. e := client.Call("Server.GetQueryResult", "", &result)
  117. if e != nil {
  118. return e
  119. }
  120. if result != "" {
  121. fmt.Println(result)
  122. }
  123. }
  124. })
  125. if err != nil {
  126. fmt.Println(err)
  127. fmt.Print("kuiper > ")
  128. }
  129. }()
  130. }
  131. }
  132. }
  133. return nil
  134. },
  135. },
  136. {
  137. Name: "create",
  138. Aliases: []string{"create"},
  139. 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 | create schema $schema_type $schema_name $schema_json",
  140. Subcommands: []cli.Command{
  141. {
  142. Name: "stream",
  143. Usage: "create stream $stream_name [-f stream_def_file]",
  144. Flags: []cli.Flag{
  145. cli.StringFlag{
  146. Name: "file, f",
  147. Usage: "the location of stream definition file",
  148. FilePath: "/home/mystream.txt",
  149. },
  150. },
  151. Action: func(c *cli.Context) error {
  152. sfile := c.String("file")
  153. if sfile != "" {
  154. if stream, err := readDef(sfile, "stream"); err != nil {
  155. fmt.Printf("%s", err)
  156. return nil
  157. } else {
  158. args := strings.Join([]string{"CREATE STREAM ", string(stream)}, " ")
  159. streamProcess(client, args)
  160. return nil
  161. }
  162. } else {
  163. streamProcess(client, "")
  164. return nil
  165. }
  166. },
  167. },
  168. {
  169. Name: "table",
  170. Usage: "create table $table_name [-f table_def_file]",
  171. Flags: []cli.Flag{
  172. cli.StringFlag{
  173. Name: "file, f",
  174. Usage: "the location of table definition file",
  175. FilePath: "/home/mytable.txt",
  176. },
  177. },
  178. Action: func(c *cli.Context) error {
  179. sfile := c.String("file")
  180. if sfile != "" {
  181. if stream, err := readDef(sfile, "table"); err != nil {
  182. fmt.Printf("%s", err)
  183. return nil
  184. } else {
  185. args := strings.Join([]string{"CREATE TABLE ", string(stream)}, " ")
  186. streamProcess(client, args)
  187. return nil
  188. }
  189. } else {
  190. streamProcess(client, "")
  191. return nil
  192. }
  193. },
  194. },
  195. {
  196. Name: "rule",
  197. Usage: "create rule $rule_name [$rule_json | -f rule_def_file]",
  198. Flags: []cli.Flag{
  199. cli.StringFlag{
  200. Name: "file, f",
  201. Usage: "the location of rule definition file",
  202. FilePath: "/home/myrule.txt",
  203. },
  204. },
  205. Action: func(c *cli.Context) error {
  206. sfile := c.String("file")
  207. if sfile != "" {
  208. if rule, err := readDef(sfile, "rule"); err != nil {
  209. fmt.Printf("%s", err)
  210. return nil
  211. } else {
  212. if len(c.Args()) != 1 {
  213. fmt.Printf("Expect rule name.\n")
  214. return nil
  215. }
  216. rname := c.Args()[0]
  217. var reply string
  218. args := &model.RPCArgDesc{Name: rname, Json: string(rule)}
  219. err = client.Call("Server.CreateRule", args, &reply)
  220. if err != nil {
  221. fmt.Println(err)
  222. } else {
  223. fmt.Println(reply)
  224. }
  225. }
  226. return nil
  227. } else {
  228. if len(c.Args()) != 2 {
  229. fmt.Printf("Expect rule name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  230. return nil
  231. }
  232. rname := c.Args()[0]
  233. rjson := c.Args()[1]
  234. var reply string
  235. args := &model.RPCArgDesc{Name: rname, Json: rjson}
  236. err = client.Call("Server.CreateRule", args, &reply)
  237. if err != nil {
  238. fmt.Println(err)
  239. } else {
  240. fmt.Println(reply)
  241. }
  242. return nil
  243. }
  244. },
  245. },
  246. {
  247. Name: "plugin",
  248. Usage: "create plugin $plugin_type $plugin_name [$plugin_json | -f plugin_def_file]",
  249. Flags: []cli.Flag{
  250. cli.StringFlag{
  251. Name: "file, f",
  252. Usage: "the location of plugin definition file",
  253. FilePath: "/home/myplugin.txt",
  254. },
  255. },
  256. Action: func(c *cli.Context) error {
  257. if len(c.Args()) < 2 {
  258. fmt.Printf("Expect plugin type and name.\n")
  259. return nil
  260. }
  261. ptype, err := getPluginType(c.Args()[0])
  262. if err != nil {
  263. fmt.Printf("%s\n", err)
  264. return nil
  265. }
  266. pname := c.Args()[1]
  267. sfile := c.String("file")
  268. args := &model.PluginDesc{
  269. RPCArgDesc: model.RPCArgDesc{
  270. Name: pname,
  271. },
  272. Type: ptype,
  273. }
  274. if sfile != "" {
  275. if len(c.Args()) != 2 {
  276. fmt.Printf("Expect plugin type, name.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  277. return nil
  278. }
  279. if p, err := readDef(sfile, "plugin"); err != nil {
  280. fmt.Printf("%s", err)
  281. return nil
  282. } else {
  283. args.Json = string(p)
  284. }
  285. } else {
  286. if len(c.Args()) != 3 {
  287. fmt.Printf("Expect plugin type, name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  288. return nil
  289. }
  290. args.Json = c.Args()[2]
  291. }
  292. var reply string
  293. err = client.Call("Server.CreatePlugin", args, &reply)
  294. if err != nil {
  295. fmt.Println(err)
  296. } else {
  297. fmt.Println(reply)
  298. }
  299. return nil
  300. },
  301. },
  302. {
  303. Name: "service",
  304. Usage: "create service $service_name $service_json",
  305. Action: func(c *cli.Context) error {
  306. if len(c.Args()) < 2 {
  307. fmt.Printf("Expect service name and json.\n")
  308. return nil
  309. }
  310. var reply string
  311. err = client.Call("Server.CreateService", &model.RPCArgDesc{
  312. Name: c.Args()[0],
  313. Json: c.Args()[1],
  314. }, &reply)
  315. if err != nil {
  316. fmt.Println(err)
  317. } else {
  318. fmt.Println(reply)
  319. }
  320. return nil
  321. },
  322. },
  323. {
  324. Name: "schema",
  325. Usage: "create schema $schema_type $schema_name $schema_json",
  326. Action: func(c *cli.Context) error {
  327. if len(c.Args()) < 3 {
  328. fmt.Printf("Expect plugin type, name and json.\n")
  329. return nil
  330. }
  331. var reply string
  332. err = client.Call("Server.CreateSchema", &model.RPCTypedArgDesc{
  333. Type: c.Args()[0],
  334. Name: c.Args()[1],
  335. Json: c.Args()[2],
  336. }, &reply)
  337. if err != nil {
  338. fmt.Println(err)
  339. } else {
  340. fmt.Println(reply)
  341. }
  342. return nil
  343. },
  344. },
  345. },
  346. },
  347. {
  348. Name: "describe",
  349. Aliases: []string{"describe"},
  350. 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 | describe schema $schema_type $schema_name",
  351. Subcommands: []cli.Command{
  352. {
  353. Name: "stream",
  354. Usage: "describe stream $stream_name",
  355. //Flags: nflag,
  356. Action: func(c *cli.Context) error {
  357. streamProcess(client, "")
  358. return nil
  359. },
  360. },
  361. {
  362. Name: "table",
  363. Usage: "describe table $table_name",
  364. //Flags: nflag,
  365. Action: func(c *cli.Context) error {
  366. streamProcess(client, "")
  367. return nil
  368. },
  369. },
  370. {
  371. Name: "rule",
  372. Usage: "describe rule $rule_name",
  373. Action: func(c *cli.Context) error {
  374. if len(c.Args()) != 1 {
  375. fmt.Printf("Expect rule name.\n")
  376. return nil
  377. }
  378. rname := c.Args()[0]
  379. var reply string
  380. err = client.Call("Server.DescRule", rname, &reply)
  381. if err != nil {
  382. fmt.Println(err)
  383. } else {
  384. fmt.Println(reply)
  385. }
  386. return nil
  387. },
  388. },
  389. {
  390. Name: "plugin",
  391. Usage: "describe plugin $plugin_type $plugin_name",
  392. //Flags: nflag,
  393. Action: func(c *cli.Context) error {
  394. ptype, err := getPluginType(c.Args()[0])
  395. if err != nil {
  396. fmt.Printf("%s\n", err)
  397. return nil
  398. }
  399. if len(c.Args()) != 2 {
  400. fmt.Printf("Expect plugin name.\n")
  401. return nil
  402. }
  403. pname := c.Args()[1]
  404. args := &model.PluginDesc{
  405. RPCArgDesc: model.RPCArgDesc{
  406. Name: pname,
  407. },
  408. Type: ptype,
  409. }
  410. var reply string
  411. err = client.Call("Server.DescPlugin", args, &reply)
  412. if err != nil {
  413. fmt.Println(err)
  414. } else {
  415. fmt.Println(reply)
  416. }
  417. return nil
  418. },
  419. },
  420. {
  421. Name: "udf",
  422. Usage: "describe udf $udf_name",
  423. //Flags: nflag,
  424. Action: func(c *cli.Context) error {
  425. if len(c.Args()) != 1 {
  426. fmt.Printf("Expect udf name.\n")
  427. return nil
  428. }
  429. pname := c.Args()[0]
  430. var reply string
  431. err = client.Call("Server.DescUdf", pname, &reply)
  432. if err != nil {
  433. fmt.Println(err)
  434. } else {
  435. fmt.Println(reply)
  436. }
  437. return nil
  438. },
  439. },
  440. {
  441. Name: "service",
  442. Usage: "describe service $service_name",
  443. Action: func(c *cli.Context) error {
  444. if len(c.Args()) != 1 {
  445. fmt.Printf("Expect service name.\n")
  446. return nil
  447. }
  448. name := c.Args()[0]
  449. var reply string
  450. err = client.Call("Server.DescService", name, &reply)
  451. if err != nil {
  452. fmt.Println(err)
  453. } else {
  454. fmt.Println(reply)
  455. }
  456. return nil
  457. },
  458. },
  459. {
  460. Name: "service_func",
  461. Usage: "describe service_func $service_func_name",
  462. Action: func(c *cli.Context) error {
  463. if len(c.Args()) != 1 {
  464. fmt.Printf("Expect service func name.\n")
  465. return nil
  466. }
  467. name := c.Args()[0]
  468. var reply string
  469. err = client.Call("Server.DescServiceFunc", name, &reply)
  470. if err != nil {
  471. fmt.Println(err)
  472. } else {
  473. fmt.Println(reply)
  474. }
  475. return nil
  476. },
  477. },
  478. {
  479. Name: "schema",
  480. Usage: "describe schema $schema_type $schema_name",
  481. Action: func(c *cli.Context) error {
  482. if len(c.Args()) != 2 {
  483. fmt.Printf("Expect schema type and name.\n")
  484. return nil
  485. }
  486. args := &model.RPCTypedArgDesc{
  487. Type: c.Args()[0],
  488. Name: c.Args()[1],
  489. }
  490. var reply string
  491. err = client.Call("Server.DescSchema", args, &reply)
  492. if err != nil {
  493. fmt.Println(err)
  494. } else {
  495. fmt.Println(reply)
  496. }
  497. return nil
  498. },
  499. },
  500. },
  501. },
  502. {
  503. Name: "drop",
  504. Aliases: []string{"drop"},
  505. Usage: "drop stream $stream_name | drop table $table_name |drop rule $rule_name | drop plugin $plugin_type $plugin_name -s $stop | drop service $service_name | drop schema $schema_type $schema_name",
  506. Subcommands: []cli.Command{
  507. {
  508. Name: "stream",
  509. Usage: "drop stream $stream_name",
  510. //Flags: nflag,
  511. Action: func(c *cli.Context) error {
  512. streamProcess(client, "")
  513. return nil
  514. },
  515. },
  516. {
  517. Name: "table",
  518. Usage: "drop table $table_name",
  519. //Flags: nflag,
  520. Action: func(c *cli.Context) error {
  521. streamProcess(client, "")
  522. return nil
  523. },
  524. },
  525. {
  526. Name: "rule",
  527. Usage: "drop rule $rule_name",
  528. //Flags: nflag,
  529. Action: func(c *cli.Context) error {
  530. if len(c.Args()) != 1 {
  531. fmt.Printf("Expect rule name.\n")
  532. return nil
  533. }
  534. rname := c.Args()[0]
  535. var reply string
  536. err = client.Call("Server.DropRule", rname, &reply)
  537. if err != nil {
  538. fmt.Println(err)
  539. } else {
  540. fmt.Println(reply)
  541. }
  542. return nil
  543. },
  544. },
  545. {
  546. Name: "plugin",
  547. Usage: "drop plugin $plugin_type $plugin_name -s stop",
  548. Flags: []cli.Flag{
  549. cli.StringFlag{
  550. Name: "stop, s",
  551. Usage: "stop kuiper after the action",
  552. },
  553. },
  554. Action: func(c *cli.Context) error {
  555. r := c.String("stop")
  556. if r != "true" && r != "false" {
  557. fmt.Printf("Expect s flag to be a boolean value.\n")
  558. return nil
  559. }
  560. if len(c.Args()) < 2 || len(c.Args()) > 3 {
  561. fmt.Printf("Expect plugin type and name.\n")
  562. return nil
  563. }
  564. ptype, err := getPluginType(c.Args()[0])
  565. if err != nil {
  566. fmt.Printf("%s\n", err)
  567. return nil
  568. }
  569. pname := c.Args()[1]
  570. args := &model.PluginDesc{
  571. RPCArgDesc: model.RPCArgDesc{
  572. Name: pname,
  573. },
  574. Type: ptype,
  575. Stop: r == "true",
  576. }
  577. var reply string
  578. err = client.Call("Server.DropPlugin", args, &reply)
  579. if err != nil {
  580. fmt.Println(err)
  581. } else {
  582. fmt.Println(reply)
  583. }
  584. return nil
  585. },
  586. },
  587. {
  588. Name: "service",
  589. Usage: "drop service $service_name",
  590. Action: func(c *cli.Context) error {
  591. if len(c.Args()) != 1 {
  592. fmt.Printf("Expect service name.\n")
  593. return nil
  594. }
  595. name := c.Args()[0]
  596. var reply string
  597. err = client.Call("Server.DropService", name, &reply)
  598. if err != nil {
  599. fmt.Println(err)
  600. } else {
  601. fmt.Println(reply)
  602. }
  603. return nil
  604. },
  605. },
  606. {
  607. Name: "schema",
  608. Usage: "drop schema $schema_type $schema_name",
  609. Action: func(c *cli.Context) error {
  610. if len(c.Args()) != 2 {
  611. fmt.Printf("Expect schema type and name.\n")
  612. return nil
  613. }
  614. args := &model.RPCTypedArgDesc{
  615. Type: c.Args()[0],
  616. Name: c.Args()[1],
  617. }
  618. var reply string
  619. err = client.Call("Server.DropSchema", args, &reply)
  620. if err != nil {
  621. fmt.Println(err)
  622. } else {
  623. fmt.Println(reply)
  624. }
  625. return nil
  626. },
  627. },
  628. },
  629. },
  630. {
  631. Name: "show",
  632. Aliases: []string{"show"},
  633. Usage: "show streams | show tables | show rules | show plugins $plugin_type | show services | show service_funcs | show schemas $schema_type",
  634. Subcommands: []cli.Command{
  635. {
  636. Name: "streams",
  637. Usage: "show streams",
  638. Action: func(c *cli.Context) error {
  639. streamProcess(client, "")
  640. return nil
  641. },
  642. },
  643. {
  644. Name: "tables",
  645. Usage: "show tables",
  646. Action: func(c *cli.Context) error {
  647. streamProcess(client, "")
  648. return nil
  649. },
  650. },
  651. {
  652. Name: "rules",
  653. Usage: "show rules",
  654. Action: func(c *cli.Context) error {
  655. var reply string
  656. err = client.Call("Server.ShowRules", 0, &reply)
  657. if err != nil {
  658. fmt.Println(err)
  659. } else {
  660. fmt.Println(reply)
  661. }
  662. return nil
  663. },
  664. },
  665. {
  666. Name: "plugins",
  667. Usage: "show plugins $plugin_type",
  668. Action: func(c *cli.Context) error {
  669. if len(c.Args()) != 1 {
  670. fmt.Printf("Expect plugin type.\n")
  671. return nil
  672. }
  673. ptype, err := getPluginType(c.Args()[0])
  674. if err != nil {
  675. fmt.Printf("%s\n", err)
  676. return nil
  677. }
  678. var reply string
  679. err = client.Call("Server.ShowPlugins", ptype, &reply)
  680. if err != nil {
  681. fmt.Println(err)
  682. } else {
  683. fmt.Println(reply)
  684. }
  685. return nil
  686. },
  687. },
  688. {
  689. Name: "udfs",
  690. Usage: "show udfs",
  691. Action: func(c *cli.Context) error {
  692. var reply string
  693. err = client.Call("Server.ShowUdfs", 0, &reply)
  694. if err != nil {
  695. fmt.Println(err)
  696. } else {
  697. fmt.Println(reply)
  698. }
  699. return nil
  700. },
  701. }, {
  702. Name: "services",
  703. Usage: "show services",
  704. Action: func(c *cli.Context) error {
  705. var reply string
  706. err = client.Call("Server.ShowServices", 0, &reply)
  707. if err != nil {
  708. fmt.Println(err)
  709. } else {
  710. fmt.Println(reply)
  711. }
  712. return nil
  713. },
  714. }, {
  715. Name: "service_funcs",
  716. Usage: "show service_funcs",
  717. Action: func(c *cli.Context) error {
  718. var reply string
  719. err = client.Call("Server.ShowServiceFuncs", 0, &reply)
  720. if err != nil {
  721. fmt.Println(err)
  722. } else {
  723. fmt.Println(reply)
  724. }
  725. return nil
  726. },
  727. }, {
  728. Name: "schemas",
  729. Usage: "show schemas $schema_type",
  730. Action: func(c *cli.Context) error {
  731. if len(c.Args()) != 1 {
  732. fmt.Printf("Expect schema type.\n")
  733. return nil
  734. }
  735. var reply string
  736. err = client.Call("Server.ShowSchemas", c.Args()[0], &reply)
  737. if err != nil {
  738. fmt.Println(err)
  739. } else {
  740. fmt.Println(reply)
  741. }
  742. return nil
  743. },
  744. },
  745. },
  746. },
  747. {
  748. Name: "getstatus",
  749. Aliases: []string{"getstatus"},
  750. Usage: "getstatus rule $rule_name",
  751. Subcommands: []cli.Command{
  752. {
  753. Name: "rule",
  754. Usage: "getstatus rule $rule_name",
  755. //Flags: nflag,
  756. Action: func(c *cli.Context) error {
  757. if len(c.Args()) != 1 {
  758. fmt.Printf("Expect rule name.\n")
  759. return nil
  760. }
  761. rname := c.Args()[0]
  762. var reply string
  763. err = client.Call("Server.GetStatusRule", rname, &reply)
  764. if err != nil {
  765. fmt.Println(err)
  766. } else {
  767. fmt.Println(reply)
  768. }
  769. return nil
  770. },
  771. },
  772. },
  773. },
  774. {
  775. Name: "gettopo",
  776. Aliases: []string{"gettopo"},
  777. Usage: "gettopo rule $rule_name",
  778. Subcommands: []cli.Command{
  779. {
  780. Name: "rule",
  781. Usage: "getstopo rule $rule_name",
  782. //Flags: nflag,
  783. Action: func(c *cli.Context) error {
  784. if len(c.Args()) != 1 {
  785. fmt.Printf("Expect rule name.\n")
  786. return nil
  787. }
  788. rname := c.Args()[0]
  789. var reply string
  790. err = client.Call("Server.GetTopoRule", rname, &reply)
  791. if err != nil {
  792. fmt.Println(err)
  793. } else {
  794. fmt.Println(reply)
  795. }
  796. return nil
  797. },
  798. },
  799. },
  800. },
  801. {
  802. Name: "start",
  803. Aliases: []string{"start"},
  804. Usage: "start rule $rule_name",
  805. Subcommands: []cli.Command{
  806. {
  807. Name: "rule",
  808. Usage: "start rule $rule_name",
  809. //Flags: nflag,
  810. Action: func(c *cli.Context) error {
  811. if len(c.Args()) != 1 {
  812. fmt.Printf("Expect rule name.\n")
  813. return nil
  814. }
  815. rname := c.Args()[0]
  816. var reply string
  817. err = client.Call("Server.StartRule", rname, &reply)
  818. if err != nil {
  819. fmt.Println(err)
  820. } else {
  821. fmt.Println(reply)
  822. }
  823. return nil
  824. },
  825. },
  826. },
  827. },
  828. {
  829. Name: "stop",
  830. Aliases: []string{"stop"},
  831. Usage: "stop rule $rule_name",
  832. Subcommands: []cli.Command{
  833. {
  834. Name: "rule",
  835. Usage: "stop rule $rule_name",
  836. //Flags: nflag,
  837. Action: func(c *cli.Context) error {
  838. if len(c.Args()) != 1 {
  839. fmt.Printf("Expect rule name.\n")
  840. return nil
  841. }
  842. rname := c.Args()[0]
  843. var reply string
  844. err = client.Call("Server.StopRule", rname, &reply)
  845. if err != nil {
  846. fmt.Println(err)
  847. } else {
  848. fmt.Println(reply)
  849. }
  850. return nil
  851. },
  852. },
  853. },
  854. },
  855. {
  856. Name: "restart",
  857. Aliases: []string{"restart"},
  858. Usage: "restart rule $rule_name",
  859. Subcommands: []cli.Command{
  860. {
  861. Name: "rule",
  862. Usage: "restart rule $rule_name",
  863. //Flags: nflag,
  864. Action: func(c *cli.Context) error {
  865. if len(c.Args()) != 1 {
  866. fmt.Printf("Expect rule name.\n")
  867. return nil
  868. }
  869. rname := c.Args()[0]
  870. var reply string
  871. err = client.Call("Server.RestartRule", rname, &reply)
  872. if err != nil {
  873. fmt.Println(err)
  874. } else {
  875. fmt.Println(reply)
  876. }
  877. return nil
  878. },
  879. },
  880. },
  881. },
  882. {
  883. Name: "register",
  884. Aliases: []string{"register"},
  885. Usage: "register plugin function $plugin_name [$plugin_json | -f plugin_def_file]",
  886. Subcommands: []cli.Command{
  887. {
  888. Name: "plugin",
  889. Usage: "register plugin $plugin_type $plugin_name [$plugin_json | -f plugin_def_file]",
  890. Flags: []cli.Flag{
  891. cli.StringFlag{
  892. Name: "file, f",
  893. Usage: "the location of plugin functions definition file",
  894. FilePath: "/home/myplugin.txt",
  895. },
  896. },
  897. Action: func(c *cli.Context) error {
  898. if len(c.Args()) < 2 {
  899. fmt.Printf("Expect plugin type and name.\n")
  900. return nil
  901. }
  902. ptype := c.Args()[0]
  903. if !strings.EqualFold(ptype, "function") {
  904. fmt.Printf("Plugin type must be function.\n")
  905. return nil
  906. }
  907. pname := c.Args()[1]
  908. sfile := c.String("file")
  909. args := &model.PluginDesc{
  910. RPCArgDesc: model.RPCArgDesc{
  911. Name: pname,
  912. },
  913. }
  914. if sfile != "" {
  915. if len(c.Args()) != 2 {
  916. fmt.Printf("Expect plugin type, name.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  917. return nil
  918. }
  919. if p, err := readDef(sfile, "plugin"); err != nil {
  920. fmt.Printf("%s", err)
  921. return nil
  922. } else {
  923. args.Json = string(p)
  924. }
  925. } else {
  926. if len(c.Args()) != 3 {
  927. fmt.Printf("Expect plugin type, name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  928. return nil
  929. }
  930. args.Json = c.Args()[2]
  931. }
  932. var reply string
  933. err = client.Call("Server.RegisterPlugin", args, &reply)
  934. if err != nil {
  935. fmt.Println(err)
  936. } else {
  937. fmt.Println(reply)
  938. }
  939. return nil
  940. },
  941. },
  942. },
  943. },
  944. {
  945. Name: "import",
  946. Aliases: []string{"import"},
  947. Usage: "import ruleset -f ruleset_file",
  948. Subcommands: []cli.Command{
  949. {
  950. Name: "ruleset",
  951. Usage: "\"import ruleset -f ruleset_file",
  952. Flags: []cli.Flag{
  953. cli.StringFlag{
  954. Name: "file, f",
  955. Usage: "the location of the ruleset json file",
  956. FilePath: "/home/ekuiper_ruleset.json",
  957. },
  958. },
  959. Action: func(c *cli.Context) error {
  960. sfile := c.String("file")
  961. if sfile == "" {
  962. fmt.Print("Required ruleset json file to import")
  963. return nil
  964. }
  965. var reply string
  966. err = client.Call("Server.Import", sfile, &reply)
  967. if err != nil {
  968. fmt.Println(err)
  969. } else {
  970. fmt.Println(reply)
  971. }
  972. return nil
  973. },
  974. },
  975. },
  976. },
  977. {
  978. Name: "export",
  979. Aliases: []string{"export"},
  980. Usage: "export ruleset $ruleset_file",
  981. Subcommands: []cli.Command{
  982. {
  983. Name: "ruleset",
  984. Usage: "\"export ruleset $ruleset_file",
  985. Action: func(c *cli.Context) error {
  986. if len(c.Args()) < 1 {
  987. fmt.Printf("Require exported file name.\n")
  988. return nil
  989. }
  990. var reply string
  991. err = client.Call("Server.Export", c.Args()[0], &reply)
  992. if err != nil {
  993. fmt.Println(err)
  994. } else {
  995. fmt.Println(reply)
  996. }
  997. return nil
  998. },
  999. },
  1000. },
  1001. },
  1002. }
  1003. app.Name = "Kuiper"
  1004. app.Usage = "The command line tool for EMQ X Kuiper."
  1005. app.Action = func(c *cli.Context) error {
  1006. cli.ShowSubcommandHelp(c)
  1007. //cli.ShowVersion(c)
  1008. return nil
  1009. }
  1010. sort.Sort(cli.FlagsByName(app.Flags))
  1011. sort.Sort(cli.CommandsByName(app.Commands))
  1012. err = app.Run(os.Args)
  1013. if err != nil {
  1014. fmt.Printf("%v", err)
  1015. }
  1016. }
  1017. func getPluginType(arg string) (ptype int, err error) {
  1018. switch arg {
  1019. case "source":
  1020. ptype = 0
  1021. case "sink":
  1022. ptype = 1
  1023. case "function":
  1024. ptype = 2
  1025. case "portable":
  1026. ptype = 3
  1027. default:
  1028. err = fmt.Errorf("Invalid plugin type %s, should be \"source\", \"sink\", \"function\" or \"portable\".\n", arg)
  1029. }
  1030. return
  1031. }
  1032. func readDef(sfile string, t string) ([]byte, error) {
  1033. if _, err := os.Stat(sfile); os.IsNotExist(err) {
  1034. return nil, fmt.Errorf("The specified %s defenition file %s is not existed.\n", t, sfile)
  1035. }
  1036. fmt.Printf("Creating a new %s from file %s.\n", t, sfile)
  1037. if rule, err := os.ReadFile(sfile); err != nil {
  1038. return nil, fmt.Errorf("Failed to read from %s definition file %s.\n", t, sfile)
  1039. } else {
  1040. return rule, nil
  1041. }
  1042. }