github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/getmodules/installer.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package getmodules
     5  
     6  import (
     7  	"context"
     8  )
     9  
    10  // PackageFetcher is a low-level utility for fetching remote module packages
    11  // into local filesystem directories in preparation for use by higher-level
    12  // module installer functionality implemented elsewhere.
    13  //
    14  // A PackageFetcher works only with entire module packages and never with
    15  // the individual modules within a package.
    16  //
    17  // A particular PackageFetcher instance remembers the target directory of
    18  // any successfully-installed package so that it can optimize future calls
    19  // that have the same package address by copying the local directory tree,
    20  // rather than fetching the package from its origin repeatedly. There is
    21  // no way to reset this cache, so a particular PackageFetcher instance should
    22  // live only for the duration of a single initialization process.
    23  type PackageFetcher struct {
    24  	getter reusingGetter
    25  }
    26  
    27  func NewPackageFetcher() *PackageFetcher {
    28  	return &PackageFetcher{
    29  		getter: reusingGetter{},
    30  	}
    31  }
    32  
    33  // FetchPackage downloads or otherwise retrieves the filesystem inside the
    34  // package at the given address into the given local installation directory.
    35  //
    36  // packageAddr must be formatted as if it were the result of an
    37  // addrs.ModulePackage.String() call. It's only defined as a raw string here
    38  // because the getmodules package can't import the addrs package due to
    39  // that creating a package dependency cycle.
    40  //
    41  // PackageFetcher only works with entire packages. If the caller is processing
    42  // a module source address which includes a subdirectory portion then the
    43  // caller must resolve that itself, possibly with the help of the
    44  // getmodules.SplitPackageSubdir and getmodules.ExpandSubdirGlobs functions.
    45  func (f *PackageFetcher) FetchPackage(ctx context.Context, instDir string, packageAddr string) error {
    46  	return f.getter.getWithGoGetter(ctx, instDir, packageAddr)
    47  }