Skip to content

Commit

Permalink
Improve XP when using --all flag.
Browse files Browse the repository at this point in the history
This commit improves handling on the following cases:

- It filters directories from Zip extraction
- It handlers better the --all flag by showing exactly all the files
without any kind of filtering. Previously, even though that flag was
added, we were still doing some filtering based on the repo name

Fixes #103
  • Loading branch information
marcosnils committed Jun 19, 2021
1 parent 74bf928 commit 09b85d3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
85 changes: 46 additions & 39 deletions pkg/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (f *Filter) FilterAssets(repoName string, as []*Asset) (*FilteredAsset, err
a := as[0]
matches = append(matches, &FilteredAsset{RepoName: repoName, Name: a.Name, URL: a.URL, score: 0})
} else {
scores := map[string]int{}
scoreKeys := []string{}
scores[repoName] = 1

if !f.opts.SkipScoring {
scores := map[string]int{}
scoreKeys := []string{}
scores[repoName] = 1
for _, os := range resolver.GetOS() {
scores[os] = 10
}
Expand All @@ -117,50 +117,55 @@ func (f *Filter) FilterAssets(repoName string, as []*Asset) (*FilteredAsset, err
for _, osSpecificExtension := range resolver.GetOSSpecificExtensions() {
scores[osSpecificExtension] = 15
}
} else {
log.Debugf("--all flag was supplied, skipping scoring")
}

for key := range scores {
scoreKeys = append(scoreKeys, strings.ToLower(key))
}
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
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)

} else {
log.Debugf("--all flag was supplied, skipping scoring")
for _, a := range as {
matches = append(matches, &FilteredAsset{RepoName: repoName, Name: a.Name, DisplayName: a.DisplayName, URL: a.URL, score: 0})
}
}

Expand Down Expand Up @@ -372,6 +377,8 @@ func (f *Filter) processZip(name string, r io.Reader) (string, io.Reader, error)
break
} else if err != nil {
return "", nil, err
} else if header.Mode().IsDir() {
continue
}

bs, err := ioutil.ReadAll(zr)
Expand Down
5 changes: 4 additions & 1 deletion pkg/assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ func TestFilterAssets(t *testing.T) {
}}, "dapr", testLinuxAMDResolver},
}

f := NewFilter(&FilterOpts{})
f := NewFilter(&FilterOpts{SkipScoring: false})
for _, c := range cases {
resolver = c.resolver
if n, err := f.FilterAssets(c.in.repoName, c.in.as); err != nil {
for _, a := range c.in.as {
fmt.Println(a.Name, c.resolver)
}
t.Fatalf("Error filtering assets %v", err)
} else if n.Name != c.out {
t.Fatalf("Error filtering %+v: %+v does not match %s", c.in, n, c.out)
Expand Down

0 comments on commit 09b85d3

Please sign in to comment.