Skip to content

Commit

Permalink
allow to configure a client using var arg options
Browse files Browse the repository at this point in the history
  • Loading branch information
azr committed Dec 14, 2018
1 parent be39683 commit 17c7d12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
15 changes: 15 additions & 0 deletions client_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package getter

// A ClientOption allows to configure a client
type ClientOption func(*Client) error

// Configure configures a client with options.
func (c *Client) Configure(opts ...ClientOption) error {
for _, opt := range opts {
err := opt(c)
if err != nil {
return err
}
}
return nil
}
30 changes: 21 additions & 9 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ func init() {
//
// 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 Get(dst, src string) error {
return (&Client{
func Get(dst, src string, opts ...ClientOption) error {
c := &Client{
Src: src,
Dst: dst,
Dir: true,
Getters: Getters,
}).Get()
}
if err := c.Configure(opts...); err != nil {
return err
}
return c.Get()
}

// GetAny downloads a URL into the given destination. Unlike Get or
Expand All @@ -89,24 +93,32 @@ func Get(dst, src string) error {
// 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{
func GetAny(dst, src string, opts ...ClientOption) error {
c := &Client{
Src: src,
Dst: dst,
Mode: ClientModeAny,
Getters: Getters,
}).Get()
}
if err := c.Configure(opts...); err != nil {
return err
}
return c.Get()
}

// GetFile downloads the file specified by src into the path specified by
// dst.
func GetFile(dst, src string) error {
return (&Client{
func GetFile(dst, src string, opts ...ClientOption) error {
c := &Client{
Src: src,
Dst: dst,
Dir: false,
Getters: Getters,
}).Get()
}
if err := c.Configure(opts...); err != nil {
return err
}
return c.Get()
}

// getRunCommand is a helper that will run a command and capture the output
Expand Down

0 comments on commit 17c7d12

Please sign in to comment.