gen.go 2.7 KB

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