mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
40ec62c139
Earlier work to make "terraform init" interruptible made the getproviders package context-aware in order to allow provider installation to be cancelled. Here we make a similar change for module installation, which is now also cancellable with SIGINT. This involves plumbing context through initwd and getmodules. Functions which can make network requests now include a context parameter whose cancellation cancels those requests. Since the module installation code is shared, "terraform get" is now also interruptible during module installation.
45 lines
1.8 KiB
Go
45 lines
1.8 KiB
Go
package getmodules
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// PackageFetcher is a low-level utility for fetching remote module packages
|
|
// into local filesystem directories in preparation for use by higher-level
|
|
// module installer functionality implemented elsewhere.
|
|
//
|
|
// A PackageFetcher works only with entire module packages and never with
|
|
// the individual modules within a package.
|
|
//
|
|
// A particular PackageFetcher instance remembers the target directory of
|
|
// any successfully-installed package so that it can optimize future calls
|
|
// that have the same package address by copying the local directory tree,
|
|
// rather than fetching the package from its origin repeatedly. There is
|
|
// no way to reset this cache, so a particular PackageFetcher instance should
|
|
// live only for the duration of a single initialization process.
|
|
type PackageFetcher struct {
|
|
getter reusingGetter
|
|
}
|
|
|
|
func NewPackageFetcher() *PackageFetcher {
|
|
return &PackageFetcher{
|
|
getter: reusingGetter{},
|
|
}
|
|
}
|
|
|
|
// FetchPackage downloads or otherwise retrieves the filesystem inside the
|
|
// package at the given address into the given local installation directory.
|
|
//
|
|
// packageAddr must be formatted as if it were the result of an
|
|
// addrs.ModulePackage.String() call. It's only defined as a raw string here
|
|
// because the getmodules package can't import the addrs package due to
|
|
// that creating a package dependency cycle.
|
|
//
|
|
// PackageFetcher only works with entire packages. If the caller is processing
|
|
// a module source address which includes a subdirectory portion then the
|
|
// caller must resolve that itself, possibly with the help of the
|
|
// getmodules.SplitPackageSubdir and getmodules.ExpandSubdirGlobs functions.
|
|
func (f *PackageFetcher) FetchPackage(ctx context.Context, instDir string, packageAddr string) error {
|
|
return f.getter.getWithGoGetter(ctx, instDir, packageAddr)
|
|
}
|