mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-28 13:57:52 +08:00
feat: enables searching pins by name (#10412)
Co-authored-by: Henrique Dias <mail@hacdias.com>
This commit is contained in:
parent
2841ec0fcd
commit
ae05085644
@ -362,6 +362,7 @@ Example:
|
||||
cmds.BoolOption(pinQuietOptionName, "q", "Write just hashes of objects."),
|
||||
cmds.BoolOption(pinStreamOptionName, "s", "Enable streaming of pins as they are discovered."),
|
||||
cmds.BoolOption(pinNamesOptionName, "n", "Enable displaying pin names (slower)."),
|
||||
cmds.StringOption(pinNameOptionName, "Display pins with names that contain the value provided (case-sensitive, exact match)."),
|
||||
},
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
api, err := cmdenv.GetApi(env, req)
|
||||
@ -372,6 +373,7 @@ Example:
|
||||
typeStr, _ := req.Options[pinTypeOptionName].(string)
|
||||
stream, _ := req.Options[pinStreamOptionName].(bool)
|
||||
displayNames, _ := req.Options[pinNamesOptionName].(bool)
|
||||
name, _ := req.Options[pinNameOptionName].(string)
|
||||
|
||||
switch typeStr {
|
||||
case "all", "direct", "indirect", "recursive":
|
||||
@ -397,7 +399,7 @@ Example:
|
||||
if len(req.Arguments) > 0 {
|
||||
err = pinLsKeys(req, typeStr, api, emit)
|
||||
} else {
|
||||
err = pinLsAll(req, typeStr, displayNames, api, emit)
|
||||
err = pinLsAll(req, typeStr, displayNames || name != "", name, api, emit)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@ -537,7 +539,7 @@ func pinLsKeys(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fu
|
||||
return nil
|
||||
}
|
||||
|
||||
func pinLsAll(req *cmds.Request, typeStr string, detailed bool, api coreiface.CoreAPI, emit func(value PinLsOutputWrapper) error) error {
|
||||
func pinLsAll(req *cmds.Request, typeStr string, detailed bool, name string, api coreiface.CoreAPI, emit func(value PinLsOutputWrapper) error) error {
|
||||
enc, err := cmdenv.GetCidEncoder(req)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -555,7 +557,7 @@ func pinLsAll(req *cmds.Request, typeStr string, detailed bool, api coreiface.Co
|
||||
panic("unhandled pin type")
|
||||
}
|
||||
|
||||
pins, err := api.Pin().Ls(req.Context, opt, options.Pin.Ls.Detailed(detailed))
|
||||
pins, err := api.Pin().Ls(req.Context, opt, options.Pin.Ls.Detailed(detailed), options.Pin.Ls.Name(name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package coreapi
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
bserv "github.com/ipfs/boxo/blockservice"
|
||||
offline "github.com/ipfs/boxo/exchange/offline"
|
||||
@ -67,7 +68,7 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) (<-chan c
|
||||
return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", settings.Type)
|
||||
}
|
||||
|
||||
return api.pinLsAll(ctx, settings.Type, settings.Detailed), nil
|
||||
return api.pinLsAll(ctx, settings.Type, settings.Detailed, settings.Name), nil
|
||||
}
|
||||
|
||||
func (api *PinAPI) IsPinned(ctx context.Context, p path.Path, opts ...caopts.PinIsPinnedOption) (string, bool, error) {
|
||||
@ -276,17 +277,17 @@ func (p *pinInfo) Err() error {
|
||||
//
|
||||
// The caller must keep reading results until the channel is closed to prevent
|
||||
// leaking the goroutine that is fetching pins.
|
||||
func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string, detailed bool) <-chan coreiface.Pin {
|
||||
func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string, detailed bool, name string) <-chan coreiface.Pin {
|
||||
out := make(chan coreiface.Pin, 1)
|
||||
|
||||
emittedSet := cid.NewSet()
|
||||
|
||||
AddToResultKeys := func(c cid.Cid, name, typeStr string) error {
|
||||
if emittedSet.Visit(c) {
|
||||
AddToResultKeys := func(c cid.Cid, pinName, typeStr string) error {
|
||||
if emittedSet.Visit(c) && (name == "" || strings.Contains(pinName, name)) {
|
||||
select {
|
||||
case out <- &pinInfo{
|
||||
pinType: typeStr,
|
||||
name: name,
|
||||
name: pinName,
|
||||
path: path.FromCid(c),
|
||||
}:
|
||||
case <-ctx.Done():
|
||||
|
||||
@ -12,6 +12,7 @@ type PinAddSettings struct {
|
||||
type PinLsSettings struct {
|
||||
Type string
|
||||
Detailed bool
|
||||
Name string
|
||||
}
|
||||
|
||||
// PinIsPinnedSettings represent the settings for PinAPI.IsPinned
|
||||
@ -205,6 +206,13 @@ func (pinLsOpts) Detailed(detailed bool) PinLsOption {
|
||||
}
|
||||
}
|
||||
|
||||
func (pinLsOpts) Name(name string) PinLsOption {
|
||||
return func(settings *PinLsSettings) error {
|
||||
settings.Name = name
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type pinIsPinnedOpts struct{}
|
||||
|
||||
// All is an option for Pin.IsPinned which will make it search in all type of pins.
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
|
||||
- [Overview](#overview)
|
||||
- [🔦 Highlights](#-highlights)
|
||||
- [Add search functionality for pin names](#add-search-functionality-for-pin-names)
|
||||
- [📝 Changelog](#-changelog)
|
||||
- [👨👩👧👦 Contributors](#-contributors)
|
||||
|
||||
@ -13,6 +14,10 @@
|
||||
|
||||
### 🔦 Highlights
|
||||
|
||||
#### Add search functionality for pin names
|
||||
|
||||
It is now possible to search for pins by name. To do so, use `ipfs pin ls --name "SomeName"`. The search is case-sensitive and will return all pins having a name which contains the exact word provided.
|
||||
|
||||
### 📝 Changelog
|
||||
|
||||
### 👨👩👧👦 Contributors
|
||||
|
||||
@ -242,6 +242,41 @@ func TestPins(t *testing.T) {
|
||||
require.NotContains(t, lsOut, outADetailed)
|
||||
})
|
||||
|
||||
t.Run("test listing pins which contains specific name", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
node := harness.NewT(t).NewNode().Init()
|
||||
cidAStr := node.IPFSAddStr(RandomStr(1000), "--pin=false")
|
||||
cidBStr := node.IPFSAddStr(RandomStr(1000), "--pin=false")
|
||||
cidCStr := node.IPFSAddStr(RandomStr(1000), "--pin=false")
|
||||
|
||||
outA := cidAStr + " recursive testPin"
|
||||
outB := cidBStr + " recursive testPin"
|
||||
outC := cidCStr + " recursive randPin"
|
||||
|
||||
_ = node.IPFS("pin", "add", "--name", "testPin", cidAStr)
|
||||
lsOut := pinLs(node, "-t=recursive", "--name=test")
|
||||
require.Contains(t, lsOut, outA)
|
||||
lsOut = pinLs(node, "-t=recursive", "--name=randomLabel")
|
||||
require.NotContains(t, lsOut, outA)
|
||||
|
||||
_ = node.IPFS("pin", "add", "--name", "testPin", cidBStr)
|
||||
lsOut = pinLs(node, "-t=recursive", "--name=test")
|
||||
require.Contains(t, lsOut, outA)
|
||||
require.Contains(t, lsOut, outB)
|
||||
|
||||
_ = node.IPFS("pin", "add", "--name", "randPin", cidCStr)
|
||||
lsOut = pinLs(node, "-t=recursive", "--name=rand")
|
||||
require.NotContains(t, lsOut, outA)
|
||||
require.NotContains(t, lsOut, outB)
|
||||
require.Contains(t, lsOut, outC)
|
||||
|
||||
lsOut = pinLs(node, "-t=recursive", "--name=testPin")
|
||||
require.Contains(t, lsOut, outA)
|
||||
require.Contains(t, lsOut, outB)
|
||||
require.NotContains(t, lsOut, outC)
|
||||
})
|
||||
|
||||
t.Run("test overwriting pin with name", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user