goctl.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "github.com/logrusorgru/aurora"
  8. "github.com/tal-tech/go-zero/core/load"
  9. "github.com/tal-tech/go-zero/core/logx"
  10. "github.com/tal-tech/go-zero/core/stat"
  11. "github.com/tal-tech/go-zero/tools/goctl/api/apigen"
  12. "github.com/tal-tech/go-zero/tools/goctl/api/dartgen"
  13. "github.com/tal-tech/go-zero/tools/goctl/api/docgen"
  14. "github.com/tal-tech/go-zero/tools/goctl/api/format"
  15. "github.com/tal-tech/go-zero/tools/goctl/api/gogen"
  16. "github.com/tal-tech/go-zero/tools/goctl/api/javagen"
  17. "github.com/tal-tech/go-zero/tools/goctl/api/ktgen"
  18. "github.com/tal-tech/go-zero/tools/goctl/api/new"
  19. "github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
  20. "github.com/tal-tech/go-zero/tools/goctl/api/validate"
  21. "github.com/tal-tech/go-zero/tools/goctl/docker"
  22. "github.com/tal-tech/go-zero/tools/goctl/internal/errorx"
  23. "github.com/tal-tech/go-zero/tools/goctl/internal/version"
  24. "github.com/tal-tech/go-zero/tools/goctl/kube"
  25. "github.com/tal-tech/go-zero/tools/goctl/model/mongo"
  26. model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
  27. "github.com/tal-tech/go-zero/tools/goctl/plugin"
  28. rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/cli"
  29. "github.com/tal-tech/go-zero/tools/goctl/tpl"
  30. "github.com/tal-tech/go-zero/tools/goctl/upgrade"
  31. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  32. "github.com/tal-tech/go-zero/tools/goctl/util/env"
  33. "github.com/urfave/cli"
  34. pluginCtl "github.com/zeromicro/protobuf/protoc-gen-go"
  35. )
  36. var commands = []cli.Command{
  37. {
  38. Name: "upgrade",
  39. Usage: "upgrade goctl to latest version",
  40. Action: upgrade.Upgrade,
  41. },
  42. {
  43. Name: "api",
  44. Usage: "generate api related files",
  45. Flags: []cli.Flag{
  46. cli.StringFlag{
  47. Name: "o",
  48. Usage: "the output api file",
  49. },
  50. cli.StringFlag{
  51. Name: "home",
  52. Usage: "the goctl home path of the template",
  53. },
  54. },
  55. Action: apigen.ApiCommand,
  56. Subcommands: []cli.Command{
  57. {
  58. Name: "new",
  59. Usage: "fast create api service",
  60. Action: new.CreateServiceCommand,
  61. Flags: []cli.Flag{
  62. cli.StringFlag{
  63. Name: "home",
  64. Usage: "the goctl home path of the template",
  65. },
  66. },
  67. },
  68. {
  69. Name: "format",
  70. Usage: "format api files",
  71. Flags: []cli.Flag{
  72. cli.StringFlag{
  73. Name: "dir",
  74. Usage: "the format target dir",
  75. },
  76. cli.BoolFlag{
  77. Name: "iu",
  78. Usage: "ignore update",
  79. },
  80. cli.BoolFlag{
  81. Name: "stdin",
  82. Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
  83. },
  84. },
  85. Action: format.GoFormatApi,
  86. },
  87. {
  88. Name: "validate",
  89. Usage: "validate api file",
  90. Flags: []cli.Flag{
  91. cli.StringFlag{
  92. Name: "api",
  93. Usage: "validate target api file",
  94. },
  95. },
  96. Action: validate.GoValidateApi,
  97. },
  98. {
  99. Name: "doc",
  100. Usage: "generate doc files",
  101. Flags: []cli.Flag{
  102. cli.StringFlag{
  103. Name: "dir",
  104. Usage: "the target dir",
  105. },
  106. cli.StringFlag{
  107. Name: "o",
  108. Required: false,
  109. Usage: "the output markdown directory",
  110. },
  111. },
  112. Action: docgen.DocCommand,
  113. },
  114. {
  115. Name: "go",
  116. Usage: "generate go files for provided api in yaml file",
  117. Flags: []cli.Flag{
  118. cli.StringFlag{
  119. Name: "dir",
  120. Usage: "the target dir",
  121. },
  122. cli.StringFlag{
  123. Name: "api",
  124. Usage: "the api file",
  125. },
  126. cli.StringFlag{
  127. Name: "style",
  128. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  129. },
  130. cli.StringFlag{
  131. Name: "home",
  132. Usage: "the goctl home path of the template",
  133. },
  134. },
  135. Action: gogen.GoCommand,
  136. },
  137. {
  138. Name: "java",
  139. Usage: "generate java files for provided api in api file",
  140. Flags: []cli.Flag{
  141. cli.StringFlag{
  142. Name: "dir",
  143. Usage: "the target dir",
  144. },
  145. cli.StringFlag{
  146. Name: "api",
  147. Usage: "the api file",
  148. },
  149. },
  150. Action: javagen.JavaCommand,
  151. },
  152. {
  153. Name: "ts",
  154. Usage: "generate ts files for provided api in api file",
  155. Flags: []cli.Flag{
  156. cli.StringFlag{
  157. Name: "dir",
  158. Usage: "the target dir",
  159. },
  160. cli.StringFlag{
  161. Name: "api",
  162. Usage: "the api file",
  163. },
  164. cli.StringFlag{
  165. Name: "webapi",
  166. Usage: "the web api file path",
  167. },
  168. cli.StringFlag{
  169. Name: "caller",
  170. Usage: "the web api caller",
  171. },
  172. cli.BoolFlag{
  173. Name: "unwrap",
  174. Usage: "unwrap the webapi caller for import",
  175. },
  176. },
  177. Action: tsgen.TsCommand,
  178. },
  179. {
  180. Name: "dart",
  181. Usage: "generate dart files for provided api in api file",
  182. Flags: []cli.Flag{
  183. cli.StringFlag{
  184. Name: "dir",
  185. Usage: "the target dir",
  186. },
  187. cli.StringFlag{
  188. Name: "api",
  189. Usage: "the api file",
  190. },
  191. },
  192. Action: dartgen.DartCommand,
  193. },
  194. {
  195. Name: "kt",
  196. Usage: "generate kotlin code for provided api file",
  197. Flags: []cli.Flag{
  198. cli.StringFlag{
  199. Name: "dir",
  200. Usage: "the target directory",
  201. },
  202. cli.StringFlag{
  203. Name: "api",
  204. Usage: "the api file",
  205. },
  206. cli.StringFlag{
  207. Name: "pkg",
  208. Usage: "define package name for kotlin file",
  209. },
  210. },
  211. Action: ktgen.KtCommand,
  212. },
  213. {
  214. Name: "plugin",
  215. Usage: "custom file generator",
  216. Flags: []cli.Flag{
  217. cli.StringFlag{
  218. Name: "plugin, p",
  219. Usage: "the plugin file",
  220. },
  221. cli.StringFlag{
  222. Name: "dir",
  223. Usage: "the target directory",
  224. },
  225. cli.StringFlag{
  226. Name: "api",
  227. Usage: "the api file",
  228. },
  229. cli.StringFlag{
  230. Name: "style",
  231. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  232. },
  233. },
  234. Action: plugin.PluginCommand,
  235. },
  236. },
  237. },
  238. {
  239. Name: "docker",
  240. Usage: "generate Dockerfile",
  241. Flags: []cli.Flag{
  242. cli.StringFlag{
  243. Name: "go",
  244. Usage: "the file that contains main function",
  245. },
  246. cli.IntFlag{
  247. Name: "port",
  248. Usage: "the port to expose, default none",
  249. Value: 0,
  250. },
  251. cli.StringFlag{
  252. Name: "home",
  253. Usage: "the goctl home path of the template",
  254. },
  255. },
  256. Action: docker.DockerCommand,
  257. },
  258. {
  259. Name: "kube",
  260. Usage: "generate kubernetes files",
  261. Subcommands: []cli.Command{
  262. {
  263. Name: "deploy",
  264. Usage: "generate deployment yaml file",
  265. Flags: []cli.Flag{
  266. cli.StringFlag{
  267. Name: "name",
  268. Usage: "the name of deployment",
  269. Required: true,
  270. },
  271. cli.StringFlag{
  272. Name: "namespace",
  273. Usage: "the namespace of deployment",
  274. Required: true,
  275. },
  276. cli.StringFlag{
  277. Name: "image",
  278. Usage: "the docker image of deployment",
  279. Required: true,
  280. },
  281. cli.StringFlag{
  282. Name: "secret",
  283. Usage: "the secret to image pull from registry",
  284. },
  285. cli.IntFlag{
  286. Name: "requestCpu",
  287. Usage: "the request cpu to deploy",
  288. Value: 500,
  289. },
  290. cli.IntFlag{
  291. Name: "requestMem",
  292. Usage: "the request memory to deploy",
  293. Value: 512,
  294. },
  295. cli.IntFlag{
  296. Name: "limitCpu",
  297. Usage: "the limit cpu to deploy",
  298. Value: 1000,
  299. },
  300. cli.IntFlag{
  301. Name: "limitMem",
  302. Usage: "the limit memory to deploy",
  303. Value: 1024,
  304. },
  305. cli.StringFlag{
  306. Name: "o",
  307. Usage: "the output yaml file",
  308. Required: true,
  309. },
  310. cli.IntFlag{
  311. Name: "replicas",
  312. Usage: "the number of replicas to deploy",
  313. Value: 3,
  314. },
  315. cli.IntFlag{
  316. Name: "revisions",
  317. Usage: "the number of revision history to limit",
  318. Value: 5,
  319. },
  320. cli.IntFlag{
  321. Name: "port",
  322. Usage: "the port of the deployment to listen on pod",
  323. Required: true,
  324. },
  325. cli.IntFlag{
  326. Name: "nodePort",
  327. Usage: "the nodePort of the deployment to expose",
  328. Value: 0,
  329. },
  330. cli.IntFlag{
  331. Name: "minReplicas",
  332. Usage: "the min replicas to deploy",
  333. Value: 3,
  334. },
  335. cli.IntFlag{
  336. Name: "maxReplicas",
  337. Usage: "the max replicas of deploy",
  338. Value: 10,
  339. },
  340. cli.StringFlag{
  341. Name: "home",
  342. Usage: "the goctl home path of the template",
  343. },
  344. },
  345. Action: kube.DeploymentCommand,
  346. },
  347. },
  348. },
  349. {
  350. Name: "rpc",
  351. Usage: "generate rpc code",
  352. Subcommands: []cli.Command{
  353. {
  354. Name: "new",
  355. Usage: `generate rpc demo service`,
  356. Flags: []cli.Flag{
  357. cli.StringFlag{
  358. Name: "style",
  359. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  360. },
  361. cli.BoolFlag{
  362. Name: "idea",
  363. Usage: "whether the command execution environment is from idea plugin. [optional]",
  364. },
  365. cli.StringFlag{
  366. Name: "home",
  367. Usage: "the goctl home path of the template",
  368. },
  369. },
  370. Action: rpc.RPCNew,
  371. },
  372. {
  373. Name: "template",
  374. Usage: `generate proto template`,
  375. Flags: []cli.Flag{
  376. cli.StringFlag{
  377. Name: "out, o",
  378. Usage: "the target path of proto",
  379. },
  380. cli.StringFlag{
  381. Name: "home",
  382. Usage: "the goctl home path of the template",
  383. },
  384. },
  385. Action: rpc.RPCTemplate,
  386. },
  387. {
  388. Name: "proto",
  389. Usage: `generate rpc from proto`,
  390. Flags: []cli.Flag{
  391. cli.StringFlag{
  392. Name: "src, s",
  393. Usage: "the file path of the proto source file",
  394. },
  395. cli.StringSliceFlag{
  396. Name: "proto_path, I",
  397. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  398. },
  399. cli.StringSliceFlag{
  400. Name: "go_opt",
  401. Usage: `native command of protoc-gen-go, specify the mapping from proto to go, eg --go_opt=proto_import=go_package_import. [optional]`,
  402. },
  403. cli.StringFlag{
  404. Name: "dir, d",
  405. Usage: `the target path of the code`,
  406. },
  407. cli.StringFlag{
  408. Name: "style",
  409. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  410. },
  411. cli.BoolFlag{
  412. Name: "idea",
  413. Usage: "whether the command execution environment is from idea plugin. [optional]",
  414. },
  415. cli.StringFlag{
  416. Name: "home",
  417. Usage: "the goctl home path of the template",
  418. },
  419. },
  420. Action: rpc.RPC,
  421. },
  422. },
  423. },
  424. {
  425. Name: "model",
  426. Usage: "generate model code",
  427. Subcommands: []cli.Command{
  428. {
  429. Name: "mysql",
  430. Usage: `generate mysql model`,
  431. Subcommands: []cli.Command{
  432. {
  433. Name: "ddl",
  434. Usage: `generate mysql model from ddl`,
  435. Flags: []cli.Flag{
  436. cli.StringFlag{
  437. Name: "src, s",
  438. Usage: "the path or path globbing patterns of the ddl",
  439. },
  440. cli.StringFlag{
  441. Name: "dir, d",
  442. Usage: "the target dir",
  443. },
  444. cli.StringFlag{
  445. Name: "style",
  446. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  447. },
  448. cli.BoolFlag{
  449. Name: "cache, c",
  450. Usage: "generate code with cache [optional]",
  451. },
  452. cli.BoolFlag{
  453. Name: "idea",
  454. Usage: "for idea plugin [optional]",
  455. },
  456. cli.StringFlag{
  457. Name: "database, db",
  458. Usage: "the name of database [optional]",
  459. },
  460. cli.StringFlag{
  461. Name: "home",
  462. Usage: "the goctl home path of the template",
  463. },
  464. },
  465. Action: model.MysqlDDL,
  466. },
  467. {
  468. Name: "datasource",
  469. Usage: `generate model from datasource`,
  470. Flags: []cli.Flag{
  471. cli.StringFlag{
  472. Name: "url",
  473. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`,
  474. },
  475. cli.StringFlag{
  476. Name: "table, t",
  477. Usage: `the table or table globbing patterns in the database`,
  478. },
  479. cli.BoolFlag{
  480. Name: "cache, c",
  481. Usage: "generate code with cache [optional]",
  482. },
  483. cli.StringFlag{
  484. Name: "dir, d",
  485. Usage: "the target dir",
  486. },
  487. cli.StringFlag{
  488. Name: "style",
  489. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  490. },
  491. cli.BoolFlag{
  492. Name: "idea",
  493. Usage: "for idea plugin [optional]",
  494. },
  495. cli.StringFlag{
  496. Name: "home",
  497. Usage: "the goctl home path of the template",
  498. },
  499. },
  500. Action: model.MySqlDataSource,
  501. },
  502. },
  503. },
  504. {
  505. Name: "pg",
  506. Usage: `generate postgresql model`,
  507. Subcommands: []cli.Command{
  508. {
  509. Name: "datasource",
  510. Usage: `generate model from datasource`,
  511. Flags: []cli.Flag{
  512. cli.StringFlag{
  513. Name: "url",
  514. Usage: `the data source of database,like "postgres://root:password@127.0.0.1:54332/database?sslmode=disable"`,
  515. },
  516. cli.StringFlag{
  517. Name: "table, t",
  518. Usage: `the table or table globbing patterns in the database`,
  519. },
  520. cli.StringFlag{
  521. Name: "schema, s",
  522. Usage: `the table schema, default is [public]`,
  523. },
  524. cli.BoolFlag{
  525. Name: "cache, c",
  526. Usage: "generate code with cache [optional]",
  527. },
  528. cli.StringFlag{
  529. Name: "dir, d",
  530. Usage: "the target dir",
  531. },
  532. cli.StringFlag{
  533. Name: "style",
  534. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  535. },
  536. cli.BoolFlag{
  537. Name: "idea",
  538. Usage: "for idea plugin [optional]",
  539. },
  540. cli.StringFlag{
  541. Name: "home",
  542. Usage: "the goctl home path of the template",
  543. },
  544. },
  545. Action: model.PostgreSqlDataSource,
  546. },
  547. },
  548. },
  549. {
  550. Name: "mongo",
  551. Usage: `generate mongo model`,
  552. Flags: []cli.Flag{
  553. cli.StringSliceFlag{
  554. Name: "type, t",
  555. Usage: "specified model type name",
  556. },
  557. cli.BoolFlag{
  558. Name: "cache, c",
  559. Usage: "generate code with cache [optional]",
  560. },
  561. cli.StringFlag{
  562. Name: "dir, d",
  563. Usage: "the target dir",
  564. },
  565. cli.StringFlag{
  566. Name: "style",
  567. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  568. },
  569. cli.StringFlag{
  570. Name: "home",
  571. Usage: "the goctl home path of the template",
  572. },
  573. },
  574. Action: mongo.Action,
  575. },
  576. },
  577. },
  578. {
  579. Name: "template",
  580. Usage: "template operation",
  581. Subcommands: []cli.Command{
  582. {
  583. Name: "init",
  584. Usage: "initialize the all templates(force update)",
  585. Flags: []cli.Flag{
  586. cli.StringFlag{
  587. Name: "home",
  588. Usage: "the goctl home path of the template",
  589. },
  590. },
  591. Action: tpl.GenTemplates,
  592. },
  593. {
  594. Name: "clean",
  595. Usage: "clean the all cache templates",
  596. Flags: []cli.Flag{
  597. cli.StringFlag{
  598. Name: "home",
  599. Usage: "the goctl home path of the template",
  600. },
  601. },
  602. Action: tpl.CleanTemplates,
  603. },
  604. {
  605. Name: "update",
  606. Usage: "update template of the target category to the latest",
  607. Flags: []cli.Flag{
  608. cli.StringFlag{
  609. Name: "category,c",
  610. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  611. },
  612. cli.StringFlag{
  613. Name: "home",
  614. Usage: "the goctl home path of the template",
  615. },
  616. },
  617. Action: tpl.UpdateTemplates,
  618. },
  619. {
  620. Name: "revert",
  621. Usage: "revert the target template to the latest",
  622. Flags: []cli.Flag{
  623. cli.StringFlag{
  624. Name: "category,c",
  625. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  626. },
  627. cli.StringFlag{
  628. Name: "name,n",
  629. Usage: "the target file name of template",
  630. },
  631. cli.StringFlag{
  632. Name: "home",
  633. Usage: "the goctl home path of the template",
  634. },
  635. },
  636. Action: tpl.RevertTemplates,
  637. },
  638. },
  639. },
  640. }
  641. func main() {
  642. logx.Disable()
  643. load.Disable()
  644. stat.DisableLog()
  645. args := os.Args
  646. pluginName := filepath.Base(args[0])
  647. if pluginName == protocGenGoctl {
  648. pluginCtl.Generate()
  649. return
  650. }
  651. app := cli.NewApp()
  652. app.Usage = "a cli tool to generate code"
  653. app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH)
  654. app.Commands = commands
  655. // cli already print error messages
  656. if err := app.Run(os.Args); err != nil {
  657. fmt.Println(aurora.Red(errorx.Wrap(err).Error()))
  658. }
  659. }
  660. func init() {
  661. err := linkProtocGenGoctl()
  662. if err != nil {
  663. console.Error("%+v", err)
  664. }
  665. }
  666. const protocGenGoctl = "protoc-gen-goctl"
  667. func linkProtocGenGoctl() error {
  668. path, err := env.LookPath("goctl")
  669. if err != nil {
  670. return err
  671. }
  672. dir := filepath.Dir(path)
  673. ext := filepath.Ext(path)
  674. target := filepath.Join(dir, protocGenGoctl)
  675. if len(ext) > 0 {
  676. target = target + ext
  677. }
  678. _, err = os.Lstat(target)
  679. if err != nil && !os.IsNotExist(err) {
  680. return err
  681. }
  682. if os.IsNotExist(err) {
  683. return os.Symlink(path, target)
  684. }
  685. return nil
  686. }