Commit 1b51d9db authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

Merge pull request #1138 from vitorbaptista/1134-display-types-on-pin-ls

core/commands: pin ls: display types by default
parents 4c133697 5d1a25bb
...@@ -173,6 +173,7 @@ Defaults to "direct". ...@@ -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". ...@@ -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". ...@@ -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". ...@@ -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". ...@@ -248,6 +271,11 @@ Defaults to "direct".
}, },
} }
type RefKeyObject struct {
Type string
Count int
}
type RefKeyList struct { type RefKeyList struct {
Keys map[string]int Keys map[string]RefKeyObject
} }
...@@ -49,7 +49,7 @@ test_expect_success "file no longer pinned" ' ...@@ -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" ' ...@@ -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" ' ...@@ -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" ' ...@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment