forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add migrate * Remove unused module * refactor filename * rename refactor to migrate Co-authored-by: anqiansong <anqiansong@bytedance.com>
- Loading branch information
Showing
7 changed files
with
317 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package migrate | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/ast" | ||
"go/format" | ||
"go/parser" | ||
"go/token" | ||
"io/fs" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/tal-tech/go-zero/tools/goctl/util/console" | ||
"github.com/tal-tech/go-zero/tools/goctl/util/ctx" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
const zeromicroVersion = "1.3.0" | ||
|
||
var fset = token.NewFileSet() | ||
|
||
func Migrate(c *cli.Context) error { | ||
verbose := c.Bool("verbose") | ||
version := c.String("version") | ||
if len(version) == 0 { | ||
version = zeromicroVersion | ||
} | ||
err := editMod(version, verbose) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = rewriteImport(verbose) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = tidy(verbose) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
console.Success("[OK] refactor finish, execute %q on project root to check status.", "go test -race ./...") | ||
return nil | ||
} | ||
|
||
func rewriteImport(verbose bool) error { | ||
if verbose { | ||
console.Info("preparing to rewrite import ...") | ||
time.Sleep(200 * time.Millisecond) | ||
} | ||
|
||
wd, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
project, err := ctx.Prepare(wd) | ||
if err != nil { | ||
return err | ||
} | ||
root := project.Dir | ||
fsys := os.DirFS(root) | ||
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { | ||
if !d.IsDir() { | ||
return nil | ||
} | ||
if verbose { | ||
console.Info("walking to %q", path) | ||
} | ||
pkgs, err := parser.ParseDir(fset, path, func(info fs.FileInfo) bool { | ||
return strings.HasSuffix(info.Name(), ".go") | ||
}, parser.ParseComments) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return rewriteFile(pkgs, verbose) | ||
}) | ||
} | ||
|
||
func rewriteFile(pkgs map[string]*ast.Package, verbose bool) error { | ||
for _, pkg := range pkgs { | ||
for filename, file := range pkg.Files { | ||
for _, imp := range file.Imports { | ||
if !strings.Contains(imp.Path.Value, deprecatedGoZeroMod) { | ||
continue | ||
} | ||
newPath := strings.ReplaceAll(imp.Path.Value, deprecatedGoZeroMod, goZeroMod) | ||
imp.EndPos = imp.End() | ||
imp.Path.Value = newPath | ||
} | ||
|
||
var w = bytes.NewBuffer(nil) | ||
err := format.Node(w, fset, file) | ||
if err != nil { | ||
return fmt.Errorf("[rewriteImport] format file %s error: %+v", filename, err) | ||
} | ||
|
||
err = ioutil.WriteFile(filename, w.Bytes(), os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("[rewriteImport] write file %s error: %+v", filename, err) | ||
} | ||
if verbose { | ||
console.Success("[OK] rewriting %q ... ", filepath.Base(filename)) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package migrate | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/tal-tech/go-zero/core/stringx" | ||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx" | ||
"github.com/tal-tech/go-zero/tools/goctl/util/console" | ||
"github.com/tal-tech/go-zero/tools/goctl/util/ctx" | ||
) | ||
|
||
const deprecatedGoZeroMod = "github.com/tal-tech/go-zero" | ||
const goZeroMod = "github.com/zeromicro/go-zero" | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package migrate | ||
|
||
import ( | ||
"net/url" | ||
"os" | ||
"strings" | ||
|
||
"github.com/tal-tech/go-zero/core/stringx" | ||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx" | ||
) | ||
|
||
var defaultProxy = "https://goproxy.cn" | ||
var defaultProxies = []string{defaultProxy} | ||
|
||
func goProxy() []string { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return defaultProxies | ||
} | ||
|
||
proxy, err := execx.Run("go env GOPROXY", wd) | ||
if err != nil { | ||
return defaultProxies | ||
} | ||
list := strings.FieldsFunc(proxy, func(r rune) bool { | ||
return r == '|' || r == ',' | ||
}) | ||
var ret []string | ||
for _, item := range list { | ||
if len(item) == 0 { | ||
continue | ||
} | ||
_, err = url.Parse(item) | ||
if err == nil && !stringx.Contains(ret, item) { | ||
ret = append(ret, item) | ||
} | ||
} | ||
if !stringx.Contains(ret, defaultProxy) { | ||
ret = append(ret, defaultProxy) | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package migrate | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/tal-tech/go-zero/tools/goctl/util/console" | ||
) | ||
|
||
var client = http.Client{ | ||
Timeout: 5 * time.Second, | ||
} | ||
|
||
func getLatest(repo string, verbose bool) ([]string, error) { | ||
proxies := goProxy() | ||
for _, proxy := range proxies { | ||
if verbose { | ||
console.Info("use go proxy %q", proxy) | ||
} | ||
log := func(err error) { | ||
console.Warning("get latest versions failed from proxy %q, error: %+v", proxy, err) | ||
} | ||
resp, err := client.Get(fmt.Sprintf("%s/%s/@v/list", proxy, repo)) | ||
if err != nil { | ||
log(err) | ||
continue | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
log(fmt.Errorf("%s", resp.Status)) | ||
continue | ||
} | ||
defer resp.Body.Close() | ||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log(err) | ||
continue | ||
} | ||
versionStr := string(data) | ||
versions := strings.Fields(versionStr) | ||
return versions, nil | ||
} | ||
return []string{}, nil | ||
} |