Skip to content

Commit

Permalink
handle modules which only contain pre-release versions (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy authored Jan 30, 2024
1 parent a256279 commit 18db73c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
27 changes: 23 additions & 4 deletions internal/modproxy/modproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,36 @@ func Query(modpath string, cached bool) (*Module, bool, error) {

// Latest finds the latest major version of a module
// cached sets the Disable-Module-Fetch: true header
func Latest(modpath string, cached bool) (*Module, error) {
// pre controls whether to return modules which only contain pre-release versions.
func Latest(modpath string, cached, pre bool) (*Module, error) {
mods, err := List(modpath, cached)
if err != nil {
return nil, err
}
for i := len(mods); i > 0; i-- {
mod := mods[i-1]
if max := mod.MaxVersion("", pre); max != "" {
return mod, nil
}
}
return nil, fmt.Errorf("no module versions found")
}

// List finds all the major versions of a module
// cached sets the Disable-Module-Fetch: true header
func List(modpath string, cached bool) ([]*Module, error) {
latest, ok, err := Query(modpath, cached)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("module not found: %s", modpath)
}
history := []*Module{latest}
for i := 0; i < 100; i++ {
nextpath, ok := latest.NextMajorPath()
if !ok {
return latest, nil
return history, nil
}
next, ok, err := Query(nextpath, cached)
if err != nil {
Expand All @@ -195,9 +213,10 @@ func Latest(modpath string, cached bool) (*Module, error) {
}
}
if !ok {
return latest, nil
return history, nil
}
latest = next
history = append(history, latest)
}
return nil, fmt.Errorf("request limit exceeded")
}
Expand Down Expand Up @@ -272,7 +291,7 @@ func Updates(opt UpdateOptions) {
continue
}
group.Go(func() error {
mod, err := Latest(m.Path, opt.Cached)
mod, err := Latest(m.Path, opt.Cached, opt.Pre)
if err != nil {
ch <- Update{Module: m, Err: err}
return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/modproxy/modproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ func TestLatest(t *testing.T) {
tests := []string{
"github.com/go-redis/redis",
"github.com/russross/blackfriday",
"github.com/urfave/cli",
}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
mod, err := Latest(tt, true)
mod, err := Latest(tt, true, true)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func getcmd(args []string) error {
case "":
version = mod.MaxVersion("", pre)
case "latest":
latest, err := modproxy.Latest(mod.Path, cached)
latest, err := modproxy.Latest(mod.Path, cached, pre)
if err != nil {
return err
}
Expand Down

0 comments on commit 18db73c

Please sign in to comment.