1 // getter is a package for downloading files or directories from a variety of
4 // getter is unique in its ability to download both directories and files.
5 // It also detects certain source strings to be protocol-specific URLs. For
6 // example, "github.com/hashicorp/go-getter" would turn into a Git URL and
7 // use the Git protocol.
9 // Protocols and detectors are extensible.
11 // To get started, see Client.
23 // Getter defines the interface that schemes must implement to download
25 type Getter interface {
26 // Get downloads the given URL into the given directory. This always
27 // assumes that we're updating and gets the latest version that it can.
29 // The directory may already exist (if we're updating). If it is in a
30 // format that isn't understood, an error should be returned. Get shouldn't
31 // simply nuke the directory.
32 Get(string, *url.URL) error
34 // GetFile downloads the give URL into the given path. The URL must
35 // reference a single file. If possible, the Getter should check if
36 // the remote end contains the same file and no-op this operation.
37 GetFile(string, *url.URL) error
39 // ClientMode returns the mode based on the given URL. This is used to
40 // allow clients to let the getters decide which mode to use.
41 ClientMode(*url.URL) (ClientMode, error)
44 // Getters is the mapping of scheme to the Getter implementation that will
45 // be used to get a dependency.
46 var Getters map[string]Getter
48 // forcedRegexp is the regular expression that finds forced getters. This
49 // syntax is schema::url, example: git::https://foo.com
50 var forcedRegexp = regexp.MustCompile(`^([A-Za-z0-9]+)::(.+)$`)
53 httpGetter := &HttpGetter{Netrc: true}
55 Getters = map[string]Getter{
56 "file": new(FileGetter),
57 "git": new(GitGetter),
65 // Get downloads the directory specified by src into the folder specified by
66 // dst. If dst already exists, Get will attempt to update it.
68 // src is a URL, whereas dst is always just a file path to a folder. This
69 // folder doesn't need to exist. It will be created if it doesn't exist.
70 func Get(dst, src string) error {
79 // GetAny downloads a URL into the given destination. Unlike Get or
80 // GetFile, both directories and files are supported.
82 // dst must be a directory. If src is a file, it will be downloaded
83 // into dst with the basename of the URL. If src is a directory or
84 // archive, it will be unpacked directly into dst.
85 func GetAny(dst, src string) error {
94 // GetFile downloads the file specified by src into the path specified by
96 func GetFile(dst, src string) error {
105 // getRunCommand is a helper that will run a command and capture the output
106 // in the case an error happens.
107 func getRunCommand(cmd *exec.Cmd) error {
115 if exiterr, ok := err.(*exec.ExitError); ok {
116 // The program has exited with an exit code != 0
117 if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
119 "%s exited with %d: %s",
126 return fmt.Errorf("error running %s: %s", cmd.Path, buf.String())
129 // getForcedGetter takes a source and returns the tuple of the forced
130 // getter and the raw URL (without the force syntax).
131 func getForcedGetter(src string) (string, string) {
133 if ms := forcedRegexp.FindStringSubmatch(src); ms != nil {