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

Commit 79bd293

Browse files
committed
pin: add a IsPinned method
1 parent 48dcede commit 79bd293

File tree

3 files changed

+228
-46
lines changed

3 files changed

+228
-46
lines changed

options/pin.go

Lines changed: 147 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package options
22

3+
import "fmt"
4+
35
type PinAddSettings struct {
46
Recursive bool
57
}
68

9+
type TypeSettings struct {
10+
Type string
11+
}
12+
713
type PinLsSettings struct {
814
Type string
915
}
1016

17+
type PinIsPinnedSettings struct {
18+
WithType string
19+
}
20+
1121
// PinRmSettings represents the settings of pin rm command
1222
type PinRmSettings struct {
1323
Recursive bool
@@ -17,13 +27,19 @@ type PinUpdateSettings struct {
1727
Unpin bool
1828
}
1929

30+
// PinAddOption pin add option func
2031
type PinAddOption func(*PinAddSettings) error
2132

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+
2239
// PinRmOption pin rm option func
2340
type PinRmOption func(*PinRmSettings) error
2441

25-
// PinLsOption pin ls option func
26-
type PinLsOption func(*PinLsSettings) error
42+
// PinUpdateOption pin update option func
2743
type PinUpdateOption func(*PinUpdateSettings) error
2844

2945
func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
@@ -41,24 +57,24 @@ func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
4157
return options, nil
4258
}
4359

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",
4863
}
4964

5065
for _, opt := range opts {
51-
if err := opt(options); err != nil {
66+
err := opt(options)
67+
if err != nil {
5268
return nil, err
5369
}
5470
}
5571

5672
return options, nil
5773
}
5874

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",
6278
}
6379

6480
for _, opt := range opts {
@@ -71,6 +87,21 @@ func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
7187
return options, nil
7288
}
7389

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+
74105
func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
75106
options := &PinUpdateSettings{
76107
Unpin: true,
@@ -86,36 +117,131 @@ func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
86117
return options, nil
87118
}
88119

89-
type pinType struct{}
90-
91120
type pinOpts struct {
92-
Type pinType
121+
Ls pinLsOpts
122+
IsPinned pinIsPinnedOpts
93123
}
94124

95125
var Pin pinOpts
96126

127+
type pinLsOpts struct{}
128+
97129
// All is an option for Pin.Ls which will make it return all pins. It is
98130
// the default
99-
func (pinType) All() PinLsOption {
100-
return Pin.pinType("all")
131+
func (pinLsOpts) All() PinLsOption {
132+
return Pin.Ls.pinType("all")
101133
}
102134

103135
// Recursive is an option for Pin.Ls which will make it only return recursive
104136
// pins
105-
func (pinType) Recursive() PinLsOption {
106-
return Pin.pinType("recursive")
137+
func (pinLsOpts) Recursive() PinLsOption {
138+
return Pin.Ls.pinType("recursive")
107139
}
108140

109141
// Direct is an option for Pin.Ls which will make it only return direct (non
110142
// recursive) pins
111-
func (pinType) Direct() PinLsOption {
112-
return Pin.pinType("direct")
143+
func (pinLsOpts) Direct() PinLsOption {
144+
return Pin.Ls.pinType("direct")
113145
}
114146

115147
// Indirect is an option for Pin.Ls which will make it only return indirect pins
116148
// (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+
}
119245
}
120246

121247
// Recursive is an option for Pin.Add which specifies whether to pin an entire
@@ -137,22 +263,6 @@ func (pinOpts) RmRecursive(recursive bool) PinRmOption {
137263
}
138264
}
139265

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-
156266
// Unpin is an option for Pin.Update which specifies whether to remove the old pin.
157267
// Default is true.
158268
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)