Skip to content

Commit

Permalink
ClientMode, GetAny
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Feb 25, 2016
1 parent 848242c commit 5f8e96a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
16 changes: 11 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ type Client struct {
Dst string
Pwd string

// 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.
Dir bool
// Mode is the method of download the client will use. See ClientMode
// for documentation.
Mode ClientMode

// Detectors is the list of detectors that are tried on the source.
// If this is nil, then the default Detectors will be used.
Expand All @@ -55,6 +53,14 @@ type Client struct {
// 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
}

// Get downloads the configured source to the destination.
Expand Down
24 changes: 24 additions & 0 deletions client_mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package getter

// ClientMode is the mode that the client operates in.
type ClientMode uint

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
)
15 changes: 15 additions & 0 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ func Get(dst, src string) error {
}).Get()
}

// 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 GetAny(dst, src string) error {
return (&Client{
Src: src,
Dst: dst,
Mode: ClientModeAny,
Getters: Getters,
}).Get()
}

// GetFile downloads the file specified by src into the path specified by
// dst.
func GetFile(dst, src string) error {
Expand Down

0 comments on commit 5f8e96a

Please sign in to comment.