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