mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
1a8da65314
It's been a long while since we gave close attention to the codepaths for module source address parsing and external module package installation. Due to their age, these codepaths often diverged from our modern practices such as representing address types in the addrs package, and encapsulating package installation details only in a particular location. In particular, this refactor makes source address parsing a separate step from module installation, which therefore makes the result of that parsing available to other Terraform subsystems which work with the configuration representation objects. This also presented the opportunity to better encapsulate our use of go-getter into a new package "getmodules" (echoing "getproviders"), which is intended to be the only part of Terraform that directly interacts with go-getter. This is largely just a refactor of the existing functionality into a new code organization, but there is one notable change in behavior here: the source address parsing now happens during configuration loading rather than module installation, which may cause errors about invalid addresses to be returned in different situations than before. That counts as backward compatible because we only promise to remain compatible with configurations that are _valid_, which means that they can be initialized, planned, and applied without any errors. This doesn't introduce any new error cases, and instead just makes a pre-existing error case be detected earlier. Our module registry client is still using its own special module address type from registry/regsrc for now, with a small shim from the new addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also rework the registry client to work with the new address type, but this commit is already big enough as it is.
66 lines
2.2 KiB
Go
66 lines
2.2 KiB
Go
package getmodules
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"runtime"
|
|
)
|
|
|
|
// fileDetector is a replacement for go-getter's own file detector which
|
|
// better meets Terraform's needs: specifically, it rejects relative filesystem
|
|
// paths with a somewhat-decent error message.
|
|
//
|
|
// This is a replacement for some historical hackery we did where we tried to
|
|
// avoid calling into go-getter altogether in this situation. This is,
|
|
// therefore, a copy of getter.FileDetector but with the "not absolute path"
|
|
// case replaced with a similar result as Terraform's old heuristic would've
|
|
// returned: a custom error type that the caller can react to in order to
|
|
// produce a hint error message if desired.
|
|
type fileDetector struct{}
|
|
|
|
func (d *fileDetector) Detect(src, pwd string) (string, bool, error) {
|
|
if len(src) == 0 {
|
|
return "", false, nil
|
|
}
|
|
|
|
if !filepath.IsAbs(src) {
|
|
return "", true, &MaybeRelativePathErr{src}
|
|
}
|
|
|
|
return fmtFileURL(src), true, nil
|
|
}
|
|
|
|
func fmtFileURL(path string) string {
|
|
if runtime.GOOS == "windows" {
|
|
// Make sure we're using "/" on Windows. URLs are "/"-based.
|
|
path = filepath.ToSlash(path)
|
|
return fmt.Sprintf("file://%s", path)
|
|
}
|
|
|
|
// Make sure that we don't start with "/" since we add that below.
|
|
if path[0] == '/' {
|
|
path = path[1:]
|
|
}
|
|
return fmt.Sprintf("file:///%s", path)
|
|
}
|
|
|
|
// MaybeRelativePathErr is the error type returned by NormalizePackageAddress
|
|
// if the source address looks like it might be intended to be a relative
|
|
// filesystem path but without the required "./" or "../" prefix.
|
|
//
|
|
// Specifically, NormalizePackageAddress will return a pointer to this type,
|
|
// so the error type will be *MaybeRelativePathErr.
|
|
//
|
|
// It has a name starting with "Maybe" because in practice we can get here
|
|
// with any string that isn't recognized as one of the supported schemes:
|
|
// treating the address as a local filesystem path is our fallback for when
|
|
// everything else fails, but it could just as easily be a typo in an attempt
|
|
// to use one of the other schemes and thus not a filesystem path at all.
|
|
type MaybeRelativePathErr struct {
|
|
Addr string
|
|
}
|
|
|
|
func (e *MaybeRelativePathErr) Error() string {
|
|
return fmt.Sprintf("Terraform cannot detect a supported external module source type for %s", e.Addr)
|
|
}
|