123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package migrate
- import (
- "errors"
- "fmt"
- "os"
- "time"
- "github.com/wuntsong-org/go-zero-plus/core/stringx"
- "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/rpc/execx"
- "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/console"
- "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/ctx"
- )
- const (
- deprecatedGoZeroMod = "github.com/tal-tech/go-zero"
- deprecatedBuilderx = "github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
- replacementBuilderx = "github.com/wuntsong-org/go-zero-plus/core/stores/builder"
- goZeroMod = "github.com/wuntsong-org/go-zero-plus"
- )
- var errInvalidGoMod = errors.New("it's only working for go module")
- func editMod(version string, verbose bool) error {
- wd, err := os.Getwd()
- if err != nil {
- return err
- }
- isGoMod, _ := ctx.IsGoMod(wd)
- if !isGoMod {
- return nil
- }
- latest, err := getLatest(goZeroMod, verbose)
- if err != nil {
- return err
- }
- if !stringx.Contains(latest, version) {
- return fmt.Errorf("release version %q is not found", version)
- }
- mod := fmt.Sprintf("%s@%s", goZeroMod, version)
- err = removeRequire(deprecatedGoZeroMod, verbose)
- if err != nil {
- return err
- }
- return addRequire(mod, verbose)
- }
- func addRequire(mod string, verbose bool) error {
- if verbose {
- console.Info("adding require %s ...", mod)
- time.Sleep(200 * time.Millisecond)
- }
- wd, err := os.Getwd()
- if err != nil {
- return err
- }
- isGoMod, _ := ctx.IsGoMod(wd)
- if !isGoMod {
- return errInvalidGoMod
- }
- _, err = execx.Run("go mod edit -require "+mod, wd)
- return err
- }
- func removeRequire(mod string, verbose bool) error {
- if verbose {
- console.Info("remove require %s ...", mod)
- time.Sleep(200 * time.Millisecond)
- }
- wd, err := os.Getwd()
- if err != nil {
- return err
- }
- _, err = execx.Run("go mod edit -droprequire "+mod, wd)
- return err
- }
- func tidy(verbose bool) error {
- if verbose {
- console.Info("go mod tidy ...")
- time.Sleep(200 * time.Millisecond)
- }
- wd, err := os.Getwd()
- if err != nil {
- return err
- }
- isGoMod, _ := ctx.IsGoMod(wd)
- if !isGoMod {
- return nil
- }
- _, err = execx.Run("go mod tidy", wd)
- return err
- }
|