Skip to content

Commit

Permalink
[runx] Allow unknown file types (#403)
Browse files Browse the repository at this point in the history
## Summary

This tweaks the logic in
#401 to allow unknown file
types if there are no known file types in the list of released
artifacts. This is needed because some releases don't have file types at
all (e.g. https://github.com/mvdan/gofumpt/releases). We try to return a
known archive, otherwise we just return the last artifact that matches
the platform (if any)

Note: this is a revert to how the logic behaved before
#401 but keeps the artifact
name improvements from that PR.

cc: @edorizzardi

## How was it tested?

* `runx --install mvdan/gofumpt`
* unit tests
  • Loading branch information
mikeland73 authored Oct 25, 2024
1 parent d52ab50 commit 54b7954
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion pkg/runx/devbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"shell": {
"scripts": {
"build": "go build -o dist/runx cmd/runx/main.go",
"build-pkg": "go build -o dist/pkg cmd/pkg/main.go"
"build-pkg": "go build -o dist/pkg cmd/pkg/main.go",
"test": "go test ./..."
}
}
}
14 changes: 8 additions & 6 deletions pkg/runx/impl/registry/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ import (
)

func findArtifactForPlatform(artifacts []types.ArtifactMetadata, platform types.Platform) (types.ArtifactMetadata, error) {
foundPlatform := false
var artifactForPlatform types.ArtifactMetadata
for _, artifact := range artifacts {
if isArtifactForPlatform(artifact.Name, platform) {
foundPlatform = true
artifactForPlatform = artifact
if isKnownArchive(artifact.Name) {
// We only consider known archives because sometimes releases contain multiple files
// We prefer known archives because sometimes releases contain multiple files
// for the same platform. Some times those files are alternative installation methods
// like `.dmg`, `.msi`, or `.deb`, and sometimes they are metadata files like `.sha256`
// or a `.sig` file. We don't want to install those.
// If no known archive is found, but the platform is supported, we return the last artifact
// with a matching platform. This is useful when releases don't have extensions.
return artifact, nil
}
}
}
if !foundPlatform {
return types.ArtifactMetadata{}, types.ErrPlatformNotSupported
if artifactForPlatform.Name != "" {
return artifactForPlatform, nil
}
return types.ArtifactMetadata{}, types.ErrNoKnownArchive
return types.ArtifactMetadata{}, types.ErrPlatformNotSupported
}

func isArtifactForPlatform(artifactName string, platform types.Platform) bool {
Expand Down
4 changes: 2 additions & 2 deletions pkg/runx/impl/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func TestFindArtifactForPlatform(t *testing.T) {
errTypeWant error
}{
{[]types.ArtifactMetadata{{Name: "mac-amd64.tar.gz"}}, types.NewPlatform("darwin", "amd64"), true, nil},
{[]types.ArtifactMetadata{{Name: "linux-amd64.deb"}}, types.NewPlatform("linux", "amd64"), false, types.ErrNoKnownArchive},
{[]types.ArtifactMetadata{{Name: "linux-amd64"}}, types.NewPlatform("linux", "amd64"), false, types.ErrNoKnownArchive},
{[]types.ArtifactMetadata{{Name: "linux-amd64.deb"}}, types.NewPlatform("linux", "amd64"), true, nil},
{[]types.ArtifactMetadata{{Name: "linux-amd64"}}, types.NewPlatform("linux", "amd64"), true, nil},
{[]types.ArtifactMetadata{{Name: "darwin_arm64.tar"}}, types.NewPlatform("windows", "amd64"), false, types.ErrPlatformNotSupported},
{[]types.ArtifactMetadata{{Name: "darwin_arm64"}}, types.NewPlatform("windows", "amd64"), false, types.ErrPlatformNotSupported},
{[]types.ArtifactMetadata{{Name: "mac-amd64.tar.gz"}}, types.NewPlatform("", ""), false, types.ErrPlatformNotSupported},
Expand Down
1 change: 0 additions & 1 deletion pkg/runx/impl/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ import "errors"
var ErrPackageNotFound = errors.New("package not found")
var ErrReleaseNotFound = errors.New("release not found")
var ErrPlatformNotSupported = errors.New("package doesn't support platform")
var ErrNoKnownArchive = errors.New("package doesn't come in a known archive")

0 comments on commit 54b7954

Please sign in to comment.