kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/getmodules/subdir.go (about)

     1  package getmodules
     2  
     3  import (
     4  	"path"
     5  
     6  	getter "github.com/hashicorp/go-getter"
     7  )
     8  
     9  // SplitPackageSubdir detects whether the given address string has a
    10  // subdirectory portion, and if so returns a non-empty subDir string
    11  // along with the trimmed package address.
    12  //
    13  // If the given string doesn't have a subdirectory portion then it'll
    14  // just be returned verbatim in packageAddr, with an empty subDir value.
    15  //
    16  // Although the rest of this package is focused only on direct remote
    17  // module packages, this particular function and its companion
    18  // ExpandSubdirGlobs are both also relevant for registry-based module
    19  // addresses, because a registry translates such an address into a
    20  // remote module package address and thus can contribute its own
    21  // additions to the final subdirectory selection.
    22  func SplitPackageSubdir(given string) (packageAddr, subDir string) {
    23  	// We delegate this mostly to go-getter, because older Terraform
    24  	// versions just used go-getter directly and so we need to preserve
    25  	// its various quirks for compatibility reasons.
    26  	//
    27  	// However, note that in Terraform we _always_ split off the subdirectory
    28  	// portion and handle it within Terraform-level code, _never_ passing
    29  	// a subdirectory portion down into go-getter's own Get function, because
    30  	// Terraform's ability to refer between local paths inside the same
    31  	// package depends on Terraform itself always being aware of where the
    32  	// package's root directory ended up on disk, and always needs the
    33  	// package installed wholesale.
    34  	packageAddr, subDir = getter.SourceDirSubdir(given)
    35  	if subDir != "" {
    36  		subDir = path.Clean(subDir)
    37  	}
    38  	return packageAddr, subDir
    39  }
    40  
    41  // ExpandSubdirGlobs handles a subdir string that might contain glob syntax,
    42  // turning it into a concrete subdirectory path by referring to the actual
    43  // files on disk in the given directory which we assume contains the content
    44  // of whichever package this is a subdirectory glob for.
    45  //
    46  // Subdir globs are used, for example, when a module registry wants to specify
    47  // to select the contents of the single directory at the root of a conventional
    48  // tar archive but it doesn't actually know the exact name of that directory.
    49  // In that case it might specify a subdir of just "*", which this function
    50  // will then expand into the single subdirectory found inside instDir, or
    51  // return an error if the result would be ambiguous.
    52  func ExpandSubdirGlobs(instDir string, subDir string) (string, error) {
    53  	// We just delegate this entirely to go-getter, because older Terraform
    54  	// versions just used go-getter directly and so we need to preserve
    55  	// its various quirks for compatibility reasons.
    56  	return getter.SubdirGlob(instDir, subDir)
    57  }