open-nomad/vendor/github.com/hashicorp/go-getter/client.go

299 lines
7.5 KiB
Go
Raw Normal View History

2016-02-12 18:02:16 +00:00
package getter
import (
2019-07-25 22:54:23 +00:00
"context"
2016-02-12 18:02:16 +00:00
"fmt"
"io/ioutil"
"os"
"path/filepath"
2016-02-16 01:38:08 +00:00
"strconv"
2016-02-12 18:02:16 +00:00
"strings"
urlhelper "github.com/hashicorp/go-getter/helper/url"
2019-07-25 22:54:23 +00:00
safetemp "github.com/hashicorp/go-safetemp"
2016-02-12 18:02:16 +00:00
)
// Client is a client for downloading things.
//
// Top-level functions such as Get are shortcuts for interacting with a client.
// Using a client directly allows more fine-grained control over how downloading
// is done, as well as customizing the protocols supported.
type Client struct {
2019-07-25 22:54:23 +00:00
// Ctx for cancellation
Ctx context.Context
2016-02-12 18:02:16 +00:00
// Src is the source URL to get.
//
// Dst is the path to save the downloaded thing as. If Dir is set to
// true, then this should be a directory. If the directory doesn't exist,
// it will be created for you.
//
// Pwd is the working directory for detection. If this isn't set, some
// detection may fail. Client will not default pwd to the current
// working directory for security reasons.
Src string
Dst string
Pwd string
2016-03-15 00:15:27 +00:00
// Mode is the method of download the client will use. See ClientMode
// for documentation.
Mode ClientMode
2016-02-12 18:02:16 +00:00
// Detectors is the list of detectors that are tried on the source.
// If this is nil, then the default Detectors will be used.
Detectors []Detector
2016-02-16 01:38:08 +00:00
// Decompressors is the map of decompressors supported by this client.
// If this is nil, then the default value is the Decompressors global.
Decompressors map[string]Decompressor
2016-02-12 18:02:16 +00:00
// Getters is the map of protocols supported by this client. If this
// is nil, then the default Getters variable will be used.
Getters map[string]Getter
2016-03-15 00:15:27 +00:00
// Dir, if true, tells the Client it is downloading a directory (versus
// a single file). This distinction is necessary since filenames and
// directory names follow the same format so disambiguating is impossible
// without knowing ahead of time.
//
// WARNING: deprecated. If Mode is set, that will take precedence.
Dir bool
2019-07-25 22:54:23 +00:00
// ProgressListener allows to track file downloads.
// By default a no op progress listener is used.
ProgressListener ProgressTracker
Options []ClientOption
2016-02-12 18:02:16 +00:00
}
// Get downloads the configured source to the destination.
func (c *Client) Get() error {
2019-07-25 22:54:23 +00:00
if err := c.Configure(c.Options...); err != nil {
return err
}
2016-02-16 01:38:08 +00:00
// Store this locally since there are cases we swap this
2016-03-15 00:15:27 +00:00
mode := c.Mode
if mode == ClientModeInvalid {
if c.Dir {
mode = ClientModeDir
} else {
mode = ClientModeFile
}
}
2016-02-16 01:38:08 +00:00
2019-07-25 22:54:23 +00:00
src, err := Detect(c.Src, c.Pwd, c.Detectors)
2016-02-12 18:02:16 +00:00
if err != nil {
return err
}
// Determine if we have a forced protocol, i.e. "git::http://..."
force, src := getForcedGetter(src)
// If there is a subdir component, then we download the root separately
// and then copy over the proper subdir.
var realDst string
dst := c.Dst
src, subDir := SourceDirSubdir(src)
if subDir != "" {
2018-04-09 20:34:38 +00:00
td, tdcloser, err := safetemp.Dir("", "getter")
2016-02-12 18:02:16 +00:00
if err != nil {
return err
}
2018-04-09 20:34:38 +00:00
defer tdcloser.Close()
2016-02-12 18:02:16 +00:00
realDst = dst
2018-04-09 20:34:38 +00:00
dst = td
2016-02-12 18:02:16 +00:00
}
u, err := urlhelper.Parse(src)
if err != nil {
return err
}
if force == "" {
force = u.Scheme
}
2019-07-25 22:54:23 +00:00
g, ok := c.Getters[force]
2016-02-12 18:02:16 +00:00
if !ok {
return fmt.Errorf(
"download not supported for scheme '%s'", force)
}
2016-02-16 01:38:08 +00:00
// We have magic query parameters that we use to signal different features
q := u.Query()
// Determine if we have an archive type
archiveV := q.Get("archive")
if archiveV != "" {
// Delete the paramter since it is a magic parameter we don't
// want to pass on to the Getter
q.Del("archive")
u.RawQuery = q.Encode()
// If we can parse the value as a bool and it is false, then
// set the archive to "-" which should never map to a decompressor
if b, err := strconv.ParseBool(archiveV); err == nil && !b {
archiveV = "-"
}
}
if archiveV == "" {
// We don't appear to... but is it part of the filename?
matchingLen := 0
2019-07-25 22:54:23 +00:00
for k := range c.Decompressors {
if strings.HasSuffix(u.Path, "."+k) && len(k) > matchingLen {
2016-02-16 01:38:08 +00:00
archiveV = k
matchingLen = len(k)
}
}
}
// If we have a decompressor, then we need to change the destination
// to download to a temporary path. We unarchive this into the final,
// real path.
var decompressDst string
var decompressDir bool
2019-07-25 22:54:23 +00:00
decompressor := c.Decompressors[archiveV]
2016-02-16 01:38:08 +00:00
if decompressor != nil {
// Create a temporary directory to store our archive. We delete
// this at the end of everything.
td, err := ioutil.TempDir("", "getter")
if err != nil {
return fmt.Errorf(
"Error creating temporary directory for archive: %s", err)
}
defer os.RemoveAll(td)
// Swap the download directory to be our temporary path and
// store the old values.
decompressDst = dst
2016-03-15 00:15:27 +00:00
decompressDir = mode != ClientModeFile
2016-02-16 01:38:08 +00:00
dst = filepath.Join(td, "archive")
2016-03-15 00:15:27 +00:00
mode = ClientModeFile
2016-02-16 01:38:08 +00:00
}
2019-07-25 22:54:23 +00:00
// Determine checksum if we have one
checksum, err := c.extractChecksum(u)
if err != nil {
return fmt.Errorf("invalid checksum: %s", err)
2016-02-12 18:02:16 +00:00
}
2019-07-25 22:54:23 +00:00
// Delete the query parameter if we have it.
q.Del("checksum")
u.RawQuery = q.Encode()
2016-03-15 00:15:27 +00:00
if mode == ClientModeAny {
// Ask the getter which client mode to use
mode, err = g.ClientMode(u)
if err != nil {
return err
}
2016-03-15 00:15:27 +00:00
// Destination is the base name of the URL path in "any" mode when
// a file source is detected.
if mode == ClientModeFile {
2017-12-06 19:16:17 +00:00
filename := filepath.Base(u.Path)
// Determine if we have a custom file name
if v := q.Get("filename"); v != "" {
// Delete the query parameter if we have it.
q.Del("filename")
u.RawQuery = q.Encode()
filename = v
}
dst = filepath.Join(dst, filename)
}
2016-03-15 00:15:27 +00:00
}
2016-02-12 18:02:16 +00:00
// If we're not downloading a directory, then just download the file
// and return.
2016-03-15 00:15:27 +00:00
if mode == ClientModeFile {
2019-07-25 22:54:23 +00:00
getFile := true
if checksum != nil {
if err := checksum.checksum(dst); err == nil {
// don't get the file if the checksum of dst is correct
getFile = false
}
2016-02-12 18:02:16 +00:00
}
2019-07-25 22:54:23 +00:00
if getFile {
err := g.GetFile(dst, u)
if err != nil {
2016-02-16 01:38:08 +00:00
return err
}
2019-07-25 22:54:23 +00:00
if checksum != nil {
if err := checksum.checksum(dst); err != nil {
return err
}
}
2016-02-16 01:38:08 +00:00
}
if decompressor != nil {
// We have a decompressor, so decompress the current destination
// into the final destination with the proper mode.
err := decompressor.Decompress(decompressDst, dst, decompressDir)
if err != nil {
return err
}
// Swap the information back
dst = decompressDst
2016-03-15 00:15:27 +00:00
if decompressDir {
mode = ClientModeAny
} else {
mode = ClientModeFile
}
2016-02-12 18:02:16 +00:00
}
2016-02-16 01:38:08 +00:00
// We check the dir value again because it can be switched back
// if we were unarchiving. If we're still only Get-ing a file, then
// we're done.
2016-03-15 00:15:27 +00:00
if mode == ClientModeFile {
2016-02-16 01:38:08 +00:00
return nil
}
2016-02-12 18:02:16 +00:00
}
2016-02-16 01:38:08 +00:00
// If we're at this point we're either downloading a directory or we've
// downloaded and unarchived a directory and we're just checking subdir.
// In the case we have a decompressor we don't Get because it was Get
// above.
if decompressor == nil {
2016-03-15 00:15:27 +00:00
// If we're getting a directory, then this is an error. You cannot
// checksum a directory. TODO: test
2019-07-25 22:54:23 +00:00
if checksum != nil {
2016-03-15 00:15:27 +00:00
return fmt.Errorf(
"checksum cannot be specified for directory download")
}
2016-02-16 01:38:08 +00:00
// We're downloading a directory, which might require a bit more work
// if we're specifying a subdir.
err := g.Get(dst, u)
if err != nil {
err = fmt.Errorf("error downloading '%s': %s", src, err)
return err
}
2016-02-12 18:02:16 +00:00
}
// If we have a subdir, copy that over
if subDir != "" {
if err := os.RemoveAll(realDst); err != nil {
return err
}
if err := os.MkdirAll(realDst, 0755); err != nil {
return err
}
// Process any globs
subDir, err := SubdirGlob(dst, subDir)
if err != nil {
return err
}
2019-07-25 22:54:23 +00:00
return copyDir(c.Ctx, realDst, subDir, false)
2016-02-12 18:02:16 +00:00
}
return nil
}