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/v2" 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(req *Request, getter Getter) (bool, error)
- func LimitedDecompressors(filesLimit int, fileSizeLimit int64) map[string]Decompressor
- func NewContextWithClient(ctx context.Context, client *Client) context.Context
- func SourceDirSubdir(src string) (string, string)
- func SubdirGlob(dst, subDir string) (string, error)
- func TestDecompressor(t testing.T, d Decompressor, cases []TestDecompressCase)
- type BitBucketDetector
- type Bzip2Decompressor
- type ChecksumError
- type Client
- type Decompressor
- type Detector
- type FileChecksum
- type FileDetector
- type FileGetter
- type FolderStorage
- type GetResult
- type Getter
- type GitDetector
- type GitGetter
- type GitHubDetector
- type GitLabDetector
- type GzipDecompressor
- type HgGetter
- type HttpGetter
- type MockGetter
- type Mode
- type ProgressTracker
- type Request
- type SmbClientGetter
- type SmbMountGetter
- type Storage
- type TarBzip2Decompressor
- type TarDecompressor
- type TarGzipDecompressor
- type TarXzDecompressor
- type TarZstdDecompressor
- type TestDecompressCase
- type XzDecompressor
- type ZipDecompressor
- type ZstdDecompressor
Constants ¶
This section is empty.
Variables ¶
var Decompressors = LimitedDecompressors(noFilesLimit, noFileSizeLimit)
Decompressors is the mapping of extension to the Decompressor implementation configured with default settings that will decompress that extension/type.
Note: these decompressors by default do not limit the number of files or the maximum file size created by the decompressed payload.
var DefaultClient = &Client{ Getters: Getters, Decompressors: Decompressors, }
var ErrSymlinkCopy = errors.New("copying of symlinks has been disabled")
ErrSymlinkCopy means that a copy of a symlink was encountered on a request with DisableSymlinks enabled.
var Getters []Getter
Getters is the mapping of scheme to the Getter implementation that will be used to get a dependency.
var SymlinkAny = os.Symlink
Functions ¶
func Detect ¶
Detect is a method used to detect if a Getter is a candidate for downloading an artifact by calling the Getter.Detect(*Request) method
func LimitedDecompressors ¶ added in v2.2.1
func LimitedDecompressors(filesLimit int, fileSizeLimit int64) map[string]Decompressor
LimitedDecompressors creates the set of Decompressors, but with each compressor configured with the given filesLimit and/or fileSizeLimit where applicable.
func NewContextWithClient ¶ added in v2.1.0
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.
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 { // FileSizeLimit limits the size of a decompressed file. // // The zero value means no limit. FileSizeLimit int64 }
Bzip2Decompressor is an implementation of Decompressor that can decompress bz2 files.
func (*Bzip2Decompressor) Decompress ¶
type ChecksumError ¶
A ChecksumError is returned when a checksum differs
func (*ChecksumError) Error ¶
func (cerr *ChecksumError) Error() string
type Client ¶
type Client struct { // 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 list of protocols supported by this client. If this // is nil, then the default Getters variable will be used. Getters []Getter // Disable symlinks is used to prevent copying or writing files through symlinks for Get requests. // When set to true any copying or writing through symlinks will result in a ErrSymlinkCopy error. DisableSymlinks bool }
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 ClientFromContext ¶ added in v2.1.0
func (*Client) GetChecksum ¶
GetChecksum extracts the checksum from the `checksum` parameter of the src of the Request ex:
http://hashicorp.com/terraform?checksum=<checksumValue> http://hashicorp.com/terraform?checksum=<checksumType>:<checksumValue> http://hashicorp.com/terraform?checksum=file:<checksum_url>
when the checksum is in a file, GetChecksum will first client.Get it in a temporary directory, parse the content of the file and finally delete it. The content of a checksum file is expected to be BSD style or GNU style. For security reasons GetChecksum does not try to get the current working directory and as a result, relative files will only be found when Request.Pwd is set.
BSD-style checksum:
MD5 (file1) = <checksum> MD5 (file2) = <checksum>
GNU-style:
<checksum> file1 <checksum> *file2
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 ¶
FileChecksum helps verifying the checksum for a file.
func (*FileChecksum) Checksum ¶
func (c *FileChecksum) Checksum(filePath string) error
Checksum computes the Checksum for filePath using the hashing algorithm from c.Hash and compares it to c.Value. If those values differ a ChecksumError will be returned.
func (*FileChecksum) String ¶
func (c *FileChecksum) String() string
String returns the hash type and the hash separated by a colon, for example:
"md5:090992ba9fd140077b0661cb75f7ce13" "sha1:ebfb681885ddf1234c18094a45bbeafd91467911"
type FileDetector ¶
type FileDetector struct{}
FileDetector implements Detector to detect file paths.
type FileGetter ¶
type FileGetter struct{}
FileGetter is a Getter implementation that will download a module from a file scheme.
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 GetResult ¶
type GetResult struct { // Local destination of the gotten object. Dst string }
GetResult is the result of a Client.Get
func Get ¶
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 ¶
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.
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(context.Context, *Request) 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(context.Context, *Request) error // Mode returns the mode based on the given URL. This is used to // allow clients to let the getters decide which mode to use. Mode(context.Context, *url.URL) (Mode, error) // Detect detects whether the Request.Src matches a known pattern to // turn it into a proper URL, and also transforms and update Request.Src // when necessary. // The Getter must validate if the Request.Src is a valid URL // with a valid scheme for the Getter, and also check if the // current Getter is the forced one and return true if that's the case. Detect(*Request) (bool, error) }
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 { Detectors []Detector // Timeout sets a deadline which all git CLI operations should // complete within. Defaults to zero which means no timeout. Timeout time.Duration }
GitGetter is a Getter implementation that will download a module from a git repository.
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 ¶
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 { // FileSizeLimit limits the size of a decompressed file. // // The zero value means no limit. FileSizeLimit int64 }
GzipDecompressor is an implementation of Decompressor that can decompress gzip files.
func (*GzipDecompressor) Decompress ¶
type HgGetter ¶
type HgGetter struct { // Timeout sets a deadline which all hg CLI operations should // complete within. Defaults to zero which means no timeout. Timeout time.Duration }
HgGetter is a Getter implementation that will download a module from a Mercurial repository.
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 // DoNotCheckHeadFirst configures the client to NOT check if the server // supports HEAD requests. DoNotCheckHeadFirst bool // HeadFirstTimeout configures the client to enforce a timeout when // the server supports HEAD requests. // // The zero value means no timeout. HeadFirstTimeout time.Duration // ReadTimeout configures the client to enforce a timeout when // making a request to an HTTP server and reading its response body. // // The zero value means no timeout. ReadTimeout time.Duration // MaxBytes limits the number of bytes that will be ready from an HTTP // response body returned from a server. The zero value means no limit. MaxBytes int64 // XTerraformGetLimit configures how many times the client with follow // the " X-Terraform-Get" header value. // // The zero value means no limit. XTerraformGetLimit int // XTerraformGetDisabled disables the client's usage of the "X-Terraform-Get" // header value. XTerraformGetDisabled bool }
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. This source must use one of the configured protocols and getters for the client, or "http"/"https" if using the HttpGetter directly.
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) GetFile ¶
func (g *HttpGetter) GetFile(ctx context.Context, req *Request) 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 }
MockGetter is an implementation of Getter that can be used for tests.
type Mode ¶
type Mode uint
Mode is the mode that the client operates in.
const ( ModeInvalid Mode = iota // ModeAny 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. ModeAny // ModeFile 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. ModeFile // ModeDir 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). ModeDir )
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 Request ¶
type Request struct { // 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 // Forced is the forced getter detected in the Src string during the // Getter detection. Forcing a getter means that go-getter will try // to download the artifact only with the Getter that is being forced. // // For example: // // Request.Src Forced // git::ssh://git@git.example.com:2222/foo/bar.git git // // This field is used by the Getters to validate when they are forced to download // the artifact. // If both Request.Src and Forced contains a forced getter, the one in the Request.Src will // be considered and will override the value of this field. Forced string // Umask is used to mask file permissions when storing local files or // decompressing an archive Umask os.FileMode // GetMode is the method of download the client will use. See Mode for // documentation. GetMode Mode // Copy, in local file mode 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 // Inplace, in local file mode if set to true, do nothing and the returned // operation will simply contain the source file path. Inplace has precedence // over Copy. Inplace bool // ProgressListener allows to track file downloads. // By default a no op progress listener is used. ProgressListener ProgressTracker // Disable symlinks is used to prevent copying or writing files through symlinks. // When set to true any copying or writing through symlinks will result in a ErrSymlinkCopy error. DisableSymlinks bool // contains filtered or unexported fields }
func (*Request) CopyReader ¶
type SmbClientGetter ¶
type SmbClientGetter struct { // Timeout in seconds sets a deadline which all smb client CLI operations should // complete within. Defaults to zero which means to use the default client timeout of 20 seconds. Timeout int }
SmbClientGetter is a Getter implementation that will download a module from a shared folder using smbclient cli.
func (*SmbClientGetter) Get ¶
func (g *SmbClientGetter) Get(ctx context.Context, req *Request) error
type SmbMountGetter ¶
type SmbMountGetter struct{}
SmbMountGetter is a Getter implementation that will download an artifact from a shared folder using the file system using FileGetter implementation. For Unix and MacOS users, the Getter will look for usual system specific mount paths such as: /Volumes/ for MacOS /run/user/1000/gvfs/smb-share:server=<hostIP>,share=<path> for Unix
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(context.Context, 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 { // FileSizeLimit limits the total size of all // decompressed files. // // The zero value means no limit. FileSizeLimit int64 // FilesLimit limits the number of files that are // allowed to be decompressed. // // The zero value means no limit. FilesLimit int }
TarBzip2Decompressor is an implementation of Decompressor that can decompress tar.bz2 files.
func (*TarBzip2Decompressor) Decompress ¶
type TarDecompressor ¶ added in v2.0.1
type TarDecompressor struct { // FileSizeLimit limits the total size of all // decompressed files. // // The zero value means no limit. FileSizeLimit int64 // FilesLimit limits the number of files that are // allowed to be decompressed. // // The zero value means no limit. FilesLimit int }
TarDecompressor is an implementation of Decompressor that can unpack tar files.
func (*TarDecompressor) Decompress ¶ added in v2.0.1
type TarGzipDecompressor ¶
type TarGzipDecompressor struct { // FileSizeLimit limits the total size of all // decompressed files. // // The zero value means no limit. FileSizeLimit int64 // FilesLimit limits the number of files that are // allowed to be decompressed. // // The zero value means no limit. FilesLimit int }
TarGzipDecompressor is an implementation of Decompressor that can decompress tar.gzip files.
func (*TarGzipDecompressor) Decompress ¶
type TarXzDecompressor ¶
type TarXzDecompressor struct { // FileSizeLimit limits the total size of all // decompressed files. // // The zero value means no limit. FileSizeLimit int64 // FilesLimit limits the number of files that are // allowed to be decompressed. // // The zero value means no limit. FilesLimit int }
TarXzDecompressor is an implementation of Decompressor that can decompress tar.xz files.
func (*TarXzDecompressor) Decompress ¶
type TarZstdDecompressor ¶ added in v2.2.0
type TarZstdDecompressor struct { // FileSizeLimit limits the total size of all // decompressed files. // // The zero value means no limit. FileSizeLimit int64 // FilesLimit limits the number of files that are // allowed to be decompressed. // // The zero value means no limit. FilesLimit int }
TarZstdDecompressor is an implementation of Decompressor that can decompress tar.zstd files.
func (*TarZstdDecompressor) Decompress ¶ added in v2.2.0
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 { // FileSizeLimit limits the size of a decompressed file. // // The zero value means no limit. FileSizeLimit int64 }
XzDecompressor is an implementation of Decompressor that can decompress xz files.
func (*XzDecompressor) Decompress ¶
type ZipDecompressor ¶
type ZipDecompressor struct { // FileSizeLimit limits the total size of all // decompressed files. // // The zero value means no limit. FileSizeLimit int64 // FilesLimit limits the number of files that are // allowed to be decompressed. // // The zero value means no limit. FilesLimit int }
ZipDecompressor is an implementation of Decompressor that can decompress zip files.
func (*ZipDecompressor) Decompress ¶
type ZstdDecompressor ¶ added in v2.2.0
type ZstdDecompressor struct { // FileSizeLimit limits the size of a decompressed file. // // The zero value means no limit. FileSizeLimit int64 }
ZstdDecompressor is an implementation of Decompressor that can decompress .zst files.
func (*ZstdDecompressor) Decompress ¶ added in v2.2.0
Source Files
¶
- checksum.go
- client.go
- client_option.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_git.go
- detect_github.go
- detect_gitlab.go
- detect_ssh.go
- folder_storage.go
- get.go
- get_file.go
- get_file_copy.go
- get_file_symlink.go
- get_git.go
- get_hg.go
- get_http.go
- get_mock.go
- get_smb_mount.go
- get_smbclient.go
- mode.go
- netrc.go
- request.go
- source.go
- storage.go