goctl.go 17 KB

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