Skip to content

Commit

Permalink
Merge pull request #221 from safing/fix/version-selection
Browse files Browse the repository at this point in the history
Fix version selection
  • Loading branch information
dhaavi authored Oct 2, 2023
2 parents 433ad6b + 85db3d9 commit 01b03aa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
8 changes: 6 additions & 2 deletions updater/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ type ResourceRegistry struct {
// version. Even if false, a pre-release version will still be used if it is
// defined as the current version by an index.
UsePreReleases bool
DevMode bool
Online bool

// DevMode specifies if a local 0.0.0 version should be always chosen, when available.
DevMode bool

// Online specifies if resources may be downloaded if not available locally.
Online bool

// StateNotifyFunc may be set to receive any changes to the registry state.
// The specified function may lock the state, but may not block or take a
Expand Down
30 changes: 27 additions & 3 deletions updater/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,26 @@ func (rv *ResourceVersion) EqualsVersion(version string) bool {
// A version is selectable if it's not blacklisted and either already locally
// available or ready to be downloaded.
func (rv *ResourceVersion) isSelectable() bool {
return !rv.Blacklisted && (rv.Available || rv.resource.registry.Online)
switch {
case rv.Blacklisted:
// Should not be used.
return false
case rv.Available:
// Is available locally, use!
return true
case !rv.resource.registry.Online:
// Cannot download, because registry is set to offline.
return false
case rv.resource.Index == nil:
// Cannot download, because resource is not part of an index.
return false
case !rv.resource.Index.AutoDownload:
// Cannot download, because index may not automatically download.
return false
default:
// Is not available locally, but we are allowed to download it on request!
return true
}
}

// isBetaVersionNumber checks if rv is marked as a beta version by checking
Expand Down Expand Up @@ -290,8 +309,13 @@ func (res *Resource) selectVersion() {
sort.Sort(res)

// export after we finish
var fallback bool
defer func() {
log.Tracef("updater: selected version %s for resource %s", res.SelectedVersion, res.Identifier)
if fallback {
log.Tracef("updater: selected version %s (as fallback) for resource %s", res.SelectedVersion, res.Identifier)
} else {
log.Debugf("updater: selected version %s for resource %s", res.SelectedVersion, res.Identifier)
}

if res.inUse() &&
res.SelectedVersion != res.ActiveVersion && // new selected version does not match previously selected version
Expand Down Expand Up @@ -356,7 +380,7 @@ func (res *Resource) selectVersion() {

// 5) Default to newest.
res.SelectedVersion = res.Versions[0]
log.Warningf("updater: falling back to version %s for %s because we failed to find a selectable one", res.SelectedVersion, res.Identifier)
fallback = true
}

// Blacklist blacklists the specified version and selects a new version.
Expand Down
2 changes: 2 additions & 0 deletions updater/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func TestVersionSelection(t *testing.T) {
registry.UsePreReleases = true
registry.DevMode = true
registry.Online = true
res.Index = &Index{AutoDownload: true}

res.selectVersion()
if res.SelectedVersion.VersionNumber != "0.0.0" {
t.Errorf("selected version should be 0.0.0, not %s", res.SelectedVersion.VersionNumber)
Expand Down
12 changes: 6 additions & 6 deletions updater/updating.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (reg *ResourceRegistry) UpdateIndexes(ctx context.Context) error {
// Get pending resources and update status.
pendingResourceVersions, _ := reg.GetPendingDownloads(true, false)
reg.state.ReportUpdateCheck(
identifiersFromResourceVersions(pendingResourceVersions),
humanInfoFromResourceVersions(pendingResourceVersions),
nil,
)

Expand Down Expand Up @@ -183,14 +183,14 @@ func (reg *ResourceRegistry) downloadIndex(ctx context.Context, client *http.Cli
}

// DownloadUpdates checks if updates are available and downloads updates of used components.
func (reg *ResourceRegistry) DownloadUpdates(ctx context.Context, automaticOnly bool) error {
func (reg *ResourceRegistry) DownloadUpdates(ctx context.Context, includeManual bool) error {
// Start registry operation.
reg.state.StartOperation(StateDownloading)
defer reg.state.EndOperation()

// Get pending updates.
toUpdate, missingSigs := reg.GetPendingDownloads(!automaticOnly, true)
downloadDetailsResources := identifiersFromResourceVersions(toUpdate)
toUpdate, missingSigs := reg.GetPendingDownloads(includeManual, true)
downloadDetailsResources := humanInfoFromResourceVersions(toUpdate)
reg.state.UpdateOperationDetails(&StateDownloadingDetails{
Resources: downloadDetailsResources,
})
Expand Down Expand Up @@ -348,11 +348,11 @@ func (reg *ResourceRegistry) GetPendingDownloads(manual, auto bool) (resources,
return toUpdate, missingSigs
}

func identifiersFromResourceVersions(resourceVersions []*ResourceVersion) []string {
func humanInfoFromResourceVersions(resourceVersions []*ResourceVersion) []string {
identifiers := make([]string, len(resourceVersions))

for i, rv := range resourceVersions {
identifiers[i] = rv.resource.Identifier
identifiers[i] = fmt.Sprintf("%s v%s", rv.resource.Identifier, rv.VersionNumber)
}

return identifiers
Expand Down

0 comments on commit 01b03aa

Please sign in to comment.