github.com/opentofu/opentofu@v1.7.1/internal/getmodules/subdir.go (about)

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