-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get versions from Go Proxy (#3269)
* feat: get versions from Go Proxy * fix: sort versions * fix: fix a lint error * fix: exclude pre-release versions * fix: fix goproxy client * fix: fix a lint error * docs: update JSONSchema
- Loading branch information
1 parent
f4c85b2
commit e6c5325
Showing
8 changed files
with
174 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,81 @@ | ||
package versiongetter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/aquaproj/aqua/v2/pkg/config/registry" | ||
"github.com/aquaproj/aqua/v2/pkg/fuzzyfinder" | ||
"github.com/hashicorp/go-version" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type GoGetter struct { | ||
gc GoProxyClient | ||
} | ||
|
||
func NewGoGetter(gc GoProxyClient) *GoGetter { | ||
return &GoGetter{ | ||
gc: gc, | ||
} | ||
} | ||
|
||
type GoProxyClient interface { | ||
List(ctx context.Context, path string) ([]string, error) | ||
} | ||
|
||
func (g *GoGetter) Get(ctx context.Context, logE *logrus.Entry, pkg *registry.PackageInfo, _ []*Filter) (string, error) { //nolint:cyclop | ||
versions, err := g.gc.List(ctx, pkg.GoVersionPath) | ||
if err != nil { | ||
return "", fmt.Errorf("list versions: %w", err) | ||
} | ||
var latest *version.Version | ||
var preLatest *version.Version | ||
for _, vs := range versions { | ||
v, err := version.NewSemver(vs) | ||
if err != nil { | ||
logE.WithError(err).WithField("version", vs).Warn("parse a version") | ||
continue | ||
} | ||
if v.Prerelease() == "" && (latest == nil || v.GreaterThan(latest)) { | ||
latest = v | ||
continue | ||
} | ||
if v.Prerelease() != "" && (preLatest == nil || v.GreaterThan(preLatest)) { | ||
preLatest = v | ||
continue | ||
} | ||
} | ||
if latest != nil { | ||
return latest.Original(), nil | ||
} | ||
if preLatest != nil { | ||
return preLatest.Original(), nil | ||
} | ||
return "", nil | ||
} | ||
|
||
func (g *GoGetter) List(ctx context.Context, logE *logrus.Entry, pkg *registry.PackageInfo, _ []*Filter, _ int) ([]*fuzzyfinder.Item, error) { | ||
vs, err := g.gc.List(ctx, pkg.GoVersionPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("list versions: %w", err) | ||
} | ||
versions := make(version.Collection, 0, len(vs)) | ||
for _, v := range vs { | ||
v, err := version.NewSemver(v) | ||
if err != nil { | ||
logE.WithError(err).WithField("version", vs).Warn("parse a version") | ||
continue | ||
} | ||
versions = append(versions, v) | ||
} | ||
sort.Sort(sort.Reverse(versions)) | ||
items := make([]*fuzzyfinder.Item, len(versions)) | ||
for i, version := range versions { | ||
items[i] = &fuzzyfinder.Item{ | ||
Item: version.Original(), | ||
} | ||
} | ||
return items, 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,43 @@ | ||
package goproxy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type Client struct { | ||
client *http.Client | ||
} | ||
|
||
func New(client *http.Client) *Client { | ||
return &Client{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (c *Client) List(ctx context.Context, path string) ([]string, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://proxy.golang.org/%s/@v/list", path), nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("create a http request: %w", err) | ||
} | ||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("send a http request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("read a response body: %w", err) | ||
} | ||
s := strings.TrimSpace(string(b)) | ||
if s == "" { | ||
return nil, nil | ||
} | ||
return strings.Split(s, "\n"), nil | ||
} |