Documentation ¶
Overview ¶
getter is a package for downloading files or directories from a variety of protocols.
getter is unique in its ability to download both directories and files. It also detects certain source strings to be protocol-specific URLs. For example, "github.com/hashicorp/go-getter" would turn into a Git URL and use the Git protocol.
Protocols and detectors are extensible.
To get started, see Client.
Index ¶
- Variables
- func Copy(ctx context.Context, dst io.Writer, src io.Reader) (int64, error)
- func Detect(src string, pwd string, ds []Detector) (string, error)
- func Get(dst, src string, opts ...ClientOption) error
- func GetAny(dst, src string, opts ...ClientOption) error
- func GetFile(dst, src string, opts ...ClientOption) error
- func SourceDirSubdir(src string) (string, string)
- func SubdirGlob(dst, subDir string) (string, error)
- func TestDecompressor(t testing.T, d Decompressor, cases []TestDecompressCase)
- func WithContext(ctx context.Context) func(*Client) error
- func WithInsecure() func(*Client) error
- func WithProgress(pl ProgressTracker) func(*Client) error
- type BitBucketDetector
- type Bzip2Decompressor
- type ChecksumError
- type Client
- type ClientMode
- type ClientOption
- type Decompressor
- type Detector
- type FileChecksum
- type FileDetector
- type FileGetter
- type FolderStorage
- type GCSDetector
- type GCSGetter
- type Getter
- type GitDetector
- type GitGetter
- type GitHubDetector
- type GitLabDetector
- type GzipDecompressor
- type HgGetter
- type HttpGetter
- type MockGetter
- type ProgressTracker
- type S3Detector
- type S3Getter
- type Storage
- type TarBzip2Decompressor
- type TarGzipDecompressor
- type TarXzDecompressor
- type TarZstdDecompressor
- type TestDecompressCase
- type XzDecompressor
- type ZipDecompressor
- type ZstdDecompressor
Constants ¶
This section is empty.
Variables ¶
var Decompressors map[string]Decompressor
Decompressors is the mapping of extension to the Decompressor implementation that will decompress that extension/type.
var Detectors []Detector
Detectors is the list of detectors that are tried on an invalid URL. This is also the order they're tried (index 0 is first).
var Getters map[string]Getter
Getters is the mapping of scheme to the Getter implementation that will be used to get a dependency.
Functions ¶
func Detect ¶
Detect turns a source string into another source string if it is detected to be of a known pattern.
The third parameter should be the list of detectors to use in the order to try them. If you don't want to configure this, just use the global Detectors variable.
This is safe to be called with an already valid source string: Detect will just return it.
func Get ¶
func Get(dst, src string, opts ...ClientOption) error
Get downloads the directory specified by src into the folder specified by dst. If dst already exists, Get will attempt to update it.
src is a URL, whereas dst is always just a file path to a folder. This folder doesn't need to exist. It will be created if it doesn't exist.
func GetAny ¶
func GetAny(dst, src string, opts ...ClientOption) error
GetAny downloads a URL into the given destination. Unlike Get or GetFile, both directories and files are supported.
dst must be a directory. If src is a file, it will be downloaded into dst with the basename of the URL. If src is a directory or archive, it will be unpacked directly into dst.
func GetFile ¶
func GetFile(dst, src string, opts ...ClientOption) error
GetFile downloads the file specified by src into the path specified by dst.
func SourceDirSubdir ¶
SourceDirSubdir takes a source URL and returns a tuple of the URL without the subdir and the subdir.
ex:
dom.com/path/?q=p => dom.com/path/?q=p, "" proto://dom.com/path//*?q=p => proto://dom.com/path?q=p, "*" proto://dom.com/path//path2?q=p => proto://dom.com/path?q=p, "path2"
func SubdirGlob ¶
SubdirGlob returns the actual subdir with globbing processed.
dst should be a destination directory that is already populated (the download is complete) and subDir should be the set subDir. If subDir is an empty string, this returns an empty string.
The returned path is the full absolute path.
func TestDecompressor ¶
func TestDecompressor(t testing.T, d Decompressor, cases []TestDecompressCase)
TestDecompressor is a helper function for testing generic decompressors.
func WithContext ¶
WithContext allows to pass a context to operation in order to be able to cancel a download in progress.
func WithInsecure ¶ added in v1.5.4
WithInsecure allows for a user to avoid checking certificates (not recommended). For example, when connecting on HTTPS where an invalid certificate is presented. User assumes all risk. Not all getters have support for insecure mode yet.
func WithProgress ¶
func WithProgress(pl ProgressTracker) func(*Client) error
WithProgress allows for a user to track the progress of a download. For example by displaying a progress bar with current download. Not all getters have progress support yet.
Types ¶
type BitBucketDetector ¶
type BitBucketDetector struct{}
BitBucketDetector implements Detector to detect BitBucket URLs and turn them into URLs that the Git or Hg Getter can understand.
type Bzip2Decompressor ¶
type Bzip2Decompressor struct{}
Bzip2Decompressor is an implementation of Decompressor that can decompress bz2 files.
func (*Bzip2Decompressor) Decompress ¶
type ChecksumError ¶ added in v1.2.0
A ChecksumError is returned when a checksum differs
func (*ChecksumError) Error ¶ added in v1.2.0
func (cerr *ChecksumError) Error() string
type Client ¶
type Client struct { // Ctx for cancellation Ctx context.Context // 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 // Mode is the method of download the client will use. See ClientMode // for documentation. Mode ClientMode // Umask is used to mask file permissions when storing local files or decompressing // an archive Umask os.FileMode // 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 // 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 // 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 // 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 // ProgressListener allows to track file downloads. // By default a no op progress listener is used. ProgressListener ProgressTracker // Insecure controls whether a client verifies the server's // certificate chain and host name. If Insecure is true, crypto/tls // accepts any certificate presented by the server and any host name in that // certificate. In this mode, TLS is susceptible to machine-in-the-middle // attacks unless custom verification is used. This should be used only for // testing or in combination with VerifyConnection or VerifyPeerCertificate. // This is identical to tls.Config.InsecureSkipVerify. Insecure bool Options []ClientOption }
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.
func (*Client) ChecksumFromFile ¶ added in v1.4.0
ChecksumFromFile will return all the FileChecksums found in file
ChecksumFromFile will try to guess the hashing algorithm based on content of checksum file
ChecksumFromFile will only return checksums for files that match file behind src
func (*Client) Configure ¶
func (c *Client) Configure(opts ...ClientOption) error
Configure configures a client with options.
type ClientMode ¶
type ClientMode uint
ClientMode is the mode that the client operates in.
const ( ClientModeInvalid ClientMode = iota // ClientModeAny downloads anything it can. In this mode, dst must // be a directory. If src is a file, it is saved into the directory // with the basename of the URL. If src is a directory or archive, // it is unpacked directly into dst. ClientModeAny // ClientModeFile downloads a single file. In this mode, dst must // be a file path (doesn't have to exist). src must point to a single // file. It is saved as dst. ClientModeFile // ClientModeDir downloads a directory. In this mode, dst must be // a directory path (doesn't have to exist). src must point to an // archive or directory (such as in s3). ClientModeDir )
type ClientOption ¶
A ClientOption allows to configure a client
type Decompressor ¶
type Decompressor interface { // Decompress should decompress src to dst. dir specifies whether dst // is a directory or single file. src is guaranteed to be a single file // that exists. dst is not guaranteed to exist already. Decompress(dst, src string, dir bool, umask os.FileMode) error }
Decompressor defines the interface that must be implemented to add support for decompressing a type.
Important: if you're implementing a decompressor, please use the containsDotDot helper in this file to ensure that files can't be decompressed outside of the specified directory.
type Detector ¶
type Detector interface { // Detect will detect whether the string matches a known pattern to // turn it into a proper URL. Detect(string, string) (string, bool, error) }
Detector defines the interface that an invalid URL or a URL with a blank scheme is passed through in order to determine if its shorthand for something else well-known.
type FileChecksum ¶ added in v1.4.0
FileChecksum helps verifying the checksum for a file.
type FileDetector ¶
type FileDetector struct{}
FileDetector implements Detector to detect file paths.
type FileGetter ¶
type FileGetter struct { // Copy, if set to true, will copy data instead of using a symlink. If // false, attempts to symlink to speed up the operation and to lower the // disk space usage. If the symlink fails, may attempt to copy on windows. Copy bool // contains filtered or unexported fields }
FileGetter is a Getter implementation that will download a module from a file scheme.
func (*FileGetter) ClientMode ¶
func (g *FileGetter) ClientMode(u *url.URL) (ClientMode, error)
type FolderStorage ¶
type FolderStorage struct { // StorageDir is the directory where the modules will be stored. StorageDir string }
FolderStorage is an implementation of the Storage interface that manages modules on the disk.
type GCSDetector ¶ added in v1.2.0
type GCSDetector struct{}
GCSDetector implements Detector to detect GCS URLs and turn them into URLs that the GCSGetter can understand.
type GCSGetter ¶ added in v1.2.0
type GCSGetter struct {
// contains filtered or unexported fields
}
GCSGetter is a Getter implementation that will download a module from a GCS bucket.
func (*GCSGetter) ClientMode ¶ added in v1.2.0
func (g *GCSGetter) ClientMode(u *url.URL) (ClientMode, error)
func (*GCSGetter) Context ¶ added in v1.2.0
Context tries to returns the Contex from the getter's client. otherwise context.Background() is returned.
type Getter ¶
type Getter interface { // Get downloads the given URL into the given directory. This always // assumes that we're updating and gets the latest version that it can. // // The directory may already exist (if we're updating). If it is in a // format that isn't understood, an error should be returned. Get shouldn't // simply nuke the directory. Get(string, *url.URL) error // GetFile downloads the give URL into the given path. The URL must // reference a single file. If possible, the Getter should check if // the remote end contains the same file and no-op this operation. GetFile(string, *url.URL) error // ClientMode returns the mode based on the given URL. This is used to // allow clients to let the getters decide which mode to use. ClientMode(*url.URL) (ClientMode, error) // SetClient allows a getter to know it's client // in order to access client's Get functions or // progress tracking. SetClient(*Client) }
Getter defines the interface that schemes must implement to download things.
type GitDetector ¶
type GitDetector struct{}
GitDetector implements Detector to detect Git SSH URLs such as git@host.com:dir1/dir2 and converts them to proper URLs.
type GitGetter ¶
type GitGetter struct {
// contains filtered or unexported fields
}
GitGetter is a Getter implementation that will download a module from a git repository.
func (*GitGetter) ClientMode ¶
func (g *GitGetter) ClientMode(_ *url.URL) (ClientMode, error)
func (*GitGetter) Context ¶
Context tries to returns the Contex from the getter's client. otherwise context.Background() is returned.
type GitHubDetector ¶
type GitHubDetector struct{}
GitHubDetector implements Detector to detect GitHub URLs and turn them into URLs that the Git Getter can understand.
type GitLabDetector ¶ added in v1.4.2
type GitLabDetector struct{}
GitLabDetector implements Detector to detect GitLab URLs and turn them into URLs that the Git Getter can understand.
type GzipDecompressor ¶
type GzipDecompressor struct{}
GzipDecompressor is an implementation of Decompressor that can decompress gzip files.
func (*GzipDecompressor) Decompress ¶
type HgGetter ¶
type HgGetter struct {
// contains filtered or unexported fields
}
HgGetter is a Getter implementation that will download a module from a Mercurial repository.
func (*HgGetter) ClientMode ¶
func (g *HgGetter) ClientMode(_ *url.URL) (ClientMode, error)
func (*HgGetter) Context ¶
Context tries to returns the Contex from the getter's client. otherwise context.Background() is returned.
type HttpGetter ¶
type HttpGetter struct { // Netrc, if true, will lookup and use auth information found // in the user's netrc file if available. Netrc bool // Client is the http.Client to use for Get requests. // This defaults to a cleanhttp.DefaultClient if left unset. Client *http.Client // Header contains optional request header fields that should be included // with every HTTP request. Note that the zero value of this field is nil, // and as such it needs to be initialized before use, via something like // make(http.Header). Header http.Header // contains filtered or unexported fields }
HttpGetter is a Getter implementation that will download from an HTTP endpoint.
For file downloads, HTTP is used directly.
The protocol for downloading a directory from an HTTP endpoint is as follows:
An HTTP GET request is made to the URL with the additional GET parameter "terraform-get=1". This lets you handle that scenario specially if you wish. The response must be a 2xx.
First, a header is looked for "X-Terraform-Get" which should contain a source URL to download.
If the header is not present, then a meta tag is searched for named "terraform-get" and the content should be a source URL.
The source URL, whether from the header or meta tag, must be a fully formed URL. The shorthand syntax of "github.com/foo/bar" or relative paths are not allowed.
func (*HttpGetter) ClientMode ¶
func (g *HttpGetter) ClientMode(u *url.URL) (ClientMode, error)
func (*HttpGetter) Context ¶
Context tries to returns the Contex from the getter's client. otherwise context.Background() is returned.
func (*HttpGetter) GetFile ¶
func (g *HttpGetter) GetFile(dst string, src *url.URL) error
GetFile fetches the file from src and stores it at dst. If the server supports Accept-Range, HttpGetter will attempt a range request. This means it is the caller's responsibility to ensure that an older version of the destination file does not exist, else it will be either falsely identified as being replaced, or corrupted with extra bytes appended.
type MockGetter ¶
type MockGetter struct { // Proxy, if set, will be called after recording the calls below. // If it isn't set, then the *Err values will be returned. Proxy Getter GetCalled bool GetDst string GetURL *url.URL GetErr error GetFileCalled bool GetFileDst string GetFileURL *url.URL GetFileErr error // contains filtered or unexported fields }
MockGetter is an implementation of Getter that can be used for tests.
func (*MockGetter) ClientMode ¶
func (g *MockGetter) ClientMode(u *url.URL) (ClientMode, error)
type ProgressTracker ¶
type ProgressTracker interface { // TrackProgress should be called when // a new object is being downloaded. // src is the location the file is // downloaded from. // currentSize is the current size of // the file in case it is a partial // download. // totalSize is the total size in bytes, // size can be zero if the file size // is not known. // stream is the file being downloaded, every // written byte will add up to processed size. // // TrackProgress returns a ReadCloser that wraps the // download in progress ( stream ). // When the download is finished, body shall be closed. TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) }
ProgressTracker allows to track the progress of downloads.
type S3Detector ¶
type S3Detector struct{}
S3Detector implements Detector to detect S3 URLs and turn them into URLs that the S3 getter can understand.
type S3Getter ¶
type S3Getter struct {
// contains filtered or unexported fields
}
S3Getter is a Getter implementation that will download a module from a S3 bucket.
func (*S3Getter) ClientMode ¶
func (g *S3Getter) ClientMode(u *url.URL) (ClientMode, error)
type Storage ¶
type Storage interface { // Dir returns the directory on local disk where the directory source // can be loaded from. Dir(string) (string, bool, error) // Get will download and optionally update the given directory. Get(string, string, bool) error }
Storage is an interface that knows how to lookup downloaded directories as well as download and update directories from their sources into the proper location.
type TarBzip2Decompressor ¶
type TarBzip2Decompressor struct{}
TarBzip2Decompressor is an implementation of Decompressor that can decompress tar.bz2 files.
func (*TarBzip2Decompressor) Decompress ¶
type TarGzipDecompressor ¶
type TarGzipDecompressor struct{}
TarGzipDecompressor is an implementation of Decompressor that can decompress tar.gzip files.
func (*TarGzipDecompressor) Decompress ¶
type TarXzDecompressor ¶
type TarXzDecompressor struct{}
TarXzDecompressor is an implementation of Decompressor that can decompress tar.xz files.
func (*TarXzDecompressor) Decompress ¶
type TarZstdDecompressor ¶ added in v1.5.2
type TarZstdDecompressor struct{}
TarZstdDecompressor is an implementation of Decompressor that can decompress tar.zstd files.
func (*TarZstdDecompressor) Decompress ¶ added in v1.5.2
type TestDecompressCase ¶
type TestDecompressCase struct { Input string // Input is the complete path to the input file Dir bool // Dir is whether or not we're testing directory mode Err bool // Err is whether we expect an error or not DirList []string // DirList is the list of files for Dir mode FileMD5 string // FileMD5 is the expected MD5 for a single file Mtime *time.Time // Mtime is the optionally expected mtime for a single file (or all files if in Dir mode) }
TestDecompressCase is a single test case for testing decompressors
type XzDecompressor ¶
type XzDecompressor struct{}
XzDecompressor is an implementation of Decompressor that can decompress xz files.
func (*XzDecompressor) Decompress ¶
type ZipDecompressor ¶
type ZipDecompressor struct{}
ZipDecompressor is an implementation of Decompressor that can decompress zip files.
func (*ZipDecompressor) Decompress ¶
type ZstdDecompressor ¶ added in v1.5.2
type ZstdDecompressor struct{}
ZstdDecompressor is an implementation of Decompressor that can decompress .zst files.
func (*ZstdDecompressor) Decompress ¶ added in v1.5.2
Source Files ¶
- checksum.go
- client.go
- client_mode.go
- client_option.go
- client_option_insecure.go
- client_option_progress.go
- common.go
- copy_dir.go
- decompress.go
- decompress_bzip2.go
- decompress_gzip.go
- decompress_tar.go
- decompress_tbz2.go
- decompress_testing.go
- decompress_tgz.go
- decompress_txz.go
- decompress_tzst.go
- decompress_xz.go
- decompress_zip.go
- decompress_zstd.go
- detect.go
- detect_bitbucket.go
- detect_file.go
- detect_gcs.go
- detect_git.go
- detect_github.go
- detect_gitlab.go
- detect_s3.go
- detect_ssh.go
- folder_storage.go
- get.go
- get_base.go
- get_file.go
- get_file_copy.go
- get_file_unix.go
- get_gcs.go
- get_git.go
- get_hg.go
- get_http.go
- get_mock.go
- get_s3.go
- netrc.go
- source.go
- storage.go