gen.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // 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. logx.Must(genEtc(dir, cfg, api))
  48. logx.Must(genConfig(dir, cfg, api))
  49. logx.Must(genMain(dir, cfg, api))
  50. logx.Must(genServiceContext(dir, cfg, api))
  51. logx.Must(genTypes(dir, cfg, api))
  52. logx.Must(genRoutes(dir, cfg, api))
  53. logx.Must(genHandlers(dir, cfg, api))
  54. logx.Must(genLogic(dir, cfg, api))
  55. logx.Must(genMiddleware(dir, cfg, api))
  56. if err := backupAndSweep(apiFile); err != nil {
  57. return err
  58. }
  59. if err := apiformat.ApiFormatByPath(apiFile); err != nil {
  60. return err
  61. }
  62. fmt.Println(aurora.Green("Done."))
  63. return nil
  64. }
  65. func backupAndSweep(apiFile string) error {
  66. var err error
  67. var wg sync.WaitGroup
  68. wg.Add(2)
  69. _ = os.MkdirAll(tmpDir, os.ModePerm)
  70. go func() {
  71. _, fileName := filepath.Split(apiFile)
  72. _, e := apiutil.Copy(apiFile, fmt.Sprintf(path.Join(tmpDir, tmpFile), fileName, time.Now().Unix()))
  73. if e != nil {
  74. err = e
  75. }
  76. wg.Done()
  77. }()
  78. go func() {
  79. if e := sweep(); e != nil {
  80. err = e
  81. }
  82. wg.Done()
  83. }()
  84. wg.Wait()
  85. return err
  86. }
  87. func sweep() error {
  88. keepTime := time.Now().AddDate(0, 0, -7)
  89. return filepath.Walk(tmpDir, func(fpath string, info os.FileInfo, err error) error {
  90. if info.IsDir() {
  91. return nil
  92. }
  93. pos := strings.LastIndexByte(info.Name(), '-')
  94. if pos > 0 {
  95. timestamp := info.Name()[pos+1:]
  96. seconds, err := strconv.ParseInt(timestamp, 10, 64)
  97. if err != nil {
  98. // print error and ignore
  99. fmt.Println(aurora.Red(fmt.Sprintf("sweep ignored file: %s", fpath)))
  100. return nil
  101. }
  102. tm := time.Unix(seconds, 0)
  103. if tm.Before(keepTime) {
  104. if err := os.Remove(fpath); err != nil {
  105. fmt.Println(aurora.Red(fmt.Sprintf("failed to remove file: %s", fpath)))
  106. return err
  107. }
  108. }
  109. }
  110. return nil
  111. })
  112. }