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.
Vitor Baptista committed
Apr 25, 2015 at 18:28 UTC
5d1a25bbb9c0991e9d693e35fe3b3b37a1a04ddd
2 files changed
+44
-14
core/commands/pin.go
+36
-8
@@ -173,6 +173,7 @@ Defaults to "direct".
173
Options: []cmds.Option{
174
cmds.StringOption("type", "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\". Defaults to \"direct\""),
175
cmds.BoolOption("count", "n", "Show refcount when listing indirect pins"),
176
+ cmds.BoolOption("quiet", "q", "Write just hashes of objects"),
177
},
178
Run: func(req cmds.Request, res cmds.Response) {
179
n, err := req.Context().GetNode()
@@ -197,20 +198,29 @@ Defaults to "direct".
198
res.SetError(err, cmds.ErrClient)
199
}
200
200
- keys := make(map[string]int)
201
+ keys := make(map[string]RefKeyObject)
202
if typeStr == "direct" || typeStr == "all" {
203
for _, k := range n.Pinning.DirectKeys() {
203
- keys[k.B58String()] = 1
204
+ keys[k.B58String()] = RefKeyObject{
205
+ Type: "direct",
206
+ Count: 1,
207
+ }
208
}
209
}
210
if typeStr == "indirect" || typeStr == "all" {
211
for k, v := range n.Pinning.IndirectKeys() {
208
- keys[k.B58String()] = v
212
+ keys[k.B58String()] = RefKeyObject{
213
+ Type: "indirect",
214
+ Count: v,
215
+ }
216
}
217
}
218
if typeStr == "recursive" || typeStr == "all" {
219
for _, k := range n.Pinning.RecursiveKeys() {
213
- keys[k.B58String()] = 1
220
+ keys[k.B58String()] = RefKeyObject{
221
+ Type: "recursive",
222
+ Count: 1,
223
+ }
224
}
225
}
226
@@ -229,6 +239,11 @@ Defaults to "direct".
239
return nil, err
240
}
241
242
+ quiet, _, err := res.Request().Option("quiet").Bool()
243
+ if err != nil {
244
+ return nil, err
245
+ }
246
+
247
keys, ok := res.Output().(*RefKeyList)
248
if !ok {
249
return nil, u.ErrCast()
@@ -236,11 +251,19 @@ Defaults to "direct".
251
out := new(bytes.Buffer)
252
if typeStr == "indirect" && count {
253
for k, v := range keys.Keys {
239
- fmt.Fprintf(out, "%s %d\n", k, v)
254
+ if quiet {
255
+ fmt.Fprintf(out, "%s\n", k, v.Count)
256
+ } else {
257
+ fmt.Fprintf(out, "%s %s %d\n", k, v.Type, v.Count)
258
+ }
259
}
260
} else {
242
- for k, _ := range keys.Keys {
243
- fmt.Fprintf(out, "%s\n", k)
261
+ for k, v := range keys.Keys {
262
+ if quiet {
263
+ fmt.Fprintf(out, "%s\n", k)
264
+ } else {
265
+ fmt.Fprintf(out, "%s %s\n", k, v.Type)
266
+ }
267
}
268
}
269
return out, nil
@@ -248,6 +271,11 @@ Defaults to "direct".
271
},
272
}
273
274
+type RefKeyObject struct {
275
+ Type string
276
+ Count int
277
+}
278
+
279
type RefKeyList struct {
252
- Keys map[string]int
280
+ Keys map[string]RefKeyObject
281
}
test/sharness/t0080-repo.sh
+8
-6
@@ -49,7 +49,7 @@ test_expect_success "file no longer pinned" '
49
echo "$HASH_WELCOME_DOCS" >expected2 &&
50
ipfs refs -r "$HASH_WELCOME_DOCS" >>expected2 &&
51
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >> expected2 &&
52
- ipfs pin ls --type=recursive >actual2 &&
52
+ ipfs pin ls --type=recursive --quiet >actual2 &&
53
test_sort_cmp expected2 actual2
54
'
55
@@ -105,6 +105,7 @@ test_expect_success "adding multiblock random file succeeds" '
105
test_expect_success "'ipfs pin ls --type=indirect' is correct" '
106
ipfs refs "$MBLOCKHASH" >refsout &&
107
ipfs refs -r "$HASH_WELCOME_DOCS" >>refsout &&
108
+ sed -i="" "s/\(.*\)/\1 indirect/g" refsout &&
109
ipfs pin ls --type=indirect >indirectpins &&
110
test_sort_cmp refsout indirectpins
111
'
@@ -122,7 +123,7 @@ test_expect_success "pin something directly" '
123
'
124
125
test_expect_success "'ipfs pin ls --type=direct' is correct" '
125
- echo "$DIRECTPIN" >directpinexpected &&
126
+ echo "$DIRECTPIN direct" >directpinexpected &&
127
ipfs pin ls --type=direct >directpinout &&
128
test_sort_cmp directpinexpected directpinout
129
'
@@ -132,17 +133,18 @@ test_expect_success "'ipfs pin ls --type=recursive' is correct" '
133
echo "$HASH_WELCOME_DOCS" >>rp_expected &&
134
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >>rp_expected &&
135
ipfs refs -r "$HASH_WELCOME_DOCS" >>rp_expected &&
136
+ sed -i="" "s/\(.*\)/\1 recursive/g" rp_expected &&
137
ipfs pin ls --type=recursive >rp_actual &&
138
test_sort_cmp rp_expected rp_actual
139
'
140
139
-test_expect_success "'ipfs pin ls --type=all' is correct" '
141
+test_expect_success "'ipfs pin ls --type=all --quiet' is correct" '
142
cat directpinout >allpins &&
143
cat rp_actual >>allpins &&
144
cat indirectpins >>allpins &&
143
- cat allpins | sort | uniq >> allpins_uniq &&
144
- ipfs pin ls --type=all >actual_allpins &&
145
- test_sort_cmp allpins_uniq actual_allpins
145
+ cut -f1 -d " " allpins | sort | uniq >> allpins_uniq_hashes &&
146
+ ipfs pin ls --type=all --quiet >actual_allpins &&
147
+ test_sort_cmp allpins_uniq_hashes actual_allpins
148
'
149
150
test_kill_ipfs_daemon