gen.go 3.2 KB

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