goctl.go 17 KB

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