Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't filter when there's a single asset #94

Merged
merged 3 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func newUpdateCmd() *updateCmd {
if len(toUpdate) == 0 {
log.Infof("All binaries are up to date")
return nil
} else if root.opts.dryRun {
return fmt.Errorf("Command aborted, dry-run mode")
}

if root.opts.dryRun {
Expand Down
98 changes: 52 additions & 46 deletions pkg/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,58 +87,64 @@ func (g FilteredAsset) String() string {
// in case it can't determine it
func FilterAssets(repoName string, as []*Asset) (*FilteredAsset, error) {
matches := []*FilteredAsset{}
scores := map[string]int{}
scores[repoName] = 1
for _, os := range resolver.GetOS() {
scores[os] = 10
}
for _, arch := range resolver.GetArch() {
scores[arch] = 5
}
for _, osSpecificExtension := range resolver.GetOSSpecificExtensions() {
scores[osSpecificExtension] = 15
}
scoreKeys := []string{}
for key := range scores {
scoreKeys = append(scoreKeys, strings.ToLower(key))
}
for _, a := range as {
urlPathBasename := path.Base(a.URL)
highestScoreForAsset := 0
gf := &FilteredAsset{RepoName: repoName, Name: a.Name, DisplayName: a.DisplayName, URL: a.URL, score: 0}
for _, candidate := range []string{a.Name, urlPathBasename} {
candidateScore := 0
if bstrings.ContainsAny(strings.ToLower(candidate), scoreKeys) &&
isSupportedExt(candidate) {
for toMatch, score := range scores {
if strings.Contains(strings.ToLower(candidate), strings.ToLower(toMatch)) {
candidateScore += score
if len(as) == 1 {
a := as[0]
matches = append(matches, &FilteredAsset{RepoName: repoName, Name: a.Name, URL: a.URL, score: 0})
} else {
scores := map[string]int{}
scores[repoName] = 1
for _, os := range resolver.GetOS() {
scores[os] = 10
}
for _, arch := range resolver.GetArch() {
scores[arch] = 5
}
for _, osSpecificExtension := range resolver.GetOSSpecificExtensions() {
scores[osSpecificExtension] = 15
}
scoreKeys := []string{}
for key := range scores {
scoreKeys = append(scoreKeys, strings.ToLower(key))
}
for _, a := range as {
urlPathBasename := path.Base(a.URL)
highestScoreForAsset := 0
gf := &FilteredAsset{RepoName: repoName, Name: a.Name, DisplayName: a.DisplayName, URL: a.URL, score: 0}
for _, candidate := range []string{a.Name, urlPathBasename} {
candidateScore := 0
if bstrings.ContainsAny(strings.ToLower(candidate), scoreKeys) &&
isSupportedExt(candidate) {
for toMatch, score := range scores {
if strings.Contains(strings.ToLower(candidate), strings.ToLower(toMatch)) {
candidateScore += score
}
}
if candidateScore > highestScoreForAsset {
highestScoreForAsset = candidateScore
gf.Name = candidate
gf.score = candidateScore
}
}
if candidateScore > highestScoreForAsset {
highestScoreForAsset = candidateScore
gf.Name = candidate
gf.score = candidateScore
}
}
if highestScoreForAsset > 0 {
matches = append(matches, gf)
}
}
if highestScoreForAsset > 0 {
matches = append(matches, gf)
}
}
highestAssetScore := 0
for i := range matches {
if matches[i].score > highestAssetScore {
highestAssetScore = matches[i].score
highestAssetScore := 0
for i := range matches {
if matches[i].score > highestAssetScore {
highestAssetScore = matches[i].score
}
}
}
for i := len(matches) - 1; i >= 0; i-- {
if matches[i].score < highestAssetScore {
log.Debugf("Removing %v (URL %v) with score %v lower than %v", matches[i].Name, matches[i].URL, matches[i].score, highestAssetScore)
matches = append(matches[:i], matches[i+1:]...)
} else {
log.Debugf("Keeping %v (URL %v) with highest score %v", matches[i].Name, matches[i].URL, matches[i].score)
for i := len(matches) - 1; i >= 0; i-- {
if matches[i].score < highestAssetScore {
log.Debugf("Removing %v (URL %v) with score %v lower than %v", matches[i].Name, matches[i].URL, matches[i].score, highestAssetScore)
matches = append(matches[:i], matches[i+1:]...)
} else {
log.Debugf("Keeping %v (URL %v) with highest score %v", matches[i].Name, matches[i].URL, matches[i].score)
}
}

}

var gf *FilteredAsset
Expand Down
24 changes: 24 additions & 0 deletions pkg/assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,27 @@ func TestIsSupportedExt(t *testing.T) {
}

}

func TestFilterSingleAsset(t *testing.T) {
type args struct {
repoName string
as []*Asset
}
cases := []struct {
in args
out string
}{
{args{"cli", []*Asset{
{Name: "dapr", URL: ""},
}}, "dapr"},
}

for _, c := range cases {
if n, err := FilterAssets(c.in.repoName, c.in.as); err != nil {
t.Fatalf("Error filtering asset [%v]", err)
} else if n.Name != c.out {
t.Fatalf("Error filtering %+v: %+v does not match %s", c.in, n, c.out)
}
}

}