Skip to content

Commit

Permalink
Don't filter when there's a single asset (#94)
Browse files Browse the repository at this point in the history
* Don't filter when there's a single asset

Fixes #93

Temporary fix until we properly implement #67

* Add dry-run mode to update command
  • Loading branch information
marcosnils authored Apr 10, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 192528b commit f241c64
Showing 3 changed files with 78 additions and 46 deletions.
2 changes: 2 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -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 {
98 changes: 52 additions & 46 deletions pkg/assets/assets.go
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions pkg/assets/assets_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

}

0 comments on commit f241c64

Please sign in to comment.