mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-02 12:20:03 +08:00
Merge pull request #1138 from vitorbaptista/1134-display-types-on-pin-ls
core/commands: pin ls: display types by default
This commit is contained in:
@ -173,6 +173,7 @@ Defaults to "direct".
|
|||||||
Options: []cmds.Option{
|
Options: []cmds.Option{
|
||||||
cmds.StringOption("type", "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\". Defaults to \"direct\""),
|
cmds.StringOption("type", "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\". Defaults to \"direct\""),
|
||||||
cmds.BoolOption("count", "n", "Show refcount when listing indirect pins"),
|
cmds.BoolOption("count", "n", "Show refcount when listing indirect pins"),
|
||||||
|
cmds.BoolOption("quiet", "q", "Write just hashes of objects"),
|
||||||
},
|
},
|
||||||
Run: func(req cmds.Request, res cmds.Response) {
|
Run: func(req cmds.Request, res cmds.Response) {
|
||||||
n, err := req.Context().GetNode()
|
n, err := req.Context().GetNode()
|
||||||
@ -197,20 +198,29 @@ Defaults to "direct".
|
|||||||
res.SetError(err, cmds.ErrClient)
|
res.SetError(err, cmds.ErrClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := make(map[string]int)
|
keys := make(map[string]RefKeyObject)
|
||||||
if typeStr == "direct" || typeStr == "all" {
|
if typeStr == "direct" || typeStr == "all" {
|
||||||
for _, k := range n.Pinning.DirectKeys() {
|
for _, k := range n.Pinning.DirectKeys() {
|
||||||
keys[k.B58String()] = 1
|
keys[k.B58String()] = RefKeyObject{
|
||||||
|
Type: "direct",
|
||||||
|
Count: 1,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if typeStr == "indirect" || typeStr == "all" {
|
if typeStr == "indirect" || typeStr == "all" {
|
||||||
for k, v := range n.Pinning.IndirectKeys() {
|
for k, v := range n.Pinning.IndirectKeys() {
|
||||||
keys[k.B58String()] = v
|
keys[k.B58String()] = RefKeyObject{
|
||||||
|
Type: "indirect",
|
||||||
|
Count: v,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if typeStr == "recursive" || typeStr == "all" {
|
if typeStr == "recursive" || typeStr == "all" {
|
||||||
for _, k := range n.Pinning.RecursiveKeys() {
|
for _, k := range n.Pinning.RecursiveKeys() {
|
||||||
keys[k.B58String()] = 1
|
keys[k.B58String()] = RefKeyObject{
|
||||||
|
Type: "recursive",
|
||||||
|
Count: 1,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,6 +239,11 @@ Defaults to "direct".
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
quiet, _, err := res.Request().Option("quiet").Bool()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
keys, ok := res.Output().(*RefKeyList)
|
keys, ok := res.Output().(*RefKeyList)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, u.ErrCast()
|
return nil, u.ErrCast()
|
||||||
@ -236,11 +251,19 @@ Defaults to "direct".
|
|||||||
out := new(bytes.Buffer)
|
out := new(bytes.Buffer)
|
||||||
if typeStr == "indirect" && count {
|
if typeStr == "indirect" && count {
|
||||||
for k, v := range keys.Keys {
|
for k, v := range keys.Keys {
|
||||||
fmt.Fprintf(out, "%s %d\n", k, v)
|
if quiet {
|
||||||
|
fmt.Fprintf(out, "%s\n", k, v.Count)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(out, "%s %s %d\n", k, v.Type, v.Count)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for k, _ := range keys.Keys {
|
for k, v := range keys.Keys {
|
||||||
fmt.Fprintf(out, "%s\n", k)
|
if quiet {
|
||||||
|
fmt.Fprintf(out, "%s\n", k)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(out, "%s %s\n", k, v.Type)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
@ -248,6 +271,11 @@ Defaults to "direct".
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type RefKeyList struct {
|
type RefKeyObject struct {
|
||||||
Keys map[string]int
|
Type string
|
||||||
|
Count int
|
||||||
|
}
|
||||||
|
|
||||||
|
type RefKeyList struct {
|
||||||
|
Keys map[string]RefKeyObject
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ test_expect_success "file no longer pinned" '
|
|||||||
echo "$HASH_WELCOME_DOCS" >expected2 &&
|
echo "$HASH_WELCOME_DOCS" >expected2 &&
|
||||||
ipfs refs -r "$HASH_WELCOME_DOCS" >>expected2 &&
|
ipfs refs -r "$HASH_WELCOME_DOCS" >>expected2 &&
|
||||||
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >> expected2 &&
|
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >> expected2 &&
|
||||||
ipfs pin ls --type=recursive >actual2 &&
|
ipfs pin ls --type=recursive --quiet >actual2 &&
|
||||||
test_sort_cmp expected2 actual2
|
test_sort_cmp expected2 actual2
|
||||||
'
|
'
|
||||||
|
|
||||||
@ -105,6 +105,7 @@ test_expect_success "adding multiblock random file succeeds" '
|
|||||||
test_expect_success "'ipfs pin ls --type=indirect' is correct" '
|
test_expect_success "'ipfs pin ls --type=indirect' is correct" '
|
||||||
ipfs refs "$MBLOCKHASH" >refsout &&
|
ipfs refs "$MBLOCKHASH" >refsout &&
|
||||||
ipfs refs -r "$HASH_WELCOME_DOCS" >>refsout &&
|
ipfs refs -r "$HASH_WELCOME_DOCS" >>refsout &&
|
||||||
|
sed -i="" "s/\(.*\)/\1 indirect/g" refsout &&
|
||||||
ipfs pin ls --type=indirect >indirectpins &&
|
ipfs pin ls --type=indirect >indirectpins &&
|
||||||
test_sort_cmp refsout indirectpins
|
test_sort_cmp refsout indirectpins
|
||||||
'
|
'
|
||||||
@ -122,7 +123,7 @@ test_expect_success "pin something directly" '
|
|||||||
'
|
'
|
||||||
|
|
||||||
test_expect_success "'ipfs pin ls --type=direct' is correct" '
|
test_expect_success "'ipfs pin ls --type=direct' is correct" '
|
||||||
echo "$DIRECTPIN" >directpinexpected &&
|
echo "$DIRECTPIN direct" >directpinexpected &&
|
||||||
ipfs pin ls --type=direct >directpinout &&
|
ipfs pin ls --type=direct >directpinout &&
|
||||||
test_sort_cmp directpinexpected directpinout
|
test_sort_cmp directpinexpected directpinout
|
||||||
'
|
'
|
||||||
@ -132,17 +133,18 @@ test_expect_success "'ipfs pin ls --type=recursive' is correct" '
|
|||||||
echo "$HASH_WELCOME_DOCS" >>rp_expected &&
|
echo "$HASH_WELCOME_DOCS" >>rp_expected &&
|
||||||
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >>rp_expected &&
|
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >>rp_expected &&
|
||||||
ipfs refs -r "$HASH_WELCOME_DOCS" >>rp_expected &&
|
ipfs refs -r "$HASH_WELCOME_DOCS" >>rp_expected &&
|
||||||
|
sed -i="" "s/\(.*\)/\1 recursive/g" rp_expected &&
|
||||||
ipfs pin ls --type=recursive >rp_actual &&
|
ipfs pin ls --type=recursive >rp_actual &&
|
||||||
test_sort_cmp rp_expected rp_actual
|
test_sort_cmp rp_expected rp_actual
|
||||||
'
|
'
|
||||||
|
|
||||||
test_expect_success "'ipfs pin ls --type=all' is correct" '
|
test_expect_success "'ipfs pin ls --type=all --quiet' is correct" '
|
||||||
cat directpinout >allpins &&
|
cat directpinout >allpins &&
|
||||||
cat rp_actual >>allpins &&
|
cat rp_actual >>allpins &&
|
||||||
cat indirectpins >>allpins &&
|
cat indirectpins >>allpins &&
|
||||||
cat allpins | sort | uniq >> allpins_uniq &&
|
cut -f1 -d " " allpins | sort | uniq >> allpins_uniq_hashes &&
|
||||||
ipfs pin ls --type=all >actual_allpins &&
|
ipfs pin ls --type=all --quiet >actual_allpins &&
|
||||||
test_sort_cmp allpins_uniq actual_allpins
|
test_sort_cmp allpins_uniq_hashes actual_allpins
|
||||||
'
|
'
|
||||||
|
|
||||||
test_kill_ipfs_daemon
|
test_kill_ipfs_daemon
|
||||||
|
Reference in New Issue
Block a user