From 18db73c103c787656346d5919bc7a63feaf86e51 Mon Sep 17 00:00:00 2001 From: Ilia Choly Date: Tue, 30 Jan 2024 15:58:52 -0500 Subject: [PATCH] handle modules which only contain pre-release versions (#27) --- internal/modproxy/modproxy.go | 27 +++++++++++++++++++++++---- internal/modproxy/modproxy_test.go | 3 ++- main.go | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/internal/modproxy/modproxy.go b/internal/modproxy/modproxy.go index a9808ae..9a7a736 100644 --- a/internal/modproxy/modproxy.go +++ b/internal/modproxy/modproxy.go @@ -163,7 +163,24 @@ 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 @@ -171,10 +188,11 @@ func Latest(modpath string, cached bool) (*Module, error) { 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 { @@ -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") } @@ -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 diff --git a/internal/modproxy/modproxy_test.go b/internal/modproxy/modproxy_test.go index eb9a733..a9561ca 100644 --- a/internal/modproxy/modproxy_test.go +++ b/internal/modproxy/modproxy_test.go @@ -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) } diff --git a/main.go b/main.go index 50026b0..f6ed65f 100644 --- a/main.go +++ b/main.go @@ -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 }