github.com/pulumi/terraform@v1.4.0/pkg/getmodules/package.go (about)

     1  package getmodules
     2  
     3  import (
     4  	getter "github.com/hashicorp/go-getter"
     5  )
     6  
     7  // NormalizePackageAddress uses the go-getter "detector" functionality in
     8  // order to turn a user-supplied source address into a normalized address
     9  // which always includes a prefix naming a protocol to fetch with and may
    10  // also include a transformed/normalized version of the protocol-specific
    11  // source address included afterward.
    12  //
    13  // This is part of the implementation of addrs.ParseModulePackage and of
    14  // addrs.ParseModuleSource, so for most callers it'd be better to call
    15  // one of those other functions instead. The addrs package can potentially
    16  // perform other processing in addition to just the go-getter detection.
    17  //
    18  // Note that this function expects to recieve only a package address, not
    19  // a full source address that might also include a subdirectory portion.
    20  // The caller must trim off any subdirectory portion using
    21  // getmodules.SplitPackageSubdir before calling this function, passing in
    22  // just the packageAddr return value, or the result will be incorrect.
    23  //
    24  // The detectors in go-getter can potentially introduce their own
    25  // package subdirectory portions. If that happens then this function will
    26  // return the subdirectory portion as a non-empty subDir return value,
    27  // which the caller must then use as a prefix for any subDir it already
    28  // extracted from the user's given package address.
    29  //
    30  // Some of go-getter's detectors make outgoing HTTP requests, and so
    31  // the behavior of this function may depend on the network connectivity
    32  // of the system where Terraform is running. However, most of the getters
    33  // we use are local-only, and so HTTP requests are only for some ambiguous
    34  // edge-cases, such as the BitBucket detector which has a mechanism to
    35  // detect whether to use Git or Mercurial, because earlier versions of
    36  // BitBucket used to support both.
    37  func NormalizePackageAddress(given string) (packageAddr, subDir string, err error) {
    38  	// Because we're passing go-getter no base directory here, the file
    39  	// detector will return an error if the user entered a relative filesystem
    40  	// path without a "../" or "./" prefix and thus ended up in here.
    41  	//
    42  	// go-getter's error message for that case is very poor, and so we'll
    43  	// try to heuristically detect that situation and return a better error
    44  	// message.
    45  
    46  	// NOTE: We're passing an empty string to the "current working directory"
    47  	// here because that's only relevant for relative filesystem paths,
    48  	// but Terraform handles relative filesystem paths itself outside of
    49  	// go-getter and so it'd always be an error to pass one into here.
    50  	// go-getter's "file" detector returns an error if it encounters a
    51  	// relative path when the pwd argument is empty.
    52  	//
    53  	// (Absolute filesystem paths _are_ valid though, for annoying historical
    54  	// reasons, and we treat them as remote packages even though "downloading"
    55  	// them just means a recursive copy of the source directory tree.)
    56  
    57  	result, err := getter.Detect(given, "", goGetterDetectors)
    58  	if err != nil {
    59  		// NOTE: go-getter's error messages are of very inconsistent quality
    60  		// and many are not suitable for an end-user audience, but they are all
    61  		// just strings and so we can't really do any sort of post-processing
    62  		// to improve them and thus we just accept some bad error messages for
    63  		// now.
    64  		return "", "", err
    65  	}
    66  
    67  	packageAddr, subDir = SplitPackageSubdir(result)
    68  	return packageAddr, subDir, nil
    69  }