main.go 30 KB

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