Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit ee0d435

Browse files
authored
Merge pull request #50 from MichaelMure/pinispinned
add Pin.IsPinned(..)
2 parents a39275b + 478caf0 commit ee0d435

File tree

3 files changed

+249
-48
lines changed

3 files changed

+249
-48
lines changed

options/pin.go

Lines changed: 159 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
11
package options
22

3+
import "fmt"
4+
5+
// PinAddSettings represent the settings for PinAPI.Add
36
type PinAddSettings struct {
47
Recursive bool
58
}
69

10+
// PinLsSettings represent the settings for PinAPI.Ls
711
type PinLsSettings struct {
812
Type string
913
}
1014

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
1221
type PinRmSettings struct {
1322
Recursive bool
1423
}
1524

25+
// PinUpdateSettings represent the settings for PinAPI.Update
1626
type PinUpdateSettings struct {
1727
Unpin bool
1828
}
1929

30+
// PinAddOption is the signature of an option for PinAPI.Add
2031
type PinAddOption func(*PinAddSettings) error
2132

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
2340
type PinRmOption func(*PinRmSettings) error
2441

25-
// PinLsOption pin ls option func
26-
type PinLsOption func(*PinLsSettings) error
42+
// PinUpdateOption is the signature of an option for PinAPI.Update
2743
type PinUpdateOption func(*PinUpdateSettings) error
2844

45+
// PinAddOptions compile a series of PinAddOption into a ready to use
46+
// PinAddSettings and set the default values.
2947
func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
3048
options := &PinAddSettings{
3149
Recursive: true,
@@ -41,24 +59,28 @@ func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
4159
return options, nil
4260
}
4361

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",
4867
}
4968

5069
for _, opt := range opts {
51-
if err := opt(options); err != nil {
70+
err := opt(options)
71+
if err != nil {
5272
return nil, err
5373
}
5474
}
5575

5676
return options, nil
5777
}
5878

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",
6284
}
6385

6486
for _, opt := range opts {
@@ -71,6 +93,24 @@ func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
7193
return options, nil
7294
}
7395

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.
74114
func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
75115
options := &PinUpdateSettings{
76116
Unpin: true,
@@ -86,36 +126,132 @@ func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
86126
return options, nil
87127
}
88128

89-
type pinType struct{}
90-
91129
type pinOpts struct {
92-
Type pinType
130+
Ls pinLsOpts
131+
IsPinned pinIsPinnedOpts
93132
}
94133

134+
// Pin provide an access to all the options for the Pin API.
95135
var Pin pinOpts
96136

137+
type pinLsOpts struct{}
138+
97139
// All is an option for Pin.Ls which will make it return all pins. It is
98140
// the default
99-
func (pinType) All() PinLsOption {
100-
return Pin.pinType("all")
141+
func (pinLsOpts) All() PinLsOption {
142+
return Pin.Ls.pinType("all")
101143
}
102144

103145
// Recursive is an option for Pin.Ls which will make it only return recursive
104146
// pins
105-
func (pinType) Recursive() PinLsOption {
106-
return Pin.pinType("recursive")
147+
func (pinLsOpts) Recursive() PinLsOption {
148+
return Pin.Ls.pinType("recursive")
107149
}
108150

109151
// Direct is an option for Pin.Ls which will make it only return direct (non
110152
// recursive) pins
111-
func (pinType) Direct() PinLsOption {
112-
return Pin.pinType("direct")
153+
func (pinLsOpts) Direct() PinLsOption {
154+
return Pin.Ls.pinType("direct")
113155
}
114156

115157
// Indirect is an option for Pin.Ls which will make it only return indirect pins
116158
// (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+
}
119255
}
120256

121257
// Recursive is an option for Pin.Add which specifies whether to pin an entire
@@ -137,22 +273,6 @@ func (pinOpts) RmRecursive(recursive bool) PinRmOption {
137273
}
138274
}
139275

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-
156276
// Unpin is an option for Pin.Update which specifies whether to remove the old pin.
157277
// Default is true.
158278
func (pinOpts) Unpin(unpin bool) PinUpdateOption {

pin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type PinAPI interface {
4646
// Ls returns list of pinned objects on this node
4747
Ls(context.Context, ...options.PinLsOption) (<-chan Pin, error)
4848

49+
// IsPinned returns whether or not the given cid is pinned
50+
// and an explanation of why its pinned
51+
IsPinned(context.Context, path.Path, ...options.PinIsPinnedOption) (string, bool, error)
52+
4953
// Rm removes pin for object specified by the path
5054
Rm(context.Context, path.Path, ...options.PinRmOption) error
5155

0 commit comments

Comments
 (0)