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