getproviders: PackageLocation now knows how to install from itself

Previously we treated PackageLocation only as pure data, describing a
location from which something could be retrieved. Unexported functions in
package providercache then treated PackageLocation values as a closed
union dispatched using a type switch.

That strategy has been okay for our relatively-generic package locations
so far, but in a future commit we intend to add a new kind of package
location referring to a manifest in an OCI distribution repository, and
installing from _that_ will require a nontrivial amount of
OCI-distribution-specific logic that will be more tightly coupled to the
getproviders.Source that will return such locations, and so we're switching
to a model where _the location object itself_ is responsible for knowing
how to install a provider package from its location, as a method of
PackageLocation.

The implementation of installing from each location type now moves from
package providercache to package getproviders, which is arguably a better
thematic home for that functionality anyway.

For now these remain tested only indirectly through the tests in package
providercache, since we didn't previously have any independent tests for
these unexported functions. We might want to add more tightly-scoped unit
tests for these to package getproviders in future, but for now this is not
materially worse than it was before.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins
2024-11-15 14:21:36 -08:00
parent 6f04489f23
commit bcdadc80ee
8 changed files with 387 additions and 355 deletions

View File

@@ -34,18 +34,7 @@ func (d *Dir) InstallPackage(ctx context.Context, meta getproviders.PackageMeta,
d.metaCache = nil
log.Printf("[TRACE] providercache.Dir.InstallPackage: installing %s v%s from %s", meta.Provider, meta.Version, meta.Location)
switch meta.Location.(type) {
case getproviders.PackageHTTPURL:
return installFromHTTPURL(ctx, meta, newPath, allowedHashes)
case getproviders.PackageLocalArchive:
return installFromLocalArchive(ctx, meta, newPath, allowedHashes)
case getproviders.PackageLocalDir:
return installFromLocalDir(ctx, meta, newPath, allowedHashes)
default:
// Should not get here, because the above should be exhaustive for
// all implementations of getproviders.Location.
return nil, fmt.Errorf("don't know how to install from a %T location", meta.Location)
}
return meta.Location.InstallProviderPackage(ctx, meta, newPath, allowedHashes)
}
// LinkFromOtherCache takes a CachedProvider value produced from another Dir
@@ -107,6 +96,6 @@ func (d *Dir) LinkFromOtherCache(entry *CachedProvider, allowedHashes []getprovi
}
// No further hash check here because we already checked the hash
// of the source directory above.
_, err := installFromLocalDir(context.TODO(), meta, newPath, nil)
_, err := meta.Location.InstallProviderPackage(context.TODO(), meta, newPath, nil)
return err
}