main.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. "encoding/json"
  18. "fmt"
  19. "net/rpc"
  20. "os"
  21. "sort"
  22. "strings"
  23. "time"
  24. "github.com/lf-edge/ekuiper/internal/conf"
  25. "github.com/lf-edge/ekuiper/internal/pkg/model"
  26. "github.com/lf-edge/ekuiper/pkg/infra"
  27. "github.com/urfave/cli"
  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. var inputs []string
  92. ticker := time.NewTicker(time.Millisecond * 300)
  93. defer ticker.Stop()
  94. for {
  95. fmt.Print("kuiper > ")
  96. text, _ := reader.ReadString('\n')
  97. inputs = append(inputs, text)
  98. // convert CRLF to LF
  99. text = strings.Replace(text, "\n", "", -1)
  100. if strings.EqualFold(text, "quit") || strings.EqualFold(text, "exit") {
  101. break
  102. } else if strings.Trim(text, " ") == "" {
  103. continue
  104. } else {
  105. var reply string
  106. err := client.Call("Server.CreateQuery", text, &reply)
  107. if err != nil {
  108. fmt.Println(err)
  109. continue
  110. }
  111. fmt.Println(reply)
  112. go func() {
  113. err := infra.SafeRun(func() error {
  114. for {
  115. <-ticker.C
  116. var result string
  117. e := client.Call("Server.GetQueryResult", "", &result)
  118. if e != nil {
  119. return e
  120. }
  121. if result != "" {
  122. fmt.Println(result)
  123. }
  124. }
  125. })
  126. if err != nil {
  127. fmt.Println(err)
  128. fmt.Print("kuiper > ")
  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 | import",
  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. Name: "import",
  774. Usage: "getstatus import",
  775. //Flags: nflag,
  776. Action: func(c *cli.Context) error {
  777. var reply string
  778. err = client.Call("Server.GetStatusImport", 0, &reply)
  779. if err != nil {
  780. fmt.Println(err)
  781. } else {
  782. fmt.Println(reply)
  783. }
  784. return nil
  785. },
  786. },
  787. },
  788. },
  789. {
  790. Name: "gettopo",
  791. Aliases: []string{"gettopo"},
  792. Usage: "gettopo rule $rule_name",
  793. Subcommands: []cli.Command{
  794. {
  795. Name: "rule",
  796. Usage: "getstopo rule $rule_name",
  797. //Flags: nflag,
  798. Action: func(c *cli.Context) error {
  799. if len(c.Args()) != 1 {
  800. fmt.Printf("Expect rule name.\n")
  801. return nil
  802. }
  803. rname := c.Args()[0]
  804. var reply string
  805. err = client.Call("Server.GetTopoRule", rname, &reply)
  806. if err != nil {
  807. fmt.Println(err)
  808. } else {
  809. fmt.Println(reply)
  810. }
  811. return nil
  812. },
  813. },
  814. },
  815. },
  816. {
  817. Name: "start",
  818. Aliases: []string{"start"},
  819. Usage: "start rule $rule_name",
  820. Subcommands: []cli.Command{
  821. {
  822. Name: "rule",
  823. Usage: "start rule $rule_name",
  824. //Flags: nflag,
  825. Action: func(c *cli.Context) error {
  826. if len(c.Args()) != 1 {
  827. fmt.Printf("Expect rule name.\n")
  828. return nil
  829. }
  830. rname := c.Args()[0]
  831. var reply string
  832. err = client.Call("Server.StartRule", rname, &reply)
  833. if err != nil {
  834. fmt.Println(err)
  835. } else {
  836. fmt.Println(reply)
  837. }
  838. return nil
  839. },
  840. },
  841. },
  842. },
  843. {
  844. Name: "stop",
  845. Aliases: []string{"stop"},
  846. Usage: "stop rule $rule_name",
  847. Subcommands: []cli.Command{
  848. {
  849. Name: "rule",
  850. Usage: "stop rule $rule_name",
  851. //Flags: nflag,
  852. Action: func(c *cli.Context) error {
  853. if len(c.Args()) != 1 {
  854. fmt.Printf("Expect rule name.\n")
  855. return nil
  856. }
  857. rname := c.Args()[0]
  858. var reply string
  859. err = client.Call("Server.StopRule", rname, &reply)
  860. if err != nil {
  861. fmt.Println(err)
  862. } else {
  863. fmt.Println(reply)
  864. }
  865. return nil
  866. },
  867. },
  868. },
  869. },
  870. {
  871. Name: "restart",
  872. Aliases: []string{"restart"},
  873. Usage: "restart rule $rule_name",
  874. Subcommands: []cli.Command{
  875. {
  876. Name: "rule",
  877. Usage: "restart rule $rule_name",
  878. //Flags: nflag,
  879. Action: func(c *cli.Context) error {
  880. if len(c.Args()) != 1 {
  881. fmt.Printf("Expect rule name.\n")
  882. return nil
  883. }
  884. rname := c.Args()[0]
  885. var reply string
  886. err = client.Call("Server.RestartRule", rname, &reply)
  887. if err != nil {
  888. fmt.Println(err)
  889. } else {
  890. fmt.Println(reply)
  891. }
  892. return nil
  893. },
  894. },
  895. },
  896. },
  897. {
  898. Name: "register",
  899. Aliases: []string{"register"},
  900. Usage: "register plugin function $plugin_name [$plugin_json | -f plugin_def_file]",
  901. Subcommands: []cli.Command{
  902. {
  903. Name: "plugin",
  904. Usage: "register plugin $plugin_type $plugin_name [$plugin_json | -f plugin_def_file]",
  905. Flags: []cli.Flag{
  906. cli.StringFlag{
  907. Name: "file, f",
  908. Usage: "the location of plugin functions definition file",
  909. FilePath: "/home/myplugin.txt",
  910. },
  911. },
  912. Action: func(c *cli.Context) error {
  913. if len(c.Args()) < 2 {
  914. fmt.Printf("Expect plugin type and name.\n")
  915. return nil
  916. }
  917. ptype := c.Args()[0]
  918. if !strings.EqualFold(ptype, "function") {
  919. fmt.Printf("Plugin type must be function.\n")
  920. return nil
  921. }
  922. pname := c.Args()[1]
  923. sfile := c.String("file")
  924. args := &model.PluginDesc{
  925. RPCArgDesc: model.RPCArgDesc{
  926. Name: pname,
  927. },
  928. }
  929. if sfile != "" {
  930. if len(c.Args()) != 2 {
  931. fmt.Printf("Expect plugin type, name.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  932. return nil
  933. }
  934. if p, err := readDef(sfile, "plugin"); err != nil {
  935. fmt.Printf("%s", err)
  936. return nil
  937. } else {
  938. args.Json = string(p)
  939. }
  940. } else {
  941. if len(c.Args()) != 3 {
  942. fmt.Printf("Expect plugin type, name and json.\nBut found %d args:%s.\n", len(c.Args()), c.Args())
  943. return nil
  944. }
  945. args.Json = c.Args()[2]
  946. }
  947. var reply string
  948. err = client.Call("Server.RegisterPlugin", args, &reply)
  949. if err != nil {
  950. fmt.Println(err)
  951. } else {
  952. fmt.Println(reply)
  953. }
  954. return nil
  955. },
  956. },
  957. },
  958. },
  959. {
  960. Name: "import",
  961. Aliases: []string{"import"},
  962. Usage: "import ruleset | data -f file -p partial -s stop",
  963. Subcommands: []cli.Command{
  964. {
  965. Name: "ruleset",
  966. Usage: "\"import ruleset -f ruleset_file",
  967. Flags: []cli.Flag{
  968. cli.StringFlag{
  969. Name: "file, f",
  970. Usage: "the location of the ruleset json file",
  971. FilePath: "/home/ekuiper_ruleset.json",
  972. },
  973. },
  974. Action: func(c *cli.Context) error {
  975. sfile := c.String("file")
  976. if sfile == "" {
  977. fmt.Print("Required ruleset json file to import")
  978. return nil
  979. }
  980. var reply string
  981. err = client.Call("Server.Import", sfile, &reply)
  982. if err != nil {
  983. fmt.Println(err)
  984. } else {
  985. fmt.Println(reply)
  986. }
  987. return nil
  988. },
  989. },
  990. {
  991. Name: "data",
  992. Usage: "\"import data -f configuration_file -p partial -s stop",
  993. Flags: []cli.Flag{
  994. cli.StringFlag{
  995. Name: "file, f",
  996. Usage: "the location of the configuration json file",
  997. FilePath: "/home/ekuiper_configuration.json",
  998. },
  999. cli.StringFlag{
  1000. Name: "stop, s",
  1001. Usage: "stop kuiper after the action",
  1002. },
  1003. cli.StringFlag{
  1004. Name: "partial, p",
  1005. Usage: "import partial configuration",
  1006. },
  1007. },
  1008. Action: func(c *cli.Context) error {
  1009. sfile := c.String("file")
  1010. if sfile == "" {
  1011. fmt.Print("Required configuration json file to import")
  1012. return nil
  1013. }
  1014. r := c.String("stop")
  1015. if r != "true" && r != "false" {
  1016. fmt.Printf("Expect s flag to be a boolean value.\n")
  1017. return nil
  1018. }
  1019. p := c.String("partial")
  1020. if p != "true" && p != "false" {
  1021. fmt.Printf("Expect p flag to be a boolean value.\n")
  1022. return nil
  1023. }
  1024. args := &model.ImportDataDesc{
  1025. FileName: sfile,
  1026. Stop: r == "true",
  1027. Partial: p == "true",
  1028. }
  1029. var reply string
  1030. err = client.Call("Server.ImportConfiguration", args, &reply)
  1031. if err != nil {
  1032. fmt.Println(err)
  1033. } else {
  1034. fmt.Println(reply)
  1035. }
  1036. return nil
  1037. },
  1038. },
  1039. },
  1040. },
  1041. {
  1042. Name: "export",
  1043. Aliases: []string{"export"},
  1044. Usage: "export ruleset | data $ruleset_file [ -r rules ]",
  1045. Subcommands: []cli.Command{
  1046. {
  1047. Name: "ruleset",
  1048. Usage: "\"export ruleset $ruleset_file",
  1049. Action: func(c *cli.Context) error {
  1050. if len(c.Args()) < 1 {
  1051. fmt.Printf("Require exported file name.\n")
  1052. return nil
  1053. }
  1054. var reply string
  1055. err = client.Call("Server.Export", c.Args()[0], &reply)
  1056. if err != nil {
  1057. fmt.Println(err)
  1058. } else {
  1059. fmt.Println(reply)
  1060. }
  1061. return nil
  1062. },
  1063. },
  1064. {
  1065. Name: "data",
  1066. Usage: "export data $configuration_file [ -r rules ]",
  1067. Flags: []cli.Flag{
  1068. cli.StringFlag{
  1069. Name: "rules, r",
  1070. Usage: "the rules id in json array format",
  1071. },
  1072. },
  1073. Action: func(c *cli.Context) error {
  1074. args := model.ExportDataDesc{
  1075. Rules: []string{},
  1076. FileName: "",
  1077. }
  1078. rulesArray := c.String("rules")
  1079. if rulesArray != "" {
  1080. var rules []string
  1081. err := json.Unmarshal([]byte(rulesArray), &rules)
  1082. if err != nil {
  1083. fmt.Printf("rules %s unmarshal error %s", rules, err)
  1084. return nil
  1085. }
  1086. args.Rules = rules
  1087. if len(c.Args()) != 1 {
  1088. fmt.Printf("Expect configuration file.\n")
  1089. return nil
  1090. }
  1091. args.FileName = c.Args()[0]
  1092. var reply string
  1093. err = client.Call("Server.ExportConfiguration", args, &reply)
  1094. if err != nil {
  1095. fmt.Println(err)
  1096. } else {
  1097. fmt.Println(reply)
  1098. }
  1099. } else {
  1100. if len(c.Args()) != 1 {
  1101. fmt.Printf("Expect configuration file.\n")
  1102. return nil
  1103. }
  1104. args.FileName = c.Args()[0]
  1105. var reply string
  1106. err = client.Call("Server.ExportConfiguration", args, &reply)
  1107. if err != nil {
  1108. fmt.Println(err)
  1109. } else {
  1110. fmt.Println(reply)
  1111. }
  1112. }
  1113. return nil
  1114. },
  1115. },
  1116. },
  1117. },
  1118. }
  1119. app.Name = "Kuiper"
  1120. app.Usage = "The command line tool for EMQ X Kuiper."
  1121. app.Action = func(c *cli.Context) error {
  1122. cli.ShowSubcommandHelp(c)
  1123. //cli.ShowVersion(c)
  1124. return nil
  1125. }
  1126. sort.Sort(cli.FlagsByName(app.Flags))
  1127. sort.Sort(cli.CommandsByName(app.Commands))
  1128. err = app.Run(os.Args)
  1129. if err != nil {
  1130. fmt.Printf("%v", err)
  1131. }
  1132. }
  1133. func getPluginType(arg string) (ptype int, err error) {
  1134. switch arg {
  1135. case "source":
  1136. ptype = 0
  1137. case "sink":
  1138. ptype = 1
  1139. case "function":
  1140. ptype = 2
  1141. case "portable":
  1142. ptype = 3
  1143. case "wasm":
  1144. ptype = 4
  1145. default:
  1146. err = fmt.Errorf("Invalid plugin type %s, should be \"source\", \"sink\", \"function\" or \"portable\" or \"wasm\".\n", arg)
  1147. }
  1148. return
  1149. }
  1150. func readDef(sfile string, t string) ([]byte, error) {
  1151. if _, err := os.Stat(sfile); os.IsNotExist(err) {
  1152. return nil, fmt.Errorf("The specified %s defenition file %s is not existed.\n", t, sfile)
  1153. }
  1154. fmt.Printf("Creating a new %s from file %s.\n", t, sfile)
  1155. if rule, err := os.ReadFile(sfile); err != nil {
  1156. return nil, fmt.Errorf("Failed to read from %s definition file %s.\n", t, sfile)
  1157. } else {
  1158. return rule, nil
  1159. }
  1160. }