From 59fb584552e0a6e78783b8ba815fc2af067839d4 Mon Sep 17 00:00:00 2001 From: ani1311 Date: Thu, 15 Apr 2021 00:04:31 +0530 Subject: [PATCH 1/2] Added Type field for Pin ls command --- shell.go | 14 +++++++++++--- shell_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/shell.go b/shell.go index e6904067e..c7c77a675 100644 --- a/shell.go +++ b/shell.go @@ -217,10 +217,12 @@ func (s *Shell) Unpin(path string) error { Exec(context.Background(), nil) } +type PinType string + const ( - DirectPin = "direct" - RecursivePin = "recursive" - IndirectPin = "indirect" + DirectPin PinType = "direct" + RecursivePin PinType = "recursive" + IndirectPin PinType = "indirect" ) type PinInfo struct { @@ -237,6 +239,12 @@ func (s *Shell) Pins() (map[string]PinInfo, error) { return raw.Keys, s.Request("pin/ls").Exec(context.Background(), &raw) } +// Pins returns a map of the pins of specified type (DirectPin, RecursivePin, or IndirectPin) +func (s *Shell) PinsOfType(pinType PinType) (map[string]PinInfo, error) { + var raw struct{ Keys map[string]PinInfo } + return raw.Keys, s.Request("pin/ls").Option("type", pinType).Exec(context.Background(), &raw) +} + // PinStreamInfo is the output type for PinsStream type PinStreamInfo struct { Cid string diff --git a/shell_test.go b/shell_test.go index 15c7ee61d..4555fe420 100644 --- a/shell_test.go +++ b/shell_test.go @@ -239,6 +239,40 @@ func TestPins(t *testing.T) { is.Equal(info.Type, RecursivePin) } +func TestPinsOfType(t *testing.T) { + is := is.New(t) + s := NewShell(shellUrl) + + // Add a thing, which pins it by default + h, err := s.Add(bytes.NewBufferString("go-ipfs-api pins test 9F3D1F30-D12A-4024-9477-8F0C8E4B3A63")) + is.Nil(err) + + pins, err := s.PinsOfType(RecursivePin) + is.Nil(err) + + _, ok := pins[h] + is.True(ok) + + err = s.Unpin(h) + is.Nil(err) + + pins, err = s.Pins() + is.Nil(err) + + _, ok = pins[h] + is.False(ok) + + err = s.Pin(h) + is.Nil(err) + + pins, err = s.Pins() + is.Nil(err) + + info, ok := pins[h] + is.True(ok) + is.Equal(info.Type, RecursivePin) +} + func TestPinsStream(t *testing.T) { is := is.New(t) s := NewShell(shellUrl) From 7f18df88e1574ebe450e1dbbd8145ca42bf25edb Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 22 Apr 2021 00:32:06 -0700 Subject: [PATCH 2/2] fix: make PinsOfType take a context We haven't bothered changing existing functions to reduce breaking changes, but we might as well add contexts to new functions. --- shell.go | 4 ++-- shell_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shell.go b/shell.go index c7c77a675..5ee022516 100644 --- a/shell.go +++ b/shell.go @@ -240,9 +240,9 @@ func (s *Shell) Pins() (map[string]PinInfo, error) { } // Pins returns a map of the pins of specified type (DirectPin, RecursivePin, or IndirectPin) -func (s *Shell) PinsOfType(pinType PinType) (map[string]PinInfo, error) { +func (s *Shell) PinsOfType(ctx context.Context, pinType PinType) (map[string]PinInfo, error) { var raw struct{ Keys map[string]PinInfo } - return raw.Keys, s.Request("pin/ls").Option("type", pinType).Exec(context.Background(), &raw) + return raw.Keys, s.Request("pin/ls").Option("type", pinType).Exec(ctx, &raw) } // PinStreamInfo is the output type for PinsStream diff --git a/shell_test.go b/shell_test.go index 4555fe420..b34eb5b1a 100644 --- a/shell_test.go +++ b/shell_test.go @@ -247,7 +247,7 @@ func TestPinsOfType(t *testing.T) { h, err := s.Add(bytes.NewBufferString("go-ipfs-api pins test 9F3D1F30-D12A-4024-9477-8F0C8E4B3A63")) is.Nil(err) - pins, err := s.PinsOfType(RecursivePin) + pins, err := s.PinsOfType(context.Background(), RecursivePin) is.Nil(err) _, ok := pins[h]