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