9 cleanhttp "github.com/hashicorp/go-cleanhttp"
10 getter "github.com/hashicorp/go-getter"
13 // We configure our own go-getter detector and getter sets here, because
14 // the set of sources we support is part of Terraform's documentation and
15 // so we don't want any new sources introduced in go-getter to sneak in here
16 // and work even though they aren't documented. This also insulates us from
17 // any meddling that might be done by other go-getter callers linked into our
20 var goGetterDetectors = []getter.Detector{
21 new(getter.GitHubDetector),
22 new(getter.BitBucketDetector),
23 new(getter.GCSDetector),
24 new(getter.S3Detector),
25 new(getter.FileDetector),
28 var goGetterNoDetectors = []getter.Detector{}
30 var goGetterDecompressors = map[string]getter.Decompressor{
31 "bz2": new(getter.Bzip2Decompressor),
32 "gz": new(getter.GzipDecompressor),
33 "xz": new(getter.XzDecompressor),
34 "zip": new(getter.ZipDecompressor),
36 "tar.bz2": new(getter.TarBzip2Decompressor),
37 "tar.tbz2": new(getter.TarBzip2Decompressor),
39 "tar.gz": new(getter.TarGzipDecompressor),
40 "tgz": new(getter.TarGzipDecompressor),
42 "tar.xz": new(getter.TarXzDecompressor),
43 "txz": new(getter.TarXzDecompressor),
46 var goGetterGetters = map[string]getter.Getter{
47 "file": new(getter.FileGetter),
48 "gcs": new(getter.GCSGetter),
49 "git": new(getter.GitGetter),
50 "hg": new(getter.HgGetter),
51 "s3": new(getter.S3Getter),
52 "http": getterHTTPGetter,
53 "https": getterHTTPGetter,
56 var getterHTTPClient = cleanhttp.DefaultClient()
58 var getterHTTPGetter = &getter.HttpGetter{
59 Client: getterHTTPClient,
63 // A reusingGetter is a helper for the module installer that remembers
64 // the final resolved addresses of all of the sources it has already been
65 // asked to install, and will copy from a prior installation directory if
66 // it has the same resolved source address.
68 // The keys in a reusingGetter are resolved and trimmed source addresses
69 // (with a scheme always present, and without any "subdir" component),
70 // and the values are the paths where each source was previously installed.
71 type reusingGetter map[string]string
73 // getWithGoGetter retrieves the package referenced in the given address
74 // into the installation path and then returns the full path to any subdir
75 // indicated in the address.
77 // The errors returned by this function are those surfaced by the underlying
78 // go-getter library, which have very inconsistent quality as
79 // end-user-actionable error messages. At this time we do not have any
80 // reasonable way to improve these error messages at this layer because
81 // the underlying errors are not separatelyr recognizable.
82 func (g reusingGetter) getWithGoGetter(instPath, addr string) (string, error) {
83 packageAddr, subDir := splitAddrSubdir(addr)
85 log.Printf("[DEBUG] will download %q to %s", packageAddr, instPath)
87 realAddr, err := getter.Detect(packageAddr, instPath, getter.Detectors)
93 realAddr, realSubDir = splitAddrSubdir(realAddr)
95 subDir = filepath.Join(realSubDir, subDir)
98 if realAddr != packageAddr {
99 log.Printf("[TRACE] go-getter detectors rewrote %q to %q", packageAddr, realAddr)
102 if prevDir, exists := g[realAddr]; exists {
103 log.Printf("[TRACE] copying previous install %s to %s", prevDir, instPath)
104 err := os.Mkdir(instPath, os.ModePerm)
106 return "", fmt.Errorf("failed to create directory %s: %s", instPath, err)
108 err = copyDir(instPath, prevDir)
110 return "", fmt.Errorf("failed to copy from %s to %s: %s", prevDir, instPath, err)
113 log.Printf("[TRACE] fetching %q to %q", realAddr, instPath)
114 client := getter.Client{
119 Mode: getter.ClientModeDir,
121 Detectors: goGetterNoDetectors, // we already did detection above
122 Decompressors: goGetterDecompressors,
123 Getters: goGetterGetters,
129 // Remember where we installed this so we might reuse this directory
130 // on subsequent calls to avoid re-downloading.
131 g[realAddr] = instPath
134 // Our subDir string can contain wildcards until this point, so that
135 // e.g. a subDir of * can expand to one top-level directory in a .tar.gz
136 // archive. Now that we've expanded the archive successfully we must
137 // resolve that into a concrete path.
140 finalDir, err = getter.SubdirGlob(instPath, subDir)
141 log.Printf("[TRACE] expanded %q to %q", subDir, finalDir)
149 // If we got this far then we have apparently succeeded in downloading
150 // the requested object!
151 return filepath.Clean(finalDir), nil