mod.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package migrate
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/wuntsong-org/go-zero-plus/core/stringx"
  8. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/rpc/execx"
  9. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/console"
  10. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/ctx"
  11. )
  12. const (
  13. deprecatedGoZeroMod = "github.com/tal-tech/go-zero"
  14. deprecatedBuilderx = "github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
  15. replacementBuilderx = "github.com/wuntsong-org/go-zero-plus/core/stores/builder"
  16. goZeroMod = "github.com/wuntsong-org/go-zero-plus"
  17. )
  18. var errInvalidGoMod = errors.New("it's only working for go module")
  19. func editMod(version string, verbose bool) error {
  20. wd, err := os.Getwd()
  21. if err != nil {
  22. return err
  23. }
  24. isGoMod, _ := ctx.IsGoMod(wd)
  25. if !isGoMod {
  26. return nil
  27. }
  28. latest, err := getLatest(goZeroMod, verbose)
  29. if err != nil {
  30. return err
  31. }
  32. if !stringx.Contains(latest, version) {
  33. return fmt.Errorf("release version %q is not found", version)
  34. }
  35. mod := fmt.Sprintf("%s@%s", goZeroMod, version)
  36. err = removeRequire(deprecatedGoZeroMod, verbose)
  37. if err != nil {
  38. return err
  39. }
  40. return addRequire(mod, verbose)
  41. }
  42. func addRequire(mod string, verbose bool) error {
  43. if verbose {
  44. console.Info("adding require %s ...", mod)
  45. time.Sleep(200 * time.Millisecond)
  46. }
  47. wd, err := os.Getwd()
  48. if err != nil {
  49. return err
  50. }
  51. isGoMod, _ := ctx.IsGoMod(wd)
  52. if !isGoMod {
  53. return errInvalidGoMod
  54. }
  55. _, err = execx.Run("go mod edit -require "+mod, wd)
  56. return err
  57. }
  58. func removeRequire(mod string, verbose bool) error {
  59. if verbose {
  60. console.Info("remove require %s ...", mod)
  61. time.Sleep(200 * time.Millisecond)
  62. }
  63. wd, err := os.Getwd()
  64. if err != nil {
  65. return err
  66. }
  67. _, err = execx.Run("go mod edit -droprequire "+mod, wd)
  68. return err
  69. }
  70. func tidy(verbose bool) error {
  71. if verbose {
  72. console.Info("go mod tidy ...")
  73. time.Sleep(200 * time.Millisecond)
  74. }
  75. wd, err := os.Getwd()
  76. if err != nil {
  77. return err
  78. }
  79. isGoMod, _ := ctx.IsGoMod(wd)
  80. if !isGoMod {
  81. return nil
  82. }
  83. _, err = execx.Run("go mod tidy", wd)
  84. return err
  85. }