master
go 310 lines 8.39 KB
Raw
1 package options
2
3 import "fmt"
4
5 // PinAddSettings represent the settings for PinAPI.Add
6 type PinAddSettings struct {
7 Recursive bool
8 Name string
9 }
10
11 // PinLsSettings represent the settings for PinAPI.Ls
12 type PinLsSettings struct {
13 Type string
14 Detailed bool
15 Name string
16 }
17
18 // PinIsPinnedSettings represent the settings for PinAPI.IsPinned
19 type PinIsPinnedSettings struct {
20 WithType string
21 }
22
23 // PinRmSettings represents the settings for PinAPI.Rm
24 type PinRmSettings struct {
25 Recursive bool
26 }
27
28 // PinUpdateSettings represent the settings for PinAPI.Update
29 type PinUpdateSettings struct {
30 Unpin bool
31 }
32
33 // PinAddOption is the signature of an option for PinAPI.Add
34 type PinAddOption func(*PinAddSettings) error
35
36 // PinLsOption is the signature of an option for PinAPI.Ls
37 type PinLsOption func(*PinLsSettings) error
38
39 // PinIsPinnedOption is the signature of an option for PinAPI.IsPinned
40 type PinIsPinnedOption func(*PinIsPinnedSettings) error
41
42 // PinRmOption is the signature of an option for PinAPI.Rm
43 type PinRmOption func(*PinRmSettings) error
44
45 // PinUpdateOption is the signature of an option for PinAPI.Update
46 type PinUpdateOption func(*PinUpdateSettings) error
47
48 // PinAddOptions compile a series of PinAddOption into a ready to use
49 // PinAddSettings and set the default values.
50 func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
51 options := &PinAddSettings{
52 Recursive: true,
53 }
54
55 for _, opt := range opts {
56 err := opt(options)
57 if err != nil {
58 return nil, err
59 }
60 }
61
62 return options, nil
63 }
64
65 // PinLsOptions compile a series of PinLsOption into a ready to use
66 // PinLsSettings and set the default values.
67 func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
68 options := &PinLsSettings{
69 Type: "all",
70 }
71
72 for _, opt := range opts {
73 err := opt(options)
74 if err != nil {
75 return nil, err
76 }
77 }
78
79 return options, nil
80 }
81
82 // PinIsPinnedOptions compile a series of PinIsPinnedOption into a ready to use
83 // PinIsPinnedSettings and set the default values.
84 func PinIsPinnedOptions(opts ...PinIsPinnedOption) (*PinIsPinnedSettings, error) {
85 options := &PinIsPinnedSettings{
86 WithType: "all",
87 }
88
89 for _, opt := range opts {
90 err := opt(options)
91 if err != nil {
92 return nil, err
93 }
94 }
95
96 return options, nil
97 }
98
99 // PinRmOptions compile a series of PinRmOption into a ready to use
100 // PinRmSettings and set the default values.
101 func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) {
102 options := &PinRmSettings{
103 Recursive: true,
104 }
105
106 for _, opt := range opts {
107 if err := opt(options); err != nil {
108 return nil, err
109 }
110 }
111
112 return options, nil
113 }
114
115 // PinUpdateOptions compile a series of PinUpdateOption into a ready to use
116 // PinUpdateSettings and set the default values.
117 func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
118 options := &PinUpdateSettings{
119 Unpin: true,
120 }
121
122 for _, opt := range opts {
123 err := opt(options)
124 if err != nil {
125 return nil, err
126 }
127 }
128
129 return options, nil
130 }
131
132 type pinOpts struct {
133 Ls pinLsOpts
134 IsPinned pinIsPinnedOpts
135 }
136
137 // Pin provide an access to all the options for the Pin API.
138 var Pin pinOpts
139
140 type pinLsOpts struct{}
141
142 // All is an option for Pin.Ls which will make it return all pins. It is
143 // the default
144 func (pinLsOpts) All() PinLsOption {
145 return Pin.Ls.pinType("all")
146 }
147
148 // Recursive is an option for Pin.Ls which will make it only return recursive
149 // pins
150 func (pinLsOpts) Recursive() PinLsOption {
151 return Pin.Ls.pinType("recursive")
152 }
153
154 // Direct is an option for Pin.Ls which will make it only return direct (non
155 // recursive) pins
156 func (pinLsOpts) Direct() PinLsOption {
157 return Pin.Ls.pinType("direct")
158 }
159
160 // Indirect is an option for Pin.Ls which will make it only return indirect pins
161 // (objects referenced by other recursively pinned objects)
162 func (pinLsOpts) Indirect() PinLsOption {
163 return Pin.Ls.pinType("indirect")
164 }
165
166 // Type is an option for Pin.Ls which will make it only return pins of the given
167 // type.
168 //
169 // Supported values:
170 // - "direct" - directly pinned objects
171 // - "recursive" - roots of recursive pins
172 // - "indirect" - indirectly pinned objects (referenced by recursively pinned
173 // objects)
174 // - "all" - all pinned objects (default)
175 func (pinLsOpts) Type(typeStr string) (PinLsOption, error) {
176 switch typeStr {
177 case "all", "direct", "indirect", "recursive":
178 return Pin.Ls.pinType(typeStr), nil
179 default:
180 return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
181 }
182 }
183
184 // pinType is an option for Pin.Ls which allows to specify which pin types should
185 // be returned
186 //
187 // Supported values:
188 // - "direct" - directly pinned objects
189 // - "recursive" - roots of recursive pins
190 // - "indirect" - indirectly pinned objects (referenced by recursively pinned
191 // objects)
192 // - "all" - all pinned objects (default)
193 func (pinLsOpts) pinType(t string) PinLsOption {
194 return func(settings *PinLsSettings) error {
195 settings.Type = t
196 return nil
197 }
198 }
199
200 // Detailed is an option for [Pin.Ls] which sets whether or not to return
201 // detailed information, such as pin names and modes.
202 func (pinLsOpts) Detailed(detailed bool) PinLsOption {
203 return func(settings *PinLsSettings) error {
204 settings.Detailed = detailed
205 return nil
206 }
207 }
208
209 func (pinLsOpts) Name(name string) PinLsOption {
210 return func(settings *PinLsSettings) error {
211 settings.Name = name
212 return nil
213 }
214 }
215
216 type pinIsPinnedOpts struct{}
217
218 // All is an option for Pin.IsPinned which will make it search in all type of pins.
219 // It is the default
220 func (pinIsPinnedOpts) All() PinIsPinnedOption {
221 return Pin.IsPinned.pinType("all")
222 }
223
224 // Recursive is an option for Pin.IsPinned which will make it only search in
225 // recursive pins
226 func (pinIsPinnedOpts) Recursive() PinIsPinnedOption {
227 return Pin.IsPinned.pinType("recursive")
228 }
229
230 // Direct is an option for Pin.IsPinned which will make it only search in direct
231 // (non recursive) pins
232 func (pinIsPinnedOpts) Direct() PinIsPinnedOption {
233 return Pin.IsPinned.pinType("direct")
234 }
235
236 // Indirect is an option for Pin.IsPinned which will make it only search indirect
237 // pins (objects referenced by other recursively pinned objects)
238 func (pinIsPinnedOpts) Indirect() PinIsPinnedOption {
239 return Pin.IsPinned.pinType("indirect")
240 }
241
242 // Type is an option for Pin.IsPinned which will make it only search pins of the given
243 // type.
244 //
245 // Supported values:
246 // - "direct" - directly pinned objects
247 // - "recursive" - roots of recursive pins
248 // - "indirect" - indirectly pinned objects (referenced by recursively pinned
249 // objects)
250 // - "all" - all pinned objects (default)
251 func (pinIsPinnedOpts) Type(typeStr string) (PinIsPinnedOption, error) {
252 switch typeStr {
253 case "all", "direct", "indirect", "recursive":
254 return Pin.IsPinned.pinType(typeStr), nil
255 default:
256 return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
257 }
258 }
259
260 // pinType is an option for Pin.IsPinned which allows to specify which pin type the given
261 // pin is expected to be, speeding up the research.
262 //
263 // Supported values:
264 // - "direct" - directly pinned objects
265 // - "recursive" - roots of recursive pins
266 // - "indirect" - indirectly pinned objects (referenced by recursively pinned
267 // objects)
268 // - "all" - all pinned objects (default)
269 func (pinIsPinnedOpts) pinType(t string) PinIsPinnedOption {
270 return func(settings *PinIsPinnedSettings) error {
271 settings.WithType = t
272 return nil
273 }
274 }
275
276 // Recursive is an option for Pin.Add which specifies whether to pin an entire
277 // object tree or just one object. Default: true
278 func (pinOpts) Recursive(recursive bool) PinAddOption {
279 return func(settings *PinAddSettings) error {
280 settings.Recursive = recursive
281 return nil
282 }
283 }
284
285 // Name is an option for Pin.Add which specifies an optional name to add to the pin.
286 func (pinOpts) Name(name string) PinAddOption {
287 return func(settings *PinAddSettings) error {
288 settings.Name = name
289 return nil
290 }
291 }
292
293 // RmRecursive is an option for Pin.Rm which specifies whether to recursively
294 // unpin the object linked to by the specified object(s). This does not remove
295 // indirect pins referenced by other recursive pins.
296 func (pinOpts) RmRecursive(recursive bool) PinRmOption {
297 return func(settings *PinRmSettings) error {
298 settings.Recursive = recursive
299 return nil
300 }
301 }
302
303 // Unpin is an option for Pin.Update which specifies whether to remove the old pin.
304 // Default is true.
305 func (pinOpts) Unpin(unpin bool) PinUpdateOption {
306 return func(settings *PinUpdateSettings) error {
307 settings.Unpin = unpin
308 return nil
309 }
310 }