Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit eaf54e8

Browse files
authored
Merge pull request #240 from ani1311/feat/type-for-pin-ls
Added Type field for Pin ls command
2 parents 009d1b1 + 7f18df8 commit eaf54e8

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

shell.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,12 @@ func (s *Shell) Unpin(path string) error {
217217
Exec(context.Background(), nil)
218218
}
219219

220+
type PinType string
221+
220222
const (
221-
DirectPin = "direct"
222-
RecursivePin = "recursive"
223-
IndirectPin = "indirect"
223+
DirectPin PinType = "direct"
224+
RecursivePin PinType = "recursive"
225+
IndirectPin PinType = "indirect"
224226
)
225227

226228
type PinInfo struct {
@@ -237,6 +239,12 @@ func (s *Shell) Pins() (map[string]PinInfo, error) {
237239
return raw.Keys, s.Request("pin/ls").Exec(context.Background(), &raw)
238240
}
239241

242+
// Pins returns a map of the pins of specified type (DirectPin, RecursivePin, or IndirectPin)
243+
func (s *Shell) PinsOfType(ctx context.Context, pinType PinType) (map[string]PinInfo, error) {
244+
var raw struct{ Keys map[string]PinInfo }
245+
return raw.Keys, s.Request("pin/ls").Option("type", pinType).Exec(ctx, &raw)
246+
}
247+
240248
// PinStreamInfo is the output type for PinsStream
241249
type PinStreamInfo struct {
242250
Cid string

shell_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,40 @@ func TestPins(t *testing.T) {
239239
is.Equal(info.Type, RecursivePin)
240240
}
241241

242+
func TestPinsOfType(t *testing.T) {
243+
is := is.New(t)
244+
s := NewShell(shellUrl)
245+
246+
// Add a thing, which pins it by default
247+
h, err := s.Add(bytes.NewBufferString("go-ipfs-api pins test 9F3D1F30-D12A-4024-9477-8F0C8E4B3A63"))
248+
is.Nil(err)
249+
250+
pins, err := s.PinsOfType(context.Background(), RecursivePin)
251+
is.Nil(err)
252+
253+
_, ok := pins[h]
254+
is.True(ok)
255+
256+
err = s.Unpin(h)
257+
is.Nil(err)
258+
259+
pins, err = s.Pins()
260+
is.Nil(err)
261+
262+
_, ok = pins[h]
263+
is.False(ok)
264+
265+
err = s.Pin(h)
266+
is.Nil(err)
267+
268+
pins, err = s.Pins()
269+
is.Nil(err)
270+
271+
info, ok := pins[h]
272+
is.True(ok)
273+
is.Equal(info.Type, RecursivePin)
274+
}
275+
242276
func TestPinsStream(t *testing.T) {
243277
is := is.New(t)
244278
s := NewShell(shellUrl)

0 commit comments

Comments
 (0)