|
1 | 1 | package getter |
2 | 2 |
|
3 | | -import "context" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | +) |
4 | 7 |
|
5 | | -// A ClientOption allows to configure a client |
| 8 | +// ClientOption is used to configure a client. |
6 | 9 | type ClientOption func(*Client) error |
7 | 10 |
|
8 | | -// Configure configures a client with options. |
| 11 | +// Configure applies all of the given client options, along with any default |
| 12 | +// behavior including context, decompressors, detectors, and getters used by |
| 13 | +// the client. |
9 | 14 | func (c *Client) Configure(opts ...ClientOption) error { |
| 15 | + // If the context has not been configured use the background context. |
10 | 16 | if c.Ctx == nil { |
11 | 17 | c.Ctx = context.Background() |
12 | 18 | } |
| 19 | + |
| 20 | + // Store the options used to configure this client. |
13 | 21 | c.Options = opts |
| 22 | + |
| 23 | + // Apply all of the client options. |
14 | 24 | for _, opt := range opts { |
15 | 25 | err := opt(c) |
16 | 26 | if err != nil { |
17 | 27 | return err |
18 | 28 | } |
19 | 29 | } |
20 | | - // Default decompressor values |
| 30 | + |
| 31 | + // If the client was not configured with any Decompressors, Detectors, |
| 32 | + // or Getters, use the default values for each. |
21 | 33 | if c.Decompressors == nil { |
22 | 34 | c.Decompressors = Decompressors |
23 | 35 | } |
24 | | - // Default detector values |
25 | 36 | if c.Detectors == nil { |
26 | 37 | c.Detectors = Detectors |
27 | 38 | } |
28 | | - // Default getter values |
29 | 39 | if c.Getters == nil { |
30 | 40 | c.Getters = Getters |
31 | 41 | } |
32 | 42 |
|
| 43 | + // Set the client for each getter, so the top-level client can know |
| 44 | + // the getter-specific client functions or progress tracking. |
33 | 45 | for _, getter := range c.Getters { |
34 | 46 | getter.SetClient(c) |
35 | 47 | } |
| 48 | + |
36 | 49 | return nil |
37 | 50 | } |
38 | 51 |
|
39 | 52 | // WithContext allows to pass a context to operation |
40 | 53 | // in order to be able to cancel a download in progress. |
41 | | -func WithContext(ctx context.Context) func(*Client) error { |
| 54 | +func WithContext(ctx context.Context) ClientOption { |
42 | 55 | return func(c *Client) error { |
43 | 56 | c.Ctx = ctx |
44 | 57 | return nil |
45 | 58 | } |
46 | 59 | } |
| 60 | + |
| 61 | +// WithDecompressors specifies which Decompressor are available. |
| 62 | +func WithDecompressors(decompressors map[string]Decompressor) ClientOption { |
| 63 | + return func(c *Client) error { |
| 64 | + c.Decompressors = decompressors |
| 65 | + return nil |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// WithDecompressors specifies which compressors are available. |
| 70 | +func WithDetectors(detectors []Detector) ClientOption { |
| 71 | + return func(c *Client) error { |
| 72 | + c.Detectors = detectors |
| 73 | + return nil |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// WithGetters specifies which getters are available. |
| 78 | +func WithGetters(getters map[string]Getter) ClientOption { |
| 79 | + return func(c *Client) error { |
| 80 | + c.Getters = getters |
| 81 | + return nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// WithMode specifies which client mode the getters should operate in. |
| 86 | +func WithMode(mode ClientMode) ClientOption { |
| 87 | + return func(c *Client) error { |
| 88 | + c.Mode = mode |
| 89 | + return nil |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// WithUmask specifies how to mask file permissions when storing local |
| 94 | +// files or decompressing an archive. |
| 95 | +func WithUmask(mode os.FileMode) ClientOption { |
| 96 | + return func(c *Client) error { |
| 97 | + c.Umask = mode |
| 98 | + return nil |
| 99 | + } |
| 100 | +} |
0 commit comments