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

Commit 2d11597

Browse files
authored
Merge pull request #42 from apiarian/pin-list
pin/ls command exposed through Pins() method
2 parents 8e5f622 + 484fd3c commit 2d11597

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

shell.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,41 @@ func (s *Shell) Unpin(path string) error {
307307
return nil
308308
}
309309

310+
const (
311+
DirectPin = "direct"
312+
RecursivePin = "recursive"
313+
IndirectPin = "indirect"
314+
)
315+
316+
type PinInfo struct {
317+
Type string
318+
}
319+
320+
// Pins returns a map of the pin hashes to their info (currently just the
321+
// pin type, one of DirectPin, RecursivePin, or IndirectPin. A map is returned
322+
// instead of a slice because it is easier to do existence lookup by map key
323+
// than unordered array searching. The map is likely to be more useful to a
324+
// client than a flat list.
325+
func (s *Shell) Pins() (map[string]PinInfo, error) {
326+
resp, err := s.newRequest("pin/ls").Send(s.httpcli)
327+
if err != nil {
328+
return nil, err
329+
}
330+
defer resp.Close()
331+
332+
if resp.Error != nil {
333+
return nil, resp.Error
334+
}
335+
336+
raw := struct{ Keys map[string]PinInfo }{}
337+
err = json.NewDecoder(resp.Output).Decode(&raw)
338+
if err != nil {
339+
return nil, err
340+
}
341+
342+
return raw.Keys, nil
343+
}
344+
310345
type PeerInfo struct {
311346
Addrs []string
312347
ID string

shell_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,40 @@ func TestFileList(t *testing.T) {
9292
}
9393
}
9494

95+
func TestPins(t *testing.T) {
96+
is := is.New(t)
97+
s := NewShell(shellUrl)
98+
99+
// Add a thing, which pins it by default
100+
h, err := s.Add(bytes.NewBufferString("go-ipfs-api pins test 9F3D1F30-D12A-4024-9477-8F0C8E4B3A63"))
101+
is.Nil(err)
102+
103+
pins, err := s.Pins()
104+
is.Nil(err)
105+
106+
_, ok := pins[h]
107+
is.True(ok)
108+
109+
err = s.Unpin(h)
110+
is.Nil(err)
111+
112+
pins, err = s.Pins()
113+
is.Nil(err)
114+
115+
_, ok = pins[h]
116+
is.False(ok)
117+
118+
err = s.Pin(h)
119+
is.Nil(err)
120+
121+
pins, err = s.Pins()
122+
is.Nil(err)
123+
124+
info, ok := pins[h]
125+
is.True(ok)
126+
is.Equal(info.Type, RecursivePin)
127+
}
128+
95129
func TestPatch_rmLink(t *testing.T) {
96130
is := is.New(t)
97131
s := NewShell(shellUrl)

0 commit comments

Comments
 (0)