mod.go 2.0 KB

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