gen.go 3.2 KB

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