Skip to content

Commit 94438a5

Browse files
authored
add allow-unresolved-version go-proxy config option (#8)
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
1 parent 7786516 commit 94438a5

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ The `version.want` option allows a special entry:
205205

206206
The `go-proxy` version method reaches out to `proxy.golang.org` to determine the latest version of a Go module. It requires the following configuration options:
207207

208-
| Option | Description |
209-
|--------|--------------------------------------------------------------------------------------------------|
210-
| `module` | The FQDN to the Go module (e.g. `github.com/anchore/syft`) |
208+
| Option | Description |
209+
|--------|----------------------------------------------------------------------------------------------------------------------|
210+
| `module` | The FQDN to the Go module (e.g. `github.com/anchore/syft`) |
211+
| `allow-unresolved-version` | If the latest version cannot be found by the proxy allow for "latest" as a valid value (which `go install` supports) |
211212

212213
The `version.want` option allows a special entry:
213214
- `latest`: don't pin to a version, use the latest available

tool/goproxy/version_resolver.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/anchore/binny/internal/log"
1212
)
1313

14+
const latest = "latest"
15+
1416
var _ binny.VersionResolver = (*VersionResolver)(nil)
1517

1618
type VersionResolver struct {
@@ -19,7 +21,8 @@ type VersionResolver struct {
1921
}
2022

2123
type VersionResolutionParameters struct {
22-
Module string `json:"module" yaml:"module" mapstructure:"module"`
24+
Module string `json:"module" yaml:"module" mapstructure:"module"`
25+
AllowUnresolvedVersion bool `json:"allow-unresolved-version" yaml:"allow-unresolved-version" mapstructure:"allow-unresolved-version"`
2326
}
2427

2528
func NewVersionResolver(cfg VersionResolutionParameters) *VersionResolver {
@@ -35,7 +38,7 @@ func (v VersionResolver) ResolveVersion(want, _ string) (string, error) {
3538
return want, nil
3639
}
3740

38-
if want == "latest" {
41+
if want == latest {
3942
return v.findLatestVersion("")
4043
}
4144

@@ -45,7 +48,7 @@ func (v VersionResolver) ResolveVersion(want, _ string) (string, error) {
4548
}
4649

4750
func (v VersionResolver) UpdateVersion(want, constraint string) (string, error) {
48-
if want == "latest" {
51+
if want == latest {
4952
if constraint != "" {
5053
return "", fmt.Errorf("cannot specify a version constraint with 'latest' go module version")
5154
}
@@ -75,8 +78,21 @@ func (v VersionResolver) findLatestVersion(versionConstraint string) (string, er
7578
return "", fmt.Errorf("failed to filter latest version: %v", err)
7679
}
7780

78-
log.WithFields("latest", latestVersion, "module", v.config.Module).
79-
Trace("found latest version from the go proxy")
81+
if latestVersion != "" {
82+
log.WithFields(latest, latestVersion, "module", v.config.Module).
83+
Trace("found latest version from the go proxy")
84+
} else {
85+
log.WithFields("module", v.config.Module).Trace("could not resolve latest version from go proxy")
86+
}
87+
88+
if latestVersion == "" {
89+
if v.config.AllowUnresolvedVersion {
90+
// this can happen if the source repo has no tags, the proxy then won't know about it.
91+
log.WithFields("module", v.config.Module).Trace("using 'latest' as the version")
92+
return latest, nil
93+
}
94+
return "", fmt.Errorf("could not resolve latest version for module %q", v.config.Module)
95+
}
8096

8197
return latestVersion, nil
8298
}

tool/goproxy/version_resolver_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ func TestVersionResolver_ResolveVersion(t *testing.T) {
4444
version: "bogus",
4545
want: "bogus",
4646
},
47+
{
48+
name: "do not allow for unresolved versions",
49+
config: VersionResolutionParameters{
50+
Module: "github.com/anchore/binny",
51+
},
52+
version: "latest",
53+
wantErr: require.Error,
54+
availableVersionsFetcher: func(url string) ([]string, error) {
55+
return []string{""}, nil
56+
},
57+
},
58+
{
59+
name: "allow for unresolved versions",
60+
config: VersionResolutionParameters{
61+
Module: "github.com/anchore/binny",
62+
AllowUnresolvedVersion: true,
63+
},
64+
version: "latest",
65+
want: "latest", // this is a pass through to go-install, which supports this as input
66+
availableVersionsFetcher: func(url string) ([]string, error) {
67+
return []string{""}, nil
68+
},
69+
},
4770
}
4871
for _, tt := range tests {
4972
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)