goctl.go 17 KB

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