goctl.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. cli.StringFlag{
  256. Name: "version",
  257. Usage: "the goctl builder golang image version",
  258. },
  259. },
  260. Action: docker.DockerCommand,
  261. },
  262. {
  263. Name: "kube",
  264. Usage: "generate kubernetes files",
  265. Subcommands: []cli.Command{
  266. {
  267. Name: "deploy",
  268. Usage: "generate deployment yaml file",
  269. Flags: []cli.Flag{
  270. cli.StringFlag{
  271. Name: "name",
  272. Usage: "the name of deployment",
  273. Required: true,
  274. },
  275. cli.StringFlag{
  276. Name: "namespace",
  277. Usage: "the namespace of deployment",
  278. Required: true,
  279. },
  280. cli.StringFlag{
  281. Name: "image",
  282. Usage: "the docker image of deployment",
  283. Required: true,
  284. },
  285. cli.StringFlag{
  286. Name: "secret",
  287. Usage: "the secret to image pull from registry",
  288. },
  289. cli.IntFlag{
  290. Name: "requestCpu",
  291. Usage: "the request cpu to deploy",
  292. Value: 500,
  293. },
  294. cli.IntFlag{
  295. Name: "requestMem",
  296. Usage: "the request memory to deploy",
  297. Value: 512,
  298. },
  299. cli.IntFlag{
  300. Name: "limitCpu",
  301. Usage: "the limit cpu to deploy",
  302. Value: 1000,
  303. },
  304. cli.IntFlag{
  305. Name: "limitMem",
  306. Usage: "the limit memory to deploy",
  307. Value: 1024,
  308. },
  309. cli.StringFlag{
  310. Name: "o",
  311. Usage: "the output yaml file",
  312. Required: true,
  313. },
  314. cli.IntFlag{
  315. Name: "replicas",
  316. Usage: "the number of replicas to deploy",
  317. Value: 3,
  318. },
  319. cli.IntFlag{
  320. Name: "revisions",
  321. Usage: "the number of revision history to limit",
  322. Value: 5,
  323. },
  324. cli.IntFlag{
  325. Name: "port",
  326. Usage: "the port of the deployment to listen on pod",
  327. Required: true,
  328. },
  329. cli.IntFlag{
  330. Name: "nodePort",
  331. Usage: "the nodePort of the deployment to expose",
  332. Value: 0,
  333. },
  334. cli.IntFlag{
  335. Name: "minReplicas",
  336. Usage: "the min replicas to deploy",
  337. Value: 3,
  338. },
  339. cli.IntFlag{
  340. Name: "maxReplicas",
  341. Usage: "the max replicas of deploy",
  342. Value: 10,
  343. },
  344. cli.StringFlag{
  345. Name: "home",
  346. Usage: "the goctl home path of the template",
  347. },
  348. },
  349. Action: kube.DeploymentCommand,
  350. },
  351. },
  352. },
  353. {
  354. Name: "rpc",
  355. Usage: "generate rpc code",
  356. Subcommands: []cli.Command{
  357. {
  358. Name: "new",
  359. Usage: `generate rpc demo service`,
  360. Flags: []cli.Flag{
  361. cli.StringFlag{
  362. Name: "style",
  363. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  364. },
  365. cli.BoolFlag{
  366. Name: "idea",
  367. Usage: "whether the command execution environment is from idea plugin. [optional]",
  368. },
  369. cli.StringFlag{
  370. Name: "home",
  371. Usage: "the goctl home path of the template",
  372. },
  373. },
  374. Action: rpc.RPCNew,
  375. },
  376. {
  377. Name: "template",
  378. Usage: `generate proto template`,
  379. Flags: []cli.Flag{
  380. cli.StringFlag{
  381. Name: "out, o",
  382. Usage: "the target path of proto",
  383. },
  384. cli.StringFlag{
  385. Name: "home",
  386. Usage: "the goctl home path of the template",
  387. },
  388. },
  389. Action: rpc.RPCTemplate,
  390. },
  391. {
  392. Name: "proto",
  393. Usage: `generate rpc from proto`,
  394. Flags: []cli.Flag{
  395. cli.StringFlag{
  396. Name: "src, s",
  397. Usage: "the file path of the proto source file",
  398. },
  399. cli.StringSliceFlag{
  400. Name: "proto_path, I",
  401. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  402. },
  403. cli.StringSliceFlag{
  404. Name: "go_opt",
  405. Usage: `native command of protoc-gen-go, specify the mapping from proto to go, eg --go_opt=proto_import=go_package_import. [optional]`,
  406. },
  407. cli.StringFlag{
  408. Name: "dir, d",
  409. Usage: `the target path of the code`,
  410. },
  411. cli.StringFlag{
  412. Name: "style",
  413. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  414. },
  415. cli.BoolFlag{
  416. Name: "idea",
  417. Usage: "whether the command execution environment is from idea plugin. [optional]",
  418. },
  419. cli.StringFlag{
  420. Name: "home",
  421. Usage: "the goctl home path of the template",
  422. },
  423. },
  424. Action: rpc.RPC,
  425. },
  426. },
  427. },
  428. {
  429. Name: "model",
  430. Usage: "generate model code",
  431. Subcommands: []cli.Command{
  432. {
  433. Name: "mysql",
  434. Usage: `generate mysql model`,
  435. Subcommands: []cli.Command{
  436. {
  437. Name: "ddl",
  438. Usage: `generate mysql model from ddl`,
  439. Flags: []cli.Flag{
  440. cli.StringFlag{
  441. Name: "src, s",
  442. Usage: "the path or path globbing patterns of the ddl",
  443. },
  444. cli.StringFlag{
  445. Name: "dir, d",
  446. Usage: "the target dir",
  447. },
  448. cli.StringFlag{
  449. Name: "style",
  450. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  451. },
  452. cli.BoolFlag{
  453. Name: "cache, c",
  454. Usage: "generate code with cache [optional]",
  455. },
  456. cli.BoolFlag{
  457. Name: "idea",
  458. Usage: "for idea plugin [optional]",
  459. },
  460. cli.StringFlag{
  461. Name: "database, db",
  462. Usage: "the name of database [optional]",
  463. },
  464. cli.StringFlag{
  465. Name: "home",
  466. Usage: "the goctl home path of the template",
  467. },
  468. },
  469. Action: model.MysqlDDL,
  470. },
  471. {
  472. Name: "datasource",
  473. Usage: `generate model from datasource`,
  474. Flags: []cli.Flag{
  475. cli.StringFlag{
  476. Name: "url",
  477. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`,
  478. },
  479. cli.StringFlag{
  480. Name: "table, t",
  481. Usage: `the table or table globbing patterns in the database`,
  482. },
  483. cli.BoolFlag{
  484. Name: "cache, c",
  485. Usage: "generate code with cache [optional]",
  486. },
  487. cli.StringFlag{
  488. Name: "dir, d",
  489. Usage: "the target dir",
  490. },
  491. cli.StringFlag{
  492. Name: "style",
  493. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  494. },
  495. cli.BoolFlag{
  496. Name: "idea",
  497. Usage: "for idea plugin [optional]",
  498. },
  499. cli.StringFlag{
  500. Name: "home",
  501. Usage: "the goctl home path of the template",
  502. },
  503. },
  504. Action: model.MySqlDataSource,
  505. },
  506. },
  507. },
  508. {
  509. Name: "pg",
  510. Usage: `generate postgresql model`,
  511. Subcommands: []cli.Command{
  512. {
  513. Name: "datasource",
  514. Usage: `generate model from datasource`,
  515. Flags: []cli.Flag{
  516. cli.StringFlag{
  517. Name: "url",
  518. Usage: `the data source of database,like "postgres://root:password@127.0.0.1:54332/database?sslmode=disable"`,
  519. },
  520. cli.StringFlag{
  521. Name: "table, t",
  522. Usage: `the table or table globbing patterns in the database`,
  523. },
  524. cli.StringFlag{
  525. Name: "schema, s",
  526. Usage: `the table schema, default is [public]`,
  527. },
  528. cli.BoolFlag{
  529. Name: "cache, c",
  530. Usage: "generate code with cache [optional]",
  531. },
  532. cli.StringFlag{
  533. Name: "dir, d",
  534. Usage: "the target dir",
  535. },
  536. cli.StringFlag{
  537. Name: "style",
  538. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  539. },
  540. cli.BoolFlag{
  541. Name: "idea",
  542. Usage: "for idea plugin [optional]",
  543. },
  544. cli.StringFlag{
  545. Name: "home",
  546. Usage: "the goctl home path of the template",
  547. },
  548. },
  549. Action: model.PostgreSqlDataSource,
  550. },
  551. },
  552. },
  553. {
  554. Name: "mongo",
  555. Usage: `generate mongo model`,
  556. Flags: []cli.Flag{
  557. cli.StringSliceFlag{
  558. Name: "type, t",
  559. Usage: "specified model type name",
  560. },
  561. cli.BoolFlag{
  562. Name: "cache, c",
  563. Usage: "generate code with cache [optional]",
  564. },
  565. cli.StringFlag{
  566. Name: "dir, d",
  567. Usage: "the target dir",
  568. },
  569. cli.StringFlag{
  570. Name: "style",
  571. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  572. },
  573. cli.StringFlag{
  574. Name: "home",
  575. Usage: "the goctl home path of the template",
  576. },
  577. },
  578. Action: mongo.Action,
  579. },
  580. },
  581. },
  582. {
  583. Name: "template",
  584. Usage: "template operation",
  585. Subcommands: []cli.Command{
  586. {
  587. Name: "init",
  588. Usage: "initialize the all templates(force update)",
  589. Flags: []cli.Flag{
  590. cli.StringFlag{
  591. Name: "home",
  592. Usage: "the goctl home path of the template",
  593. },
  594. },
  595. Action: tpl.GenTemplates,
  596. },
  597. {
  598. Name: "clean",
  599. Usage: "clean the all cache templates",
  600. Flags: []cli.Flag{
  601. cli.StringFlag{
  602. Name: "home",
  603. Usage: "the goctl home path of the template",
  604. },
  605. },
  606. Action: tpl.CleanTemplates,
  607. },
  608. {
  609. Name: "update",
  610. Usage: "update template of the target category to the latest",
  611. Flags: []cli.Flag{
  612. cli.StringFlag{
  613. Name: "category,c",
  614. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  615. },
  616. cli.StringFlag{
  617. Name: "home",
  618. Usage: "the goctl home path of the template",
  619. },
  620. },
  621. Action: tpl.UpdateTemplates,
  622. },
  623. {
  624. Name: "revert",
  625. Usage: "revert the target template to the latest",
  626. Flags: []cli.Flag{
  627. cli.StringFlag{
  628. Name: "category,c",
  629. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  630. },
  631. cli.StringFlag{
  632. Name: "name,n",
  633. Usage: "the target file name of template",
  634. },
  635. cli.StringFlag{
  636. Name: "home",
  637. Usage: "the goctl home path of the template",
  638. },
  639. },
  640. Action: tpl.RevertTemplates,
  641. },
  642. },
  643. },
  644. }
  645. func main() {
  646. logx.Disable()
  647. load.Disable()
  648. stat.DisableLog()
  649. args := os.Args
  650. pluginName := filepath.Base(args[0])
  651. if pluginName == protocGenGoctl {
  652. pluginCtl.Generate()
  653. return
  654. }
  655. app := cli.NewApp()
  656. app.Usage = "a cli tool to generate code"
  657. app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH)
  658. app.Commands = commands
  659. // cli already print error messages
  660. if err := app.Run(os.Args); err != nil {
  661. fmt.Println(aurora.Red(errorx.Wrap(err).Error()))
  662. }
  663. }
  664. func init() {
  665. err := linkProtocGenGoctl()
  666. if err != nil {
  667. console.Error("%+v", err)
  668. }
  669. }
  670. const protocGenGoctl = "protoc-gen-goctl"
  671. func linkProtocGenGoctl() error {
  672. path, err := env.LookPath("goctl")
  673. if err != nil {
  674. return err
  675. }
  676. dir := filepath.Dir(path)
  677. ext := filepath.Ext(path)
  678. target := filepath.Join(dir, protocGenGoctl)
  679. if len(ext) > 0 {
  680. target = target + ext
  681. }
  682. _, err = os.Lstat(target)
  683. if err != nil && !os.IsNotExist(err) {
  684. return err
  685. }
  686. if os.IsNotExist(err) {
  687. return os.Symlink(path, target)
  688. }
  689. return nil
  690. }