main.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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. Name: "services",
  701. Usage: "show services",
  702. Action: func(c *cli.Context) error {
  703. var reply string
  704. err = client.Call("Server.ShowServices", 0, &reply)
  705. if err != nil {
  706. fmt.Println(err)
  707. } else {
  708. fmt.Println(reply)
  709. }
  710. return nil
  711. },
  712. }, {
  713. Name: "service_funcs",
  714. Usage: "show service_funcs",
  715. Action: func(c *cli.Context) error {
  716. var reply string
  717. err = client.Call("Server.ShowServiceFuncs", 0, &reply)
  718. if err != nil {
  719. fmt.Println(err)
  720. } else {
  721. fmt.Println(reply)
  722. }
  723. return nil
  724. },
  725. }, {
  726. Name: "schemas",
  727. Usage: "show schemas $schema_type",
  728. Action: func(c *cli.Context) error {
  729. if len(c.Args()) != 1 {
  730. fmt.Printf("Expect schema type.\n")
  731. return nil
  732. }
  733. var reply string
  734. err = client.Call("Server.ShowSchemas", c.Args()[0], &reply)
  735. if err != nil {
  736. fmt.Println(err)
  737. } else {
  738. fmt.Println(reply)
  739. }
  740. return nil
  741. },
  742. },
  743. },
  744. },
  745. {
  746. Name: "getstatus",
  747. Aliases: []string{"getstatus"},
  748. Usage: "getstatus rule $rule_name | import",
  749. Subcommands: []cli.Command{
  750. {
  751. Name: "rule",
  752. Usage: "getstatus rule $rule_name",
  753. //Flags: nflag,
  754. Action: func(c *cli.Context) error {
  755. if len(c.Args()) != 1 {
  756. fmt.Printf("Expect rule name.\n")
  757. return nil
  758. }
  759. rname := c.Args()[0]
  760. var reply string
  761. err = client.Call("Server.GetStatusRule", rname, &reply)
  762. if err != nil {
  763. fmt.Println(err)
  764. } else {
  765. fmt.Println(reply)
  766. }
  767. return nil
  768. },
  769. },
  770. {
  771. Name: "import",
  772. Usage: "getstatus import",
  773. //Flags: nflag,
  774. Action: func(c *cli.Context) error {
  775. var reply string
  776. err = client.Call("Server.GetStatusImport", 0, &reply)
  777. if err != nil {
  778. fmt.Println(err)
  779. } else {
  780. fmt.Println(reply)
  781. }
  782. return nil
  783. },
  784. },
  785. },
  786. },
  787. {
  788. Name: "gettopo",
  789. Aliases: []string{"gettopo"},
  790. Usage: "gettopo rule $rule_name",
  791. Subcommands: []cli.Command{
  792. {
  793. Name: "rule",
  794. Usage: "getstopo rule $rule_name",
  795. //Flags: nflag,
  796. Action: func(c *cli.Context) error {
  797. if len(c.Args()) != 1 {
  798. fmt.Printf("Expect rule name.\n")
  799. return nil
  800. }
  801. rname := c.Args()[0]
  802. var reply string
  803. err = client.Call("Server.GetTopoRule", rname, &reply)
  804. if err != nil {
  805. fmt.Println(err)
  806. } else {
  807. fmt.Println(reply)
  808. }
  809. return nil
  810. },
  811. },
  812. },
  813. },
  814. {
  815. Name: "start",
  816. Aliases: []string{"start"},
  817. Usage: "start rule $rule_name",
  818. Subcommands: []cli.Command{
  819. {
  820. Name: "rule",
  821. Usage: "start rule $rule_name",
  822. //Flags: nflag,
  823. Action: func(c *cli.Context) error {
  824. if len(c.Args()) != 1 {
  825. fmt.Printf("Expect rule name.\n")
  826. return nil
  827. }
  828. rname := c.Args()[0]
  829. var reply string
  830. err = client.Call("Server.StartRule", rname, &reply)
  831. if err != nil {
  832. fmt.Println(err)
  833. } else {
  834. fmt.Println(reply)
  835. }
  836. return nil
  837. },
  838. },
  839. },
  840. },
  841. {
  842. Name: "stop",
  843. Aliases: []string{"stop"},
  844. Usage: "stop rule $rule_name",
  845. Subcommands: []cli.Command{
  846. {
  847. Name: "rule",
  848. Usage: "stop rule $rule_name",
  849. //Flags: nflag,
  850. Action: func(c *cli.Context) error {
  851. if len(c.Args()) != 1 {
  852. fmt.Printf("Expect rule name.\n")
  853. return nil
  854. }
  855. rname := c.Args()[0]
  856. var reply string
  857. err = client.Call("Server.StopRule", rname, &reply)
  858. if err != nil {
  859. fmt.Println(err)
  860. } else {
  861. fmt.Println(reply)
  862. }
  863. return nil
  864. },
  865. },
  866. },
  867. },
  868. {
  869. Name: "restart",
  870. Aliases: []string{"restart"},
  871. Usage: "restart rule $rule_name",
  872. Subcommands: []cli.Command{
  873. {
  874. Name: "rule",
  875. Usage: "restart rule $rule_name",
  876. //Flags: nflag,
  877. Action: func(c *cli.Context) error {
  878. if len(c.Args()) != 1 {
  879. fmt.Printf("Expect rule name.\n")
  880. return nil
  881. }
  882. rname := c.Args()[0]
  883. var reply string
  884. err = client.Call("Server.RestartRule", rname, &reply)
  885. if err != nil {
  886. fmt.Println(err)
  887. } else {
  888. fmt.Println(reply)
  889. }
  890. return nil
  891. },
  892. },
  893. },
  894. },
  895. {
  896. Name: "register",
  897. Aliases: []string{"register"},
  898. Usage: "register plugin function $plugin_name [$plugin_json | -f plugin_def_file]",
  899. Subcommands: []cli.Command{
  900. {
  901. Name: "plugin",
  902. Usage: "register plugin $plugin_type $plugin_name [$plugin_json | -f plugin_def_file]",
  903. Flags: []cli.Flag{
  904. cli.StringFlag{
  905. Name: "file, f",
  906. Usage: "the location of plugin functions definition file",
  907. FilePath: "/home/myplugin.txt",
  908. },
  909. },
  910. Action: func(c *cli.Context) error {
  911. if len(c.Args()) < 2 {
  912. fmt.Printf("Expect plugin type and name.\n")
  913. return nil
  914. }
  915. ptype := c.Args()[0]
  916. if !strings.EqualFold(ptype, "function") {
  917. fmt.Printf("Plugin type must be function.\n")
  918. return nil
  919. }
  920. pname := c.Args()[1]
  921. sfile := c.String("file")
  922. args := &model.PluginDesc{
  923. RPCArgDesc: model.RPCArgDesc{
  924. Name: pname,
  925. },
  926. }
  927. if sfile != "" {
  928. if len(c.Args()) != 2 {
  929. fmt.Printf("Expect plugin type, name.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  930. return nil
  931. }
  932. if p, err := readDef(sfile, "plugin"); err != nil {
  933. fmt.Printf("%s", err)
  934. return nil
  935. } else {
  936. args.Json = string(p)
  937. }
  938. } else {
  939. if len(c.Args()) != 3 {
  940. fmt.Printf("Expect plugin type, name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  941. return nil
  942. }
  943. args.Json = c.Args()[2]
  944. }
  945. var reply string
  946. err = client.Call("Server.RegisterPlugin", args, &reply)
  947. if err != nil {
  948. fmt.Println(err)
  949. } else {
  950. fmt.Println(reply)
  951. }
  952. return nil
  953. },
  954. },
  955. },
  956. },
  957. {
  958. Name: "import",
  959. Aliases: []string{"import"},
  960. Usage: "import ruleset | data -f file -p partial -s stop",
  961. Subcommands: []cli.Command{
  962. {
  963. Name: "ruleset",
  964. Usage: "\"import ruleset -f ruleset_file",
  965. Flags: []cli.Flag{
  966. cli.StringFlag{
  967. Name: "file, f",
  968. Usage: "the location of the ruleset json file",
  969. FilePath: "/home/ekuiper_ruleset.json",
  970. },
  971. },
  972. Action: func(c *cli.Context) error {
  973. sfile := c.String("file")
  974. if sfile == "" {
  975. fmt.Print("Required ruleset json file to import")
  976. return nil
  977. }
  978. var reply string
  979. err = client.Call("Server.Import", sfile, &reply)
  980. if err != nil {
  981. fmt.Println(err)
  982. } else {
  983. fmt.Println(reply)
  984. }
  985. return nil
  986. },
  987. },
  988. {
  989. Name: "data",
  990. Usage: "\"import data -f configuration_file -p partial -s stop",
  991. Flags: []cli.Flag{
  992. cli.StringFlag{
  993. Name: "file, f",
  994. Usage: "the location of the configuration json file",
  995. FilePath: "/home/ekuiper_configuration.json",
  996. },
  997. cli.StringFlag{
  998. Name: "stop, s",
  999. Usage: "stop kuiper after the action",
  1000. },
  1001. cli.StringFlag{
  1002. Name: "partial, p",
  1003. Usage: "import partial configuration",
  1004. },
  1005. },
  1006. Action: func(c *cli.Context) error {
  1007. sfile := c.String("file")
  1008. if sfile == "" {
  1009. fmt.Print("Required configuration json file to import")
  1010. return nil
  1011. }
  1012. r := c.String("stop")
  1013. if r != "true" && r != "false" {
  1014. fmt.Printf("Expect s flag to be a boolean value.\n")
  1015. return nil
  1016. }
  1017. p := c.String("partial")
  1018. if p != "true" && p != "false" {
  1019. fmt.Printf("Expect p flag to be a boolean value.\n")
  1020. return nil
  1021. }
  1022. args := &model.ImportDataDesc{
  1023. FileName: sfile,
  1024. Stop: r == "true",
  1025. Partial: p == "true",
  1026. }
  1027. var reply string
  1028. err = client.Call("Server.ImportConfiguration", args, &reply)
  1029. if err != nil {
  1030. fmt.Println(err)
  1031. } else {
  1032. fmt.Println(reply)
  1033. }
  1034. return nil
  1035. },
  1036. },
  1037. },
  1038. },
  1039. {
  1040. Name: "export",
  1041. Aliases: []string{"export"},
  1042. Usage: "export ruleset | data $ruleset_file [ -r rules ]",
  1043. Subcommands: []cli.Command{
  1044. {
  1045. Name: "ruleset",
  1046. Usage: "\"export ruleset $ruleset_file",
  1047. Action: func(c *cli.Context) error {
  1048. if len(c.Args()) < 1 {
  1049. fmt.Printf("Require exported file name.\n")
  1050. return nil
  1051. }
  1052. var reply string
  1053. err = client.Call("Server.Export", c.Args()[0], &reply)
  1054. if err != nil {
  1055. fmt.Println(err)
  1056. } else {
  1057. fmt.Println(reply)
  1058. }
  1059. return nil
  1060. },
  1061. },
  1062. {
  1063. Name: "data",
  1064. Usage: "export data $configuration_file [ -r rules ]",
  1065. Flags: []cli.Flag{
  1066. cli.StringFlag{
  1067. Name: "rules, r",
  1068. Usage: "the rules id in json array format",
  1069. },
  1070. },
  1071. Action: func(c *cli.Context) error {
  1072. args := model.ExportDataDesc{
  1073. Rules: []string{},
  1074. FileName: "",
  1075. }
  1076. rulesArray := c.String("rules")
  1077. if rulesArray != "" {
  1078. var rules []string
  1079. err := json.Unmarshal([]byte(rulesArray), &rules)
  1080. if err != nil {
  1081. fmt.Printf("rules %s unmarshal error %s", rules, err)
  1082. return nil
  1083. }
  1084. args.Rules = rules
  1085. if len(c.Args()) != 1 {
  1086. fmt.Printf("Expect configuration file.\n")
  1087. return nil
  1088. }
  1089. args.FileName = c.Args()[0]
  1090. var reply string
  1091. err = client.Call("Server.ExportConfiguration", args, &reply)
  1092. if err != nil {
  1093. fmt.Println(err)
  1094. } else {
  1095. fmt.Println(reply)
  1096. }
  1097. } else {
  1098. if len(c.Args()) != 1 {
  1099. fmt.Printf("Expect configuration file.\n")
  1100. return nil
  1101. }
  1102. args.FileName = c.Args()[0]
  1103. var reply string
  1104. err = client.Call("Server.ExportConfiguration", args, &reply)
  1105. if err != nil {
  1106. fmt.Println(err)
  1107. } else {
  1108. fmt.Println(reply)
  1109. }
  1110. }
  1111. return nil
  1112. },
  1113. },
  1114. },
  1115. },
  1116. }
  1117. app.Name = "Kuiper"
  1118. app.Usage = "The command line tool for EMQ X Kuiper."
  1119. app.Action = func(c *cli.Context) error {
  1120. cli.ShowSubcommandHelp(c)
  1121. //cli.ShowVersion(c)
  1122. return nil
  1123. }
  1124. sort.Sort(cli.FlagsByName(app.Flags))
  1125. sort.Sort(cli.CommandsByName(app.Commands))
  1126. err = app.Run(os.Args)
  1127. if err != nil {
  1128. fmt.Printf("%v", err)
  1129. }
  1130. }
  1131. func getPluginType(arg string) (ptype int, err error) {
  1132. switch arg {
  1133. case "source":
  1134. ptype = 0
  1135. case "sink":
  1136. ptype = 1
  1137. case "function":
  1138. ptype = 2
  1139. case "portable":
  1140. ptype = 3
  1141. case "wasm":
  1142. ptype = 4
  1143. default:
  1144. err = fmt.Errorf("Invalid plugin type %s, should be \"source\", \"sink\", \"function\" or \"portable\" or \"wasm\".\n", arg)
  1145. }
  1146. return
  1147. }
  1148. func readDef(sfile string, t string) ([]byte, error) {
  1149. if _, err := os.Stat(sfile); os.IsNotExist(err) {
  1150. return nil, fmt.Errorf("The specified %s defenition file %s is not existed.\n", t, sfile)
  1151. }
  1152. fmt.Printf("Creating a new %s from file %s.\n", t, sfile)
  1153. if rule, err := os.ReadFile(sfile); err != nil {
  1154. return nil, fmt.Errorf("Failed to read from %s definition file %s.\n", t, sfile)
  1155. } else {
  1156. return rule, nil
  1157. }
  1158. }