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

Commit c264934

Browse files
authored
Merge pull request #190 from MichaelMure/pin-ls-stream
pin ls --stream support
2 parents e25a99c + f19a34a commit c264934

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

shell.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ type PinInfo struct {
228228
}
229229

230230
// Pins returns a map of the pin hashes to their info (currently just the
231-
// pin type, one of DirectPin, RecursivePin, or IndirectPin. A map is returned
231+
// pin type, one of DirectPin, RecursivePin, or IndirectPin). A map is returned
232232
// instead of a slice because it is easier to do existence lookup by map key
233233
// than unordered array searching. The map is likely to be more useful to a
234234
// client than a flat list.
@@ -237,6 +237,49 @@ func (s *Shell) Pins() (map[string]PinInfo, error) {
237237
return raw.Keys, s.Request("pin/ls").Exec(context.Background(), &raw)
238238
}
239239

240+
// PinStreamInfo is the output type for PinsStream
241+
type PinStreamInfo struct {
242+
Cid string
243+
Type string
244+
}
245+
246+
// PinsStream is a streamed version of Pins. It returns a channel of the pins
247+
// with their type, one of DirectPin, RecursivePin, or IndirectPin.
248+
func (s *Shell) PinsStream(ctx context.Context) (<-chan PinStreamInfo, error) {
249+
resp, err := s.Request("pin/ls").
250+
Option("stream", true).
251+
Send(ctx)
252+
if err != nil {
253+
return nil, err
254+
}
255+
256+
if resp.Error != nil {
257+
resp.Close()
258+
return nil, resp.Error
259+
}
260+
261+
out := make(chan PinStreamInfo)
262+
go func() {
263+
defer resp.Close()
264+
var pin PinStreamInfo
265+
defer close(out)
266+
dec := json.NewDecoder(resp.Output)
267+
for {
268+
err := dec.Decode(&pin)
269+
if err != nil {
270+
return
271+
}
272+
select {
273+
case out <- pin:
274+
case <-ctx.Done():
275+
return
276+
}
277+
}
278+
}()
279+
280+
return out, nil
281+
}
282+
240283
type PeerInfo struct {
241284
Addrs []string
242285
ID string

shell_test.go

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

242+
func TestPinsStream(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 0C7023F8-1FEC-4155-A8E0-432A5853F46B"))
248+
is.Nil(err)
249+
250+
pinChan, err := s.PinsStream(context.Background())
251+
is.Nil(err)
252+
253+
pins := accumulatePins(pinChan)
254+
255+
_, ok := pins[h]
256+
is.True(ok)
257+
258+
err = s.Unpin(h)
259+
is.Nil(err)
260+
261+
pinChan, err = s.PinsStream(context.Background())
262+
is.Nil(err)
263+
264+
pins = accumulatePins(pinChan)
265+
266+
_, ok = pins[h]
267+
is.False(ok)
268+
269+
err = s.Pin(h)
270+
is.Nil(err)
271+
272+
pinChan, err = s.PinsStream(context.Background())
273+
is.Nil(err)
274+
275+
pins = accumulatePins(pinChan)
276+
277+
_type, ok := pins[h]
278+
is.True(ok)
279+
is.Equal(_type, RecursivePin)
280+
}
281+
282+
func accumulatePins(pinChan <-chan PinStreamInfo) map[string]string {
283+
pins := make(map[string]string)
284+
for pin := range pinChan {
285+
pins[pin.Cid] = pin.Type
286+
}
287+
return pins
288+
}
289+
242290
func TestPatch_rmLink(t *testing.T) {
243291
is := is.New(t)
244292
s := NewShell(shellUrl)

0 commit comments

Comments
 (0)