Commit 5d1a25bb authored by Vitor Baptista's avatar Vitor Baptista

core/commands: pin ls: display types by default

If you want to get only the hashes (i.e. the previous behaviour), you can use
the `--quiet` flag.
parent 4c133697
......@@ -173,6 +173,7 @@ Defaults to "direct".
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.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) {
n, err := req.Context().GetNode()
......@@ -197,20 +198,29 @@ Defaults to "direct".
res.SetError(err, cmds.ErrClient)
}
keys := make(map[string]int)
keys := make(map[string]RefKeyObject)
if typeStr == "direct" || typeStr == "all" {
for _, k := range n.Pinning.DirectKeys() {
keys[k.B58String()] = 1
keys[k.B58String()] = RefKeyObject{
Type: "direct",
Count: 1,
}
}
}
if typeStr == "indirect" || typeStr == "all" {
for k, v := range n.Pinning.IndirectKeys() {
keys[k.B58String()] = v
keys[k.B58String()] = RefKeyObject{
Type: "indirect",
Count: v,
}
}
}
if typeStr == "recursive" || typeStr == "all" {
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
}
quiet, _, err := res.Request().Option("quiet").Bool()
if err != nil {
return nil, err
}
keys, ok := res.Output().(*RefKeyList)
if !ok {
return nil, u.ErrCast()
......@@ -236,11 +251,19 @@ Defaults to "direct".
out := new(bytes.Buffer)
if typeStr == "indirect" && count {
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 {
for k, _ := range keys.Keys {
fmt.Fprintf(out, "%s\n", k)
for k, v := range keys.Keys {
if quiet {
fmt.Fprintf(out, "%s\n", k)
} else {
fmt.Fprintf(out, "%s %s\n", k, v.Type)
}
}
}
return out, nil
......@@ -248,6 +271,11 @@ Defaults to "direct".
},
}
type RefKeyObject struct {
Type string
Count int
}
type RefKeyList struct {
Keys map[string]int
Keys map[string]RefKeyObject
}
......@@ -49,7 +49,7 @@ test_expect_success "file no longer pinned" '
echo "$HASH_WELCOME_DOCS" >expected2 &&
ipfs refs -r "$HASH_WELCOME_DOCS" >>expected2 &&
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >> expected2 &&
ipfs pin ls --type=recursive >actual2 &&
ipfs pin ls --type=recursive --quiet >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" '
ipfs refs "$MBLOCKHASH" >refsout &&
ipfs refs -r "$HASH_WELCOME_DOCS" >>refsout &&
sed -i="" "s/\(.*\)/\1 indirect/g" refsout &&
ipfs pin ls --type=indirect >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" '
echo "$DIRECTPIN" >directpinexpected &&
echo "$DIRECTPIN direct" >directpinexpected &&
ipfs pin ls --type=direct >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 QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >>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 &&
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 rp_actual >>allpins &&
cat indirectpins >>allpins &&
cat allpins | sort | uniq >> allpins_uniq &&
ipfs pin ls --type=all >actual_allpins &&
test_sort_cmp allpins_uniq actual_allpins
cut -f1 -d " " allpins | sort | uniq >> allpins_uniq_hashes &&
ipfs pin ls --type=all --quiet >actual_allpins &&
test_sort_cmp allpins_uniq_hashes actual_allpins
'
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