gen.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package gogen
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "time"
  12. "git.i2edu.net/i2/go-zero/core/logx"
  13. apiformat "git.i2edu.net/i2/go-zero/tools/goctl/api/format"
  14. "git.i2edu.net/i2/go-zero/tools/goctl/api/parser"
  15. apiutil "git.i2edu.net/i2/go-zero/tools/goctl/api/util"
  16. "git.i2edu.net/i2/go-zero/tools/goctl/config"
  17. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  18. "github.com/logrusorgru/aurora"
  19. "github.com/urfave/cli"
  20. )
  21. const tmpFile = "%s-%d"
  22. var tmpDir = path.Join(os.TempDir(), "goctl")
  23. // GoCommand gen go project files from command line
  24. func GoCommand(c *cli.Context) error {
  25. apiFile := c.String("api")
  26. dir := c.String("dir")
  27. namingStyle := c.String("style")
  28. if len(apiFile) == 0 {
  29. return errors.New("missing -api")
  30. }
  31. if len(dir) == 0 {
  32. return errors.New("missing -dir")
  33. }
  34. return DoGenProject(apiFile, dir, namingStyle)
  35. }
  36. // DoGenProject gen go project files with api file
  37. func DoGenProject(apiFile, dir, style string) error {
  38. api, err := parser.Parse(apiFile)
  39. if err != nil {
  40. return err
  41. }
  42. cfg, err := config.NewConfig(style)
  43. if err != nil {
  44. return err
  45. }
  46. logx.Must(util.MkdirIfNotExist(dir))
  47. rootPkg, err := getParentPackage(dir)
  48. if err != nil {
  49. return err
  50. }
  51. logx.Must(genEtc(dir, cfg, api))
  52. logx.Must(genConfig(dir, cfg, api))
  53. logx.Must(genMain(dir, rootPkg, cfg, api))
  54. logx.Must(genServiceContext(dir, rootPkg, cfg, api))
  55. logx.Must(genTypes(dir, cfg, api))
  56. logx.Must(genRoutes(dir, rootPkg, cfg, api))
  57. logx.Must(genHandlers(dir, rootPkg, cfg, api))
  58. logx.Must(genLogic(dir, rootPkg, cfg, api))
  59. logx.Must(genMiddleware(dir, cfg, api))
  60. if err := backupAndSweep(apiFile); err != nil {
  61. return err
  62. }
  63. if err := apiformat.ApiFormatByPath(apiFile); err != nil {
  64. return err
  65. }
  66. fmt.Println(aurora.Green("Done."))
  67. return nil
  68. }
  69. func backupAndSweep(apiFile string) error {
  70. var err error
  71. var wg sync.WaitGroup
  72. wg.Add(2)
  73. _ = os.MkdirAll(tmpDir, os.ModePerm)
  74. go func() {
  75. _, fileName := filepath.Split(apiFile)
  76. _, e := apiutil.Copy(apiFile, fmt.Sprintf(path.Join(tmpDir, tmpFile), fileName, time.Now().Unix()))
  77. if e != nil {
  78. err = e
  79. }
  80. wg.Done()
  81. }()
  82. go func() {
  83. if e := sweep(); e != nil {
  84. err = e
  85. }
  86. wg.Done()
  87. }()
  88. wg.Wait()
  89. return err
  90. }
  91. func sweep() error {
  92. keepTime := time.Now().AddDate(0, 0, -7)
  93. return filepath.Walk(tmpDir, func(fpath string, info os.FileInfo, err error) error {
  94. if info.IsDir() {
  95. return nil
  96. }
  97. pos := strings.LastIndexByte(info.Name(), '-')
  98. if pos > 0 {
  99. timestamp := info.Name()[pos+1:]
  100. seconds, err := strconv.ParseInt(timestamp, 10, 64)
  101. if err != nil {
  102. // print error and ignore
  103. fmt.Println(aurora.Red(fmt.Sprintf("sweep ignored file: %s", fpath)))
  104. return nil
  105. }
  106. tm := time.Unix(seconds, 0)
  107. if tm.Before(keepTime) {
  108. if err := os.Remove(fpath); err != nil {
  109. fmt.Println(aurora.Red(fmt.Sprintf("failed to remove file: %s", fpath)))
  110. return err
  111. }
  112. }
  113. }
  114. return nil
  115. })
  116. }