Skip to content

Commit

Permalink
add allow-unresolved-version go-proxy config option (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman authored Oct 1, 2023
1 parent 7786516 commit 94438a5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@ The `version.want` option allows a special entry:

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:

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

The `version.want` option allows a special entry:
- `latest`: don't pin to a version, use the latest available
26 changes: 21 additions & 5 deletions tool/goproxy/version_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/anchore/binny/internal/log"
)

const latest = "latest"

var _ binny.VersionResolver = (*VersionResolver)(nil)

type VersionResolver struct {
Expand All @@ -19,7 +21,8 @@ type VersionResolver struct {
}

type VersionResolutionParameters struct {
Module string `json:"module" yaml:"module" mapstructure:"module"`
Module string `json:"module" yaml:"module" mapstructure:"module"`
AllowUnresolvedVersion bool `json:"allow-unresolved-version" yaml:"allow-unresolved-version" mapstructure:"allow-unresolved-version"`
}

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

if want == "latest" {
if want == latest {
return v.findLatestVersion("")
}

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

func (v VersionResolver) UpdateVersion(want, constraint string) (string, error) {
if want == "latest" {
if want == latest {
if constraint != "" {
return "", fmt.Errorf("cannot specify a version constraint with 'latest' go module version")
}
Expand Down Expand Up @@ -75,8 +78,21 @@ func (v VersionResolver) findLatestVersion(versionConstraint string) (string, er
return "", fmt.Errorf("failed to filter latest version: %v", err)
}

log.WithFields("latest", latestVersion, "module", v.config.Module).
Trace("found latest version from the go proxy")
if latestVersion != "" {
log.WithFields(latest, latestVersion, "module", v.config.Module).
Trace("found latest version from the go proxy")
} else {
log.WithFields("module", v.config.Module).Trace("could not resolve latest version from go proxy")
}

if latestVersion == "" {
if v.config.AllowUnresolvedVersion {
// this can happen if the source repo has no tags, the proxy then won't know about it.
log.WithFields("module", v.config.Module).Trace("using 'latest' as the version")
return latest, nil
}
return "", fmt.Errorf("could not resolve latest version for module %q", v.config.Module)
}

return latestVersion, nil
}
Expand Down
23 changes: 23 additions & 0 deletions tool/goproxy/version_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ func TestVersionResolver_ResolveVersion(t *testing.T) {
version: "bogus",
want: "bogus",
},
{
name: "do not allow for unresolved versions",
config: VersionResolutionParameters{
Module: "github.com/anchore/binny",
},
version: "latest",
wantErr: require.Error,
availableVersionsFetcher: func(url string) ([]string, error) {
return []string{""}, nil
},
},
{
name: "allow for unresolved versions",
config: VersionResolutionParameters{
Module: "github.com/anchore/binny",
AllowUnresolvedVersion: true,
},
version: "latest",
want: "latest", // this is a pass through to go-install, which supports this as input
availableVersionsFetcher: func(url string) ([]string, error) {
return []string{""}, nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 94438a5

Please sign in to comment.