main.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. // Copyright 2021-2023 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. "encoding/json"
  18. "fmt"
  19. "net/rpc"
  20. "os"
  21. "sort"
  22. "strings"
  23. "time"
  24. "github.com/urfave/cli"
  25. "github.com/lf-edge/ekuiper/internal/conf"
  26. "github.com/lf-edge/ekuiper/internal/pkg/model"
  27. "github.com/lf-edge/ekuiper/pkg/infra"
  28. )
  29. type clientConf struct {
  30. Host string `yaml:"host"`
  31. Port int `yaml:"port"`
  32. }
  33. const ClientYaml = "client.yaml"
  34. func streamProcess(client *rpc.Client, args string) {
  35. var reply string
  36. if args == "" {
  37. args = strings.Join(os.Args[1:], " ")
  38. }
  39. err := client.Call("Server.Stream", args, &reply)
  40. if err != nil {
  41. fmt.Println(err)
  42. } else {
  43. fmt.Println(reply)
  44. }
  45. }
  46. var (
  47. Version = "unknown"
  48. LoadFileType = "relative"
  49. )
  50. func main() {
  51. conf.LoadFileType = LoadFileType
  52. app := cli.NewApp()
  53. app.Version = Version
  54. // nflag := []cli.Flag { cli.StringFlag{
  55. // Name: "name, n",
  56. // Usage: "the name of stream",
  57. // }}
  58. var cfg map[string]clientConf
  59. err := conf.LoadConfigByName(ClientYaml, &cfg)
  60. if err != nil {
  61. conf.Log.Fatal(err)
  62. fmt.Printf("Failed to load config file with error %s.\n", err)
  63. }
  64. var config *clientConf
  65. c, ok := cfg["basic"]
  66. if !ok {
  67. fmt.Printf("No basic config in client.yaml, will use the default configuration.\n")
  68. } else {
  69. config = &c
  70. }
  71. if config == nil {
  72. config = &clientConf{
  73. Host: "127.0.0.1",
  74. Port: 20498,
  75. }
  76. }
  77. fmt.Printf("Connecting to %s:%d... \n", config.Host, config.Port)
  78. // Create a TCP connection to localhost on port 1234
  79. client, err := rpc.DialHTTP("tcp", fmt.Sprintf("%s:%d", config.Host, config.Port))
  80. if err != nil {
  81. fmt.Printf("Failed to connect the server, please start the server.\n")
  82. return
  83. }
  84. app.Commands = []cli.Command{
  85. {
  86. Name: "query",
  87. Aliases: []string{"query"},
  88. Usage: "query command line",
  89. Action: func(c *cli.Context) error {
  90. reader := bufio.NewReader(os.Stdin)
  91. ticker := time.NewTicker(time.Millisecond * 300)
  92. defer ticker.Stop()
  93. for {
  94. fmt.Print("kuiper > ")
  95. text, _ := reader.ReadString('\n')
  96. // convert CRLF to LF
  97. text = strings.Replace(text, "\n", "", -1)
  98. if strings.EqualFold(text, "quit") || strings.EqualFold(text, "exit") {
  99. break
  100. } else if strings.Trim(text, " ") == "" {
  101. continue
  102. } else {
  103. var reply string
  104. err := client.Call("Server.CreateQuery", text, &reply)
  105. if err != nil {
  106. fmt.Println(err)
  107. continue
  108. }
  109. fmt.Println(reply)
  110. go func() {
  111. err := infra.SafeRun(func() error {
  112. for {
  113. <-ticker.C
  114. var result string
  115. e := client.Call("Server.GetQueryResult", "", &result)
  116. if e != nil {
  117. return e
  118. }
  119. if result != "" {
  120. fmt.Println(result)
  121. }
  122. }
  123. })
  124. if err != nil {
  125. fmt.Println(err)
  126. fmt.Print("kuiper > ")
  127. }
  128. }()
  129. }
  130. }
  131. return nil
  132. },
  133. },
  134. {
  135. Name: "create",
  136. Aliases: []string{"create"},
  137. 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",
  138. Subcommands: []cli.Command{
  139. {
  140. Name: "stream",
  141. Usage: "create stream $stream_name [-f stream_def_file]",
  142. Flags: []cli.Flag{
  143. cli.StringFlag{
  144. Name: "file, f",
  145. Usage: "the location of stream definition file",
  146. FilePath: "/home/mystream.txt",
  147. },
  148. },
  149. Action: func(c *cli.Context) error {
  150. sfile := c.String("file")
  151. if sfile != "" {
  152. if stream, err := readDef(sfile, "stream"); err != nil {
  153. fmt.Printf("%s", err)
  154. return nil
  155. } else {
  156. args := strings.Join([]string{"CREATE STREAM ", string(stream)}, " ")
  157. streamProcess(client, args)
  158. return nil
  159. }
  160. } else {
  161. streamProcess(client, "")
  162. return nil
  163. }
  164. },
  165. },
  166. {
  167. Name: "table",
  168. Usage: "create table $table_name [-f table_def_file]",
  169. Flags: []cli.Flag{
  170. cli.StringFlag{
  171. Name: "file, f",
  172. Usage: "the location of table definition file",
  173. FilePath: "/home/mytable.txt",
  174. },
  175. },
  176. Action: func(c *cli.Context) error {
  177. sfile := c.String("file")
  178. if sfile != "" {
  179. if stream, err := readDef(sfile, "table"); err != nil {
  180. fmt.Printf("%s", err)
  181. return nil
  182. } else {
  183. args := strings.Join([]string{"CREATE TABLE ", string(stream)}, " ")
  184. streamProcess(client, args)
  185. return nil
  186. }
  187. } else {
  188. streamProcess(client, "")
  189. return nil
  190. }
  191. },
  192. },
  193. {
  194. Name: "rule",
  195. Usage: "create rule $rule_name [$rule_json | -f rule_def_file]",
  196. Flags: []cli.Flag{
  197. cli.StringFlag{
  198. Name: "file, f",
  199. Usage: "the location of rule definition file",
  200. FilePath: "/home/myrule.txt",
  201. },
  202. },
  203. Action: func(c *cli.Context) error {
  204. sfile := c.String("file")
  205. if sfile != "" {
  206. if rule, err := readDef(sfile, "rule"); err != nil {
  207. fmt.Printf("%s", err)
  208. return nil
  209. } else {
  210. if len(c.Args()) != 1 {
  211. fmt.Printf("Expect rule name.\n")
  212. return nil
  213. }
  214. rname := c.Args()[0]
  215. var reply string
  216. args := &model.RPCArgDesc{Name: rname, Json: string(rule)}
  217. err = client.Call("Server.CreateRule", args, &reply)
  218. if err != nil {
  219. fmt.Println(err)
  220. } else {
  221. fmt.Println(reply)
  222. }
  223. }
  224. return nil
  225. } else {
  226. if len(c.Args()) != 2 {
  227. fmt.Printf("Expect rule name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  228. return nil
  229. }
  230. rname := c.Args()[0]
  231. rjson := c.Args()[1]
  232. var reply string
  233. args := &model.RPCArgDesc{Name: rname, Json: rjson}
  234. err = client.Call("Server.CreateRule", args, &reply)
  235. if err != nil {
  236. fmt.Println(err)
  237. } else {
  238. fmt.Println(reply)
  239. }
  240. return nil
  241. }
  242. },
  243. },
  244. {
  245. Name: "plugin",
  246. Usage: "create plugin $plugin_type $plugin_name [$plugin_json | -f plugin_def_file]",
  247. Flags: []cli.Flag{
  248. cli.StringFlag{
  249. Name: "file, f",
  250. Usage: "the location of plugin definition file",
  251. FilePath: "/home/myplugin.txt",
  252. },
  253. },
  254. Action: func(c *cli.Context) error {
  255. if len(c.Args()) < 2 {
  256. fmt.Printf("Expect plugin type and name.\n")
  257. return nil
  258. }
  259. ptype, err := getPluginType(c.Args()[0])
  260. if err != nil {
  261. fmt.Printf("%s\n", err)
  262. return nil
  263. }
  264. pname := c.Args()[1]
  265. sfile := c.String("file")
  266. args := &model.PluginDesc{
  267. RPCArgDesc: model.RPCArgDesc{
  268. Name: pname,
  269. },
  270. Type: ptype,
  271. }
  272. if sfile != "" {
  273. if len(c.Args()) != 2 {
  274. fmt.Printf("Expect plugin type, name.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  275. return nil
  276. }
  277. if p, err := readDef(sfile, "plugin"); err != nil {
  278. fmt.Printf("%s", err)
  279. return nil
  280. } else {
  281. args.Json = string(p)
  282. }
  283. } else {
  284. if len(c.Args()) != 3 {
  285. fmt.Printf("Expect plugin type, name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  286. return nil
  287. }
  288. args.Json = c.Args()[2]
  289. }
  290. var reply string
  291. err = client.Call("Server.CreatePlugin", args, &reply)
  292. if err != nil {
  293. fmt.Println(err)
  294. } else {
  295. fmt.Println(reply)
  296. }
  297. return nil
  298. },
  299. },
  300. {
  301. Name: "service",
  302. Usage: "create service $service_name $service_json",
  303. Action: func(c *cli.Context) error {
  304. if len(c.Args()) < 2 {
  305. fmt.Printf("Expect service name and json.\n")
  306. return nil
  307. }
  308. var reply string
  309. err = client.Call("Server.CreateService", &model.RPCArgDesc{
  310. Name: c.Args()[0],
  311. Json: c.Args()[1],
  312. }, &reply)
  313. if err != nil {
  314. fmt.Println(err)
  315. } else {
  316. fmt.Println(reply)
  317. }
  318. return nil
  319. },
  320. },
  321. {
  322. Name: "schema",
  323. Usage: "create schema $schema_type $schema_name $schema_json",
  324. Action: func(c *cli.Context) error {
  325. if len(c.Args()) < 3 {
  326. fmt.Printf("Expect plugin type, name and json.\n")
  327. return nil
  328. }
  329. var reply string
  330. err = client.Call("Server.CreateSchema", &model.RPCTypedArgDesc{
  331. Type: c.Args()[0],
  332. Name: c.Args()[1],
  333. Json: c.Args()[2],
  334. }, &reply)
  335. if err != nil {
  336. fmt.Println(err)
  337. } else {
  338. fmt.Println(reply)
  339. }
  340. return nil
  341. },
  342. },
  343. },
  344. },
  345. {
  346. Name: "describe",
  347. Aliases: []string{"describe"},
  348. 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",
  349. Subcommands: []cli.Command{
  350. {
  351. Name: "stream",
  352. Usage: "describe stream $stream_name",
  353. // Flags: nflag,
  354. Action: func(c *cli.Context) error {
  355. streamProcess(client, "")
  356. return nil
  357. },
  358. },
  359. {
  360. Name: "table",
  361. Usage: "describe table $table_name",
  362. // Flags: nflag,
  363. Action: func(c *cli.Context) error {
  364. streamProcess(client, "")
  365. return nil
  366. },
  367. },
  368. {
  369. Name: "rule",
  370. Usage: "describe rule $rule_name",
  371. Action: func(c *cli.Context) error {
  372. if len(c.Args()) != 1 {
  373. fmt.Printf("Expect rule name.\n")
  374. return nil
  375. }
  376. rname := c.Args()[0]
  377. var reply string
  378. err = client.Call("Server.DescRule", rname, &reply)
  379. if err != nil {
  380. fmt.Println(err)
  381. } else {
  382. fmt.Println(reply)
  383. }
  384. return nil
  385. },
  386. },
  387. {
  388. Name: "plugin",
  389. Usage: "describe plugin $plugin_type $plugin_name",
  390. // Flags: nflag,
  391. Action: func(c *cli.Context) error {
  392. ptype, err := getPluginType(c.Args()[0])
  393. if err != nil {
  394. fmt.Printf("%s\n", err)
  395. return nil
  396. }
  397. if len(c.Args()) != 2 {
  398. fmt.Printf("Expect plugin name.\n")
  399. return nil
  400. }
  401. pname := c.Args()[1]
  402. args := &model.PluginDesc{
  403. RPCArgDesc: model.RPCArgDesc{
  404. Name: pname,
  405. },
  406. Type: ptype,
  407. }
  408. var reply string
  409. err = client.Call("Server.DescPlugin", args, &reply)
  410. if err != nil {
  411. fmt.Println(err)
  412. } else {
  413. fmt.Println(reply)
  414. }
  415. return nil
  416. },
  417. },
  418. {
  419. Name: "udf",
  420. Usage: "describe udf $udf_name",
  421. // Flags: nflag,
  422. Action: func(c *cli.Context) error {
  423. if len(c.Args()) != 1 {
  424. fmt.Printf("Expect udf name.\n")
  425. return nil
  426. }
  427. pname := c.Args()[0]
  428. var reply string
  429. err = client.Call("Server.DescUdf", pname, &reply)
  430. if err != nil {
  431. fmt.Println(err)
  432. } else {
  433. fmt.Println(reply)
  434. }
  435. return nil
  436. },
  437. },
  438. {
  439. Name: "service",
  440. Usage: "describe service $service_name",
  441. Action: func(c *cli.Context) error {
  442. if len(c.Args()) != 1 {
  443. fmt.Printf("Expect service name.\n")
  444. return nil
  445. }
  446. name := c.Args()[0]
  447. var reply string
  448. err = client.Call("Server.DescService", name, &reply)
  449. if err != nil {
  450. fmt.Println(err)
  451. } else {
  452. fmt.Println(reply)
  453. }
  454. return nil
  455. },
  456. },
  457. {
  458. Name: "service_func",
  459. Usage: "describe service_func $service_func_name",
  460. Action: func(c *cli.Context) error {
  461. if len(c.Args()) != 1 {
  462. fmt.Printf("Expect service func name.\n")
  463. return nil
  464. }
  465. name := c.Args()[0]
  466. var reply string
  467. err = client.Call("Server.DescServiceFunc", name, &reply)
  468. if err != nil {
  469. fmt.Println(err)
  470. } else {
  471. fmt.Println(reply)
  472. }
  473. return nil
  474. },
  475. },
  476. {
  477. Name: "schema",
  478. Usage: "describe schema $schema_type $schema_name",
  479. Action: func(c *cli.Context) error {
  480. if len(c.Args()) != 2 {
  481. fmt.Printf("Expect schema type and name.\n")
  482. return nil
  483. }
  484. args := &model.RPCTypedArgDesc{
  485. Type: c.Args()[0],
  486. Name: c.Args()[1],
  487. }
  488. var reply string
  489. err = client.Call("Server.DescSchema", args, &reply)
  490. if err != nil {
  491. fmt.Println(err)
  492. } else {
  493. fmt.Println(reply)
  494. }
  495. return nil
  496. },
  497. },
  498. },
  499. },
  500. {
  501. Name: "drop",
  502. Aliases: []string{"drop"},
  503. 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",
  504. Subcommands: []cli.Command{
  505. {
  506. Name: "stream",
  507. Usage: "drop stream $stream_name",
  508. // Flags: nflag,
  509. Action: func(c *cli.Context) error {
  510. streamProcess(client, "")
  511. return nil
  512. },
  513. },
  514. {
  515. Name: "table",
  516. Usage: "drop table $table_name",
  517. // Flags: nflag,
  518. Action: func(c *cli.Context) error {
  519. streamProcess(client, "")
  520. return nil
  521. },
  522. },
  523. {
  524. Name: "rule",
  525. Usage: "drop rule $rule_name",
  526. // Flags: nflag,
  527. Action: func(c *cli.Context) error {
  528. if len(c.Args()) != 1 {
  529. fmt.Printf("Expect rule name.\n")
  530. return nil
  531. }
  532. rname := c.Args()[0]
  533. var reply string
  534. err = client.Call("Server.DropRule", rname, &reply)
  535. if err != nil {
  536. fmt.Println(err)
  537. } else {
  538. fmt.Println(reply)
  539. }
  540. return nil
  541. },
  542. },
  543. {
  544. Name: "plugin",
  545. Usage: "drop plugin $plugin_type $plugin_name -s stop",
  546. Flags: []cli.Flag{
  547. cli.StringFlag{
  548. Name: "stop, s",
  549. Usage: "stop kuiper after the action",
  550. },
  551. },
  552. Action: func(c *cli.Context) error {
  553. r := c.String("stop")
  554. if r != "true" && r != "false" {
  555. fmt.Printf("Expect s flag to be a boolean value.\n")
  556. return nil
  557. }
  558. if len(c.Args()) < 2 || len(c.Args()) > 3 {
  559. fmt.Printf("Expect plugin type and name.\n")
  560. return nil
  561. }
  562. ptype, err := getPluginType(c.Args()[0])
  563. if err != nil {
  564. fmt.Printf("%s\n", err)
  565. return nil
  566. }
  567. pname := c.Args()[1]
  568. args := &model.PluginDesc{
  569. RPCArgDesc: model.RPCArgDesc{
  570. Name: pname,
  571. },
  572. Type: ptype,
  573. Stop: r == "true",
  574. }
  575. var reply string
  576. err = client.Call("Server.DropPlugin", args, &reply)
  577. if err != nil {
  578. fmt.Println(err)
  579. } else {
  580. fmt.Println(reply)
  581. }
  582. return nil
  583. },
  584. },
  585. {
  586. Name: "service",
  587. Usage: "drop service $service_name",
  588. Action: func(c *cli.Context) error {
  589. if len(c.Args()) != 1 {
  590. fmt.Printf("Expect service name.\n")
  591. return nil
  592. }
  593. name := c.Args()[0]
  594. var reply string
  595. err = client.Call("Server.DropService", name, &reply)
  596. if err != nil {
  597. fmt.Println(err)
  598. } else {
  599. fmt.Println(reply)
  600. }
  601. return nil
  602. },
  603. },
  604. {
  605. Name: "schema",
  606. Usage: "drop schema $schema_type $schema_name",
  607. Action: func(c *cli.Context) error {
  608. if len(c.Args()) != 2 {
  609. fmt.Printf("Expect schema type and name.\n")
  610. return nil
  611. }
  612. args := &model.RPCTypedArgDesc{
  613. Type: c.Args()[0],
  614. Name: c.Args()[1],
  615. }
  616. var reply string
  617. err = client.Call("Server.DropSchema", args, &reply)
  618. if err != nil {
  619. fmt.Println(err)
  620. } else {
  621. fmt.Println(reply)
  622. }
  623. return nil
  624. },
  625. },
  626. },
  627. },
  628. {
  629. Name: "show",
  630. Aliases: []string{"show"},
  631. Usage: "show streams | show tables | show rules | show plugins $plugin_type | show services | show service_funcs | show schemas $schema_type",
  632. Subcommands: []cli.Command{
  633. {
  634. Name: "streams",
  635. Usage: "show streams",
  636. Action: func(c *cli.Context) error {
  637. streamProcess(client, "")
  638. return nil
  639. },
  640. },
  641. {
  642. Name: "tables",
  643. Usage: "show tables",
  644. Action: func(c *cli.Context) error {
  645. streamProcess(client, "")
  646. return nil
  647. },
  648. },
  649. {
  650. Name: "rules",
  651. Usage: "show rules",
  652. Action: func(c *cli.Context) error {
  653. var reply string
  654. err = client.Call("Server.ShowRules", 0, &reply)
  655. if err != nil {
  656. fmt.Println(err)
  657. } else {
  658. fmt.Println(reply)
  659. }
  660. return nil
  661. },
  662. },
  663. {
  664. Name: "plugins",
  665. Usage: "show plugins $plugin_type",
  666. Action: func(c *cli.Context) error {
  667. if len(c.Args()) != 1 {
  668. fmt.Printf("Expect plugin type.\n")
  669. return nil
  670. }
  671. ptype, err := getPluginType(c.Args()[0])
  672. if err != nil {
  673. fmt.Printf("%s\n", err)
  674. return nil
  675. }
  676. var reply string
  677. err = client.Call("Server.ShowPlugins", ptype, &reply)
  678. if err != nil {
  679. fmt.Println(err)
  680. } else {
  681. fmt.Println(reply)
  682. }
  683. return nil
  684. },
  685. },
  686. {
  687. Name: "udfs",
  688. Usage: "show udfs",
  689. Action: func(c *cli.Context) error {
  690. var reply string
  691. err = client.Call("Server.ShowUdfs", 0, &reply)
  692. if err != nil {
  693. fmt.Println(err)
  694. } else {
  695. fmt.Println(reply)
  696. }
  697. return nil
  698. },
  699. },
  700. {
  701. Name: "services",
  702. Usage: "show services",
  703. Action: func(c *cli.Context) error {
  704. var reply string
  705. err = client.Call("Server.ShowServices", 0, &reply)
  706. if err != nil {
  707. fmt.Println(err)
  708. } else {
  709. fmt.Println(reply)
  710. }
  711. return nil
  712. },
  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. {
  729. Name: "schemas",
  730. Usage: "show schemas $schema_type",
  731. Action: func(c *cli.Context) error {
  732. if len(c.Args()) != 1 {
  733. fmt.Printf("Expect schema type.\n")
  734. return nil
  735. }
  736. var reply string
  737. err = client.Call("Server.ShowSchemas", c.Args()[0], &reply)
  738. if err != nil {
  739. fmt.Println(err)
  740. } else {
  741. fmt.Println(reply)
  742. }
  743. return nil
  744. },
  745. },
  746. },
  747. },
  748. {
  749. Name: "getstatus",
  750. Aliases: []string{"getstatus"},
  751. Usage: "getstatus rule $rule_name | import",
  752. Subcommands: []cli.Command{
  753. {
  754. Name: "rule",
  755. Usage: "getstatus rule $rule_name",
  756. // Flags: nflag,
  757. Action: func(c *cli.Context) error {
  758. if len(c.Args()) != 1 {
  759. fmt.Printf("Expect rule name.\n")
  760. return nil
  761. }
  762. rname := c.Args()[0]
  763. var reply string
  764. err = client.Call("Server.GetStatusRule", rname, &reply)
  765. if err != nil {
  766. fmt.Println(err)
  767. } else {
  768. fmt.Println(reply)
  769. }
  770. return nil
  771. },
  772. },
  773. {
  774. Name: "import",
  775. Usage: "getstatus import",
  776. // Flags: nflag,
  777. Action: func(c *cli.Context) error {
  778. var reply string
  779. err = client.Call("Server.GetStatusImport", 0, &reply)
  780. if err != nil {
  781. fmt.Println(err)
  782. } else {
  783. fmt.Println(reply)
  784. }
  785. return nil
  786. },
  787. },
  788. },
  789. },
  790. {
  791. Name: "gettopo",
  792. Aliases: []string{"gettopo"},
  793. Usage: "gettopo rule $rule_name",
  794. Subcommands: []cli.Command{
  795. {
  796. Name: "rule",
  797. Usage: "getstopo rule $rule_name",
  798. // Flags: nflag,
  799. Action: func(c *cli.Context) error {
  800. if len(c.Args()) != 1 {
  801. fmt.Printf("Expect rule name.\n")
  802. return nil
  803. }
  804. rname := c.Args()[0]
  805. var reply string
  806. err = client.Call("Server.GetTopoRule", rname, &reply)
  807. if err != nil {
  808. fmt.Println(err)
  809. } else {
  810. fmt.Println(reply)
  811. }
  812. return nil
  813. },
  814. },
  815. },
  816. },
  817. {
  818. Name: "start",
  819. Aliases: []string{"start"},
  820. Usage: "start rule $rule_name",
  821. Subcommands: []cli.Command{
  822. {
  823. Name: "rule",
  824. Usage: "start rule $rule_name",
  825. // Flags: nflag,
  826. Action: func(c *cli.Context) error {
  827. if len(c.Args()) != 1 {
  828. fmt.Printf("Expect rule name.\n")
  829. return nil
  830. }
  831. rname := c.Args()[0]
  832. var reply string
  833. err = client.Call("Server.StartRule", rname, &reply)
  834. if err != nil {
  835. fmt.Println(err)
  836. } else {
  837. fmt.Println(reply)
  838. }
  839. return nil
  840. },
  841. },
  842. },
  843. },
  844. {
  845. Name: "stop",
  846. Aliases: []string{"stop"},
  847. Usage: "stop rule $rule_name",
  848. Subcommands: []cli.Command{
  849. {
  850. Name: "rule",
  851. Usage: "stop rule $rule_name",
  852. // Flags: nflag,
  853. Action: func(c *cli.Context) error {
  854. if len(c.Args()) != 1 {
  855. fmt.Printf("Expect rule name.\n")
  856. return nil
  857. }
  858. rname := c.Args()[0]
  859. var reply string
  860. err = client.Call("Server.StopRule", rname, &reply)
  861. if err != nil {
  862. fmt.Println(err)
  863. } else {
  864. fmt.Println(reply)
  865. }
  866. return nil
  867. },
  868. },
  869. },
  870. },
  871. {
  872. Name: "restart",
  873. Aliases: []string{"restart"},
  874. Usage: "restart rule $rule_name",
  875. Subcommands: []cli.Command{
  876. {
  877. Name: "rule",
  878. Usage: "restart rule $rule_name",
  879. // Flags: nflag,
  880. Action: func(c *cli.Context) error {
  881. if len(c.Args()) != 1 {
  882. fmt.Printf("Expect rule name.\n")
  883. return nil
  884. }
  885. rname := c.Args()[0]
  886. var reply string
  887. err = client.Call("Server.RestartRule", rname, &reply)
  888. if err != nil {
  889. fmt.Println(err)
  890. } else {
  891. fmt.Println(reply)
  892. }
  893. return nil
  894. },
  895. },
  896. },
  897. },
  898. {
  899. Name: "register",
  900. Aliases: []string{"register"},
  901. Usage: "register plugin function $plugin_name [$plugin_json | -f plugin_def_file]",
  902. Subcommands: []cli.Command{
  903. {
  904. Name: "plugin",
  905. Usage: "register plugin $plugin_type $plugin_name [$plugin_json | -f plugin_def_file]",
  906. Flags: []cli.Flag{
  907. cli.StringFlag{
  908. Name: "file, f",
  909. Usage: "the location of plugin functions definition file",
  910. FilePath: "/home/myplugin.txt",
  911. },
  912. },
  913. Action: func(c *cli.Context) error {
  914. if len(c.Args()) < 2 {
  915. fmt.Printf("Expect plugin type and name.\n")
  916. return nil
  917. }
  918. ptype := c.Args()[0]
  919. if !strings.EqualFold(ptype, "function") {
  920. fmt.Printf("Plugin type must be function.\n")
  921. return nil
  922. }
  923. pname := c.Args()[1]
  924. sfile := c.String("file")
  925. args := &model.PluginDesc{
  926. RPCArgDesc: model.RPCArgDesc{
  927. Name: pname,
  928. },
  929. }
  930. if sfile != "" {
  931. if len(c.Args()) != 2 {
  932. fmt.Printf("Expect plugin type, name.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  933. return nil
  934. }
  935. if p, err := readDef(sfile, "plugin"); err != nil {
  936. fmt.Printf("%s", err)
  937. return nil
  938. } else {
  939. args.Json = string(p)
  940. }
  941. } else {
  942. if len(c.Args()) != 3 {
  943. fmt.Printf("Expect plugin type, name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  944. return nil
  945. }
  946. args.Json = c.Args()[2]
  947. }
  948. var reply string
  949. err = client.Call("Server.RegisterPlugin", args, &reply)
  950. if err != nil {
  951. fmt.Println(err)
  952. } else {
  953. fmt.Println(reply)
  954. }
  955. return nil
  956. },
  957. },
  958. },
  959. },
  960. {
  961. Name: "import",
  962. Aliases: []string{"import"},
  963. Usage: "import ruleset | data -f file -p partial -s stop",
  964. Subcommands: []cli.Command{
  965. {
  966. Name: "ruleset",
  967. Usage: "\"import ruleset -f ruleset_file",
  968. Flags: []cli.Flag{
  969. cli.StringFlag{
  970. Name: "file, f",
  971. Usage: "the location of the ruleset json file",
  972. FilePath: "/home/ekuiper_ruleset.json",
  973. },
  974. },
  975. Action: func(c *cli.Context) error {
  976. sfile := c.String("file")
  977. if sfile == "" {
  978. fmt.Print("Required ruleset json file to import")
  979. return nil
  980. }
  981. var reply string
  982. err = client.Call("Server.Import", sfile, &reply)
  983. if err != nil {
  984. fmt.Println(err)
  985. } else {
  986. fmt.Println(reply)
  987. }
  988. return nil
  989. },
  990. },
  991. {
  992. Name: "data",
  993. Usage: "\"import data -f configuration_file -p partial -s stop",
  994. Flags: []cli.Flag{
  995. cli.StringFlag{
  996. Name: "file, f",
  997. Usage: "the location of the configuration json file",
  998. FilePath: "/home/ekuiper_configuration.json",
  999. },
  1000. cli.StringFlag{
  1001. Name: "stop, s",
  1002. Usage: "stop kuiper after the action",
  1003. },
  1004. cli.StringFlag{
  1005. Name: "partial, p",
  1006. Usage: "import partial configuration",
  1007. },
  1008. },
  1009. Action: func(c *cli.Context) error {
  1010. sfile := c.String("file")
  1011. if sfile == "" {
  1012. fmt.Print("Required configuration json file to import")
  1013. return nil
  1014. }
  1015. r := c.String("stop")
  1016. if r != "true" && r != "false" {
  1017. fmt.Printf("Expect s flag to be a boolean value.\n")
  1018. return nil
  1019. }
  1020. p := c.String("partial")
  1021. if p != "true" && p != "false" {
  1022. fmt.Printf("Expect p flag to be a boolean value.\n")
  1023. return nil
  1024. }
  1025. args := &model.ImportDataDesc{
  1026. FileName: sfile,
  1027. Stop: r == "true",
  1028. Partial: p == "true",
  1029. }
  1030. var reply string
  1031. err = client.Call("Server.ImportConfiguration", args, &reply)
  1032. if err != nil {
  1033. fmt.Println(err)
  1034. } else {
  1035. fmt.Println(reply)
  1036. }
  1037. return nil
  1038. },
  1039. },
  1040. },
  1041. },
  1042. {
  1043. Name: "export",
  1044. Aliases: []string{"export"},
  1045. Usage: "export ruleset | data $ruleset_file [ -r rules ]",
  1046. Subcommands: []cli.Command{
  1047. {
  1048. Name: "ruleset",
  1049. Usage: "\"export ruleset $ruleset_file",
  1050. Action: func(c *cli.Context) error {
  1051. if len(c.Args()) < 1 {
  1052. fmt.Printf("Require exported file name.\n")
  1053. return nil
  1054. }
  1055. var reply string
  1056. err = client.Call("Server.Export", c.Args()[0], &reply)
  1057. if err != nil {
  1058. fmt.Println(err)
  1059. } else {
  1060. fmt.Println(reply)
  1061. }
  1062. return nil
  1063. },
  1064. },
  1065. {
  1066. Name: "data",
  1067. Usage: "export data $configuration_file [ -r rules ]",
  1068. Flags: []cli.Flag{
  1069. cli.StringFlag{
  1070. Name: "rules, r",
  1071. Usage: "the rules id in json array format",
  1072. },
  1073. },
  1074. Action: func(c *cli.Context) error {
  1075. args := model.ExportDataDesc{
  1076. Rules: []string{},
  1077. FileName: "",
  1078. }
  1079. rulesArray := c.String("rules")
  1080. if rulesArray != "" {
  1081. var rules []string
  1082. err := json.Unmarshal([]byte(rulesArray), &rules)
  1083. if err != nil {
  1084. fmt.Printf("rules %s unmarshal error %s", rules, err)
  1085. return nil
  1086. }
  1087. args.Rules = rules
  1088. if len(c.Args()) != 1 {
  1089. fmt.Printf("Expect configuration file.\n")
  1090. return nil
  1091. }
  1092. args.FileName = c.Args()[0]
  1093. var reply string
  1094. err = client.Call("Server.ExportConfiguration", args, &reply)
  1095. if err != nil {
  1096. fmt.Println(err)
  1097. } else {
  1098. fmt.Println(reply)
  1099. }
  1100. } else {
  1101. if len(c.Args()) != 1 {
  1102. fmt.Printf("Expect configuration file.\n")
  1103. return nil
  1104. }
  1105. args.FileName = c.Args()[0]
  1106. var reply string
  1107. err = client.Call("Server.ExportConfiguration", args, &reply)
  1108. if err != nil {
  1109. fmt.Println(err)
  1110. } else {
  1111. fmt.Println(reply)
  1112. }
  1113. }
  1114. return nil
  1115. },
  1116. },
  1117. },
  1118. },
  1119. }
  1120. app.Name = "Kuiper"
  1121. app.Usage = "The command line tool for EMQ X Kuiper."
  1122. app.Action = func(c *cli.Context) error {
  1123. cli.ShowSubcommandHelp(c)
  1124. // cli.ShowVersion(c)
  1125. return nil
  1126. }
  1127. sort.Sort(cli.FlagsByName(app.Flags))
  1128. sort.Sort(cli.CommandsByName(app.Commands))
  1129. err = app.Run(os.Args)
  1130. if err != nil {
  1131. fmt.Printf("%v", err)
  1132. }
  1133. }
  1134. func getPluginType(arg string) (ptype int, err error) {
  1135. switch arg {
  1136. case "source":
  1137. ptype = 0
  1138. case "sink":
  1139. ptype = 1
  1140. case "function":
  1141. ptype = 2
  1142. case "portable":
  1143. ptype = 3
  1144. case "wasm":
  1145. ptype = 4
  1146. default:
  1147. err = fmt.Errorf("Invalid plugin type %s, should be \"source\", \"sink\", \"function\" or \"portable\" or \"wasm\".\n", arg)
  1148. }
  1149. return
  1150. }
  1151. func readDef(sfile string, t string) ([]byte, error) {
  1152. if _, err := os.Stat(sfile); os.IsNotExist(err) {
  1153. return nil, fmt.Errorf("The specified %s defenition file %s is not existed.\n", t, sfile)
  1154. }
  1155. fmt.Printf("Creating a new %s from file %s.\n", t, sfile)
  1156. if rule, err := os.ReadFile(sfile); err != nil {
  1157. return nil, fmt.Errorf("Failed to read from %s definition file %s.\n", t, sfile)
  1158. } else {
  1159. return rule, nil
  1160. }
  1161. }