-
Notifications
You must be signed in to change notification settings - Fork 1
fix: 使用go mod graph 替代 go.mod 解析 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||||||||
| package modutil | ||||||||||||||
|
|
||||||||||||||
| import ( | ||||||||||||||
| mapset "github.com/deckarep/golang-set/v2" | ||||||||||||||
| ver "github.com/hashicorp/go-version" | ||||||||||||||
| "github.com/pubgo/funk/pretty" | ||||||||||||||
| "github.com/pubgo/funk/v2/result" | ||||||||||||||
| "github.com/pubgo/protobuild/internal/shutil" | ||||||||||||||
| "github.com/samber/lo" | ||||||||||||||
| "strings" | ||||||||||||||
| "testing" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| func TestName(t *testing.T) { | ||||||||||||||
| pretty.Println(LoadVersions()) | ||||||||||||||
|
|
||||||||||||||
| modList := strings.Split(result.Wrap(shutil.GoModGraph()).Must(), "\n") | ||||||||||||||
| modSet := mapset.NewSet[string]() | ||||||||||||||
| for _, m := range modList { | ||||||||||||||
| for _, v := range strings.Split(m, " ") { | ||||||||||||||
| modSet.Add(strings.TrimSpace(v)) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| var modMap = make(map[string][]*ver.Version) | ||||||||||||||
| modSet.Each(func(s string) bool { | ||||||||||||||
| ver2 := strings.Split(s, "@") | ||||||||||||||
| if len(ver2) != 2 { | ||||||||||||||
| return false | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if !strings.HasPrefix(ver2[1], "v") { | ||||||||||||||
| return false | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| modMap[ver2[0]] = append(modMap[ver2[0]], ver.Must(ver.NewSemver(ver2[1]))) | ||||||||||||||
| return false | ||||||||||||||
| }) | ||||||||||||||
|
|
||||||||||||||
| for k, v := range modMap { | ||||||||||||||
| pretty.Println(k, maxVersion(v).String(), minVersion(v).String()) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+14
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test function
Please refactor this to be a proper unit test. It should call |
||||||||||||||
|
|
||||||||||||||
| func minVersion(versions []*ver.Version) *ver.Version { | ||||||||||||||
| return lo.MaxBy(versions, func(a *ver.Version, b *ver.Version) bool { return !a.GreaterThan(b) }) | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
ver.Must(...)can cause a panic ifgo mod graphprovides a version string that cannot be parsed as a semantic version. Consider handling the error fromver.NewSemvergracefully, for example, by logging the error and skipping the invalid entry.