12 urlhelper "github.com/hashicorp/go-getter/helper/url"
13 safetemp "github.com/hashicorp/go-safetemp"
16 // Client is a client for downloading things.
18 // Top-level functions such as Get are shortcuts for interacting with a client.
19 // Using a client directly allows more fine-grained control over how downloading
20 // is done, as well as customizing the protocols supported.
22 // Ctx for cancellation
25 // Src is the source URL to get.
27 // Dst is the path to save the downloaded thing as. If Dir is set to
28 // true, then this should be a directory. If the directory doesn't exist,
29 // it will be created for you.
31 // Pwd is the working directory for detection. If this isn't set, some
32 // detection may fail. Client will not default pwd to the current
33 // working directory for security reasons.
38 // Mode is the method of download the client will use. See ClientMode
42 // Detectors is the list of detectors that are tried on the source.
43 // If this is nil, then the default Detectors will be used.
46 // Decompressors is the map of decompressors supported by this client.
47 // If this is nil, then the default value is the Decompressors global.
48 Decompressors map[string]Decompressor
50 // Getters is the map of protocols supported by this client. If this
51 // is nil, then the default Getters variable will be used.
52 Getters map[string]Getter
54 // Dir, if true, tells the Client it is downloading a directory (versus
55 // a single file). This distinction is necessary since filenames and
56 // directory names follow the same format so disambiguating is impossible
57 // without knowing ahead of time.
59 // WARNING: deprecated. If Mode is set, that will take precedence.
62 // ProgressListener allows to track file downloads.
63 // By default a no op progress listener is used.
64 ProgressListener ProgressTracker
66 Options []ClientOption
69 // Get downloads the configured source to the destination.
70 func (c *Client) Get() error {
71 if err := c.Configure(c.Options...); err != nil {
75 // Store this locally since there are cases we swap this
77 if mode == ClientModeInvalid {
85 src, err := Detect(c.Src, c.Pwd, c.Detectors)
90 // Determine if we have a forced protocol, i.e. "git::http://..."
91 force, src := getForcedGetter(src)
93 // If there is a subdir component, then we download the root separately
94 // and then copy over the proper subdir.
97 src, subDir := SourceDirSubdir(src)
99 td, tdcloser, err := safetemp.Dir("", "getter")
103 defer tdcloser.Close()
109 u, err := urlhelper.Parse(src)
117 g, ok := c.Getters[force]
120 "download not supported for scheme '%s'", force)
123 // We have magic query parameters that we use to signal different features
126 // Determine if we have an archive type
127 archiveV := q.Get("archive")
129 // Delete the paramter since it is a magic parameter we don't
130 // want to pass on to the Getter
132 u.RawQuery = q.Encode()
134 // If we can parse the value as a bool and it is false, then
135 // set the archive to "-" which should never map to a decompressor
136 if b, err := strconv.ParseBool(archiveV); err == nil && !b {
141 // We don't appear to... but is it part of the filename?
143 for k := range c.Decompressors {
144 if strings.HasSuffix(u.Path, "."+k) && len(k) > matchingLen {
151 // If we have a decompressor, then we need to change the destination
152 // to download to a temporary path. We unarchive this into the final,
154 var decompressDst string
155 var decompressDir bool
156 decompressor := c.Decompressors[archiveV]
157 if decompressor != nil {
158 // Create a temporary directory to store our archive. We delete
159 // this at the end of everything.
160 td, err := ioutil.TempDir("", "getter")
163 "Error creating temporary directory for archive: %s", err)
165 defer os.RemoveAll(td)
167 // Swap the download directory to be our temporary path and
168 // store the old values.
170 decompressDir = mode != ClientModeFile
171 dst = filepath.Join(td, "archive")
172 mode = ClientModeFile
175 // Determine checksum if we have one
176 checksum, err := c.extractChecksum(u)
178 return fmt.Errorf("invalid checksum: %s", err)
181 // Delete the query parameter if we have it.
183 u.RawQuery = q.Encode()
185 if mode == ClientModeAny {
186 // Ask the getter which client mode to use
187 mode, err = g.ClientMode(u)
192 // Destination is the base name of the URL path in "any" mode when
193 // a file source is detected.
194 if mode == ClientModeFile {
195 filename := filepath.Base(u.Path)
197 // Determine if we have a custom file name
198 if v := q.Get("filename"); v != "" {
199 // Delete the query parameter if we have it.
201 u.RawQuery = q.Encode()
206 dst = filepath.Join(dst, filename)
210 // If we're not downloading a directory, then just download the file
212 if mode == ClientModeFile {
215 if err := checksum.checksum(dst); err == nil {
216 // don't get the file if the checksum of dst is correct
221 err := g.GetFile(dst, u)
227 if err := checksum.checksum(dst); err != nil {
233 if decompressor != nil {
234 // We have a decompressor, so decompress the current destination
235 // into the final destination with the proper mode.
236 err := decompressor.Decompress(decompressDst, dst, decompressDir)
241 // Swap the information back
246 mode = ClientModeFile
250 // We check the dir value again because it can be switched back
251 // if we were unarchiving. If we're still only Get-ing a file, then
253 if mode == ClientModeFile {
258 // If we're at this point we're either downloading a directory or we've
259 // downloaded and unarchived a directory and we're just checking subdir.
260 // In the case we have a decompressor we don't Get because it was Get
262 if decompressor == nil {
263 // If we're getting a directory, then this is an error. You cannot
264 // checksum a directory. TODO: test
267 "checksum cannot be specified for directory download")
270 // We're downloading a directory, which might require a bit more work
271 // if we're specifying a subdir.
274 err = fmt.Errorf("error downloading '%s': %s", src, err)
279 // If we have a subdir, copy that over
281 if err := os.RemoveAll(realDst); err != nil {
284 if err := os.MkdirAll(realDst, 0755); err != nil {
289 subDir, err := SubdirGlob(dst, subDir)
294 return copyDir(c.Ctx, realDst, subDir, false)