]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/terraform/configs/configload/getter.go
update vendor and go.mod
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / configs / configload / getter.go
1 package configload
2
3 import (
4 "fmt"
5 "log"
6 "os"
7 "path/filepath"
8
9 cleanhttp "github.com/hashicorp/go-cleanhttp"
10 getter "github.com/hashicorp/go-getter"
11 )
12
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
18 // executable.
19
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),
26 }
27
28 var goGetterNoDetectors = []getter.Detector{}
29
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),
35
36 "tar.bz2": new(getter.TarBzip2Decompressor),
37 "tar.tbz2": new(getter.TarBzip2Decompressor),
38
39 "tar.gz": new(getter.TarGzipDecompressor),
40 "tgz": new(getter.TarGzipDecompressor),
41
42 "tar.xz": new(getter.TarXzDecompressor),
43 "txz": new(getter.TarXzDecompressor),
44 }
45
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,
54 }
55
56 var getterHTTPClient = cleanhttp.DefaultClient()
57
58 var getterHTTPGetter = &getter.HttpGetter{
59 Client: getterHTTPClient,
60 Netrc: true,
61 }
62
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.
67 //
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
72
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.
76 //
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)
84
85 log.Printf("[DEBUG] will download %q to %s", packageAddr, instPath)
86
87 realAddr, err := getter.Detect(packageAddr, instPath, getter.Detectors)
88 if err != nil {
89 return "", err
90 }
91
92 var realSubDir string
93 realAddr, realSubDir = splitAddrSubdir(realAddr)
94 if realSubDir != "" {
95 subDir = filepath.Join(realSubDir, subDir)
96 }
97
98 if realAddr != packageAddr {
99 log.Printf("[TRACE] go-getter detectors rewrote %q to %q", packageAddr, realAddr)
100 }
101
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)
105 if err != nil {
106 return "", fmt.Errorf("failed to create directory %s: %s", instPath, err)
107 }
108 err = copyDir(instPath, prevDir)
109 if err != nil {
110 return "", fmt.Errorf("failed to copy from %s to %s: %s", prevDir, instPath, err)
111 }
112 } else {
113 log.Printf("[TRACE] fetching %q to %q", realAddr, instPath)
114 client := getter.Client{
115 Src: realAddr,
116 Dst: instPath,
117 Pwd: instPath,
118
119 Mode: getter.ClientModeDir,
120
121 Detectors: goGetterNoDetectors, // we already did detection above
122 Decompressors: goGetterDecompressors,
123 Getters: goGetterGetters,
124 }
125 err = client.Get()
126 if err != nil {
127 return "", err
128 }
129 // Remember where we installed this so we might reuse this directory
130 // on subsequent calls to avoid re-downloading.
131 g[realAddr] = instPath
132 }
133
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.
138 var finalDir string
139 if subDir != "" {
140 finalDir, err = getter.SubdirGlob(instPath, subDir)
141 log.Printf("[TRACE] expanded %q to %q", subDir, finalDir)
142 if err != nil {
143 return "", err
144 }
145 } else {
146 finalDir = instPath
147 }
148
149 // If we got this far then we have apparently succeeded in downloading
150 // the requested object!
151 return filepath.Clean(finalDir), nil
152 }