gen.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. home := c.String("home")
  29. if len(home) > 0 {
  30. util.RegisterGoctlHome(home)
  31. }
  32. if len(apiFile) == 0 {
  33. return errors.New("missing -api")
  34. }
  35. if len(dir) == 0 {
  36. return errors.New("missing -dir")
  37. }
  38. return DoGenProject(apiFile, dir, namingStyle)
  39. }
  40. // DoGenProject gen go project files with api file
  41. func DoGenProject(apiFile, dir, style string) error {
  42. api, err := parser.Parse(apiFile)
  43. if err != nil {
  44. return err
  45. }
  46. cfg, err := config.NewConfig(style)
  47. if err != nil {
  48. return err
  49. }
  50. logx.Must(util.MkdirIfNotExist(dir))
  51. rootPkg, err := getParentPackage(dir)
  52. if err != nil {
  53. return err
  54. }
  55. logx.Must(genEtc(dir, cfg, api))
  56. logx.Must(genConfig(dir, cfg, api))
  57. logx.Must(genMain(dir, rootPkg, cfg, api))
  58. logx.Must(genServiceContext(dir, rootPkg, cfg, api))
  59. logx.Must(genTypes(dir, cfg, api))
  60. logx.Must(genRoutes(dir, rootPkg, cfg, api))
  61. logx.Must(genHandlers(dir, rootPkg, cfg, api))
  62. logx.Must(genLogic(dir, rootPkg, cfg, api))
  63. logx.Must(genMiddleware(dir, cfg, api))
  64. if err := backupAndSweep(apiFile); err != nil {
  65. return err
  66. }
  67. if err := apiformat.ApiFormatByPath(apiFile); err != nil {
  68. return err
  69. }
  70. fmt.Println(aurora.Green("Done."))
  71. return nil
  72. }
  73. func backupAndSweep(apiFile string) error {
  74. var err error
  75. var wg sync.WaitGroup
  76. wg.Add(2)
  77. _ = os.MkdirAll(tmpDir, os.ModePerm)
  78. go func() {
  79. _, fileName := filepath.Split(apiFile)
  80. _, e := apiutil.Copy(apiFile, fmt.Sprintf(path.Join(tmpDir, tmpFile), fileName, time.Now().Unix()))
  81. if e != nil {
  82. err = e
  83. }
  84. wg.Done()
  85. }()
  86. go func() {
  87. if e := sweep(); e != nil {
  88. err = e
  89. }
  90. wg.Done()
  91. }()
  92. wg.Wait()
  93. return err
  94. }
  95. func sweep() error {
  96. keepTime := time.Now().AddDate(0, 0, -7)
  97. return filepath.Walk(tmpDir, func(fpath string, info os.FileInfo, err error) error {
  98. if info.IsDir() {
  99. return nil
  100. }
  101. pos := strings.LastIndexByte(info.Name(), '-')
  102. if pos > 0 {
  103. timestamp := info.Name()[pos+1:]
  104. seconds, err := strconv.ParseInt(timestamp, 10, 64)
  105. if err != nil {
  106. // print error and ignore
  107. fmt.Println(aurora.Red(fmt.Sprintf("sweep ignored file: %s", fpath)))
  108. return nil
  109. }
  110. tm := time.Unix(seconds, 0)
  111. if tm.Before(keepTime) {
  112. if err := os.Remove(fpath); err != nil {
  113. fmt.Println(aurora.Red(fmt.Sprintf("failed to remove file: %s", fpath)))
  114. return err
  115. }
  116. }
  117. }
  118. return nil
  119. })
  120. }