commands/pin: use new cmds lib
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Oct 27, 2018 at 12:26 UTC
6fc049b59f4989e88bf059dac5fb39d8fd6282ab
2 files changed
+103
-188
core/commands/pin.go
+102
-187
@@ -1,20 +1,19 @@
1
package commands
2
3
import (
4
- "bytes"
4
"context"
5
"fmt"
6
"io"
7
"time"
8
10
- cmds "github.com/ipfs/go-ipfs/commands"
9
core "github.com/ipfs/go-ipfs/core"
12
- e "github.com/ipfs/go-ipfs/core/commands/e"
10
+ cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
14
pin "github.com/ipfs/go-ipfs/pin"
15
16
+ cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds"
17
offline "gx/ipfs/QmPpnbwgAuvhUkA9jGooR88ZwZtTUHXXvoQNKdjZC6nYku/go-ipfs-exchange-offline"
18
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
19
bserv "gx/ipfs/QmVPeMNK9DfGLXDZzs2W4RoFWC9Zq1EnLGmLXtYtWrNdcW/go-blockservice"
@@ -65,43 +64,36 @@ var addPinCmd = &cmds.Command{
64
cmdkit.BoolOption(pinProgressOptionName, "Show progress"),
65
},
66
Type: AddPinOutput{},
68
- Run: func(req cmds.Request, res cmds.Response) {
69
- n, err := req.InvocContext().GetNode()
67
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
68
+ n, err := cmdenv.GetNode(env)
69
if err != nil {
71
- res.SetError(err, cmdkit.ErrNormal)
72
- return
70
+ return err
71
}
72
75
- api, err := req.InvocContext().GetApi()
73
+ api, err := cmdenv.GetApi(env)
74
if err != nil {
77
- res.SetError(err, cmdkit.ErrNormal)
78
- return
75
+ return err
76
}
77
78
defer n.Blockstore.PinLock().Unlock()
79
80
// set recursive flag
84
- recursive, _, err := req.Option(pinRecursiveOptionName).Bool()
85
- if err != nil {
86
- res.SetError(err, cmdkit.ErrNormal)
87
- return
88
- }
89
- showProgress, _, _ := req.Option(pinProgressOptionName).Bool()
81
+ recursive, _ := req.Options[pinRecursiveOptionName].(bool)
82
+ showProgress, _ := req.Options[pinProgressOptionName].(bool)
83
84
if !showProgress {
92
- added, err := corerepo.Pin(n, api, req.Context(), req.Arguments(), recursive)
85
+ added, err := corerepo.Pin(n, api, req.Context, req.Arguments, recursive)
86
if err != nil {
94
- res.SetError(err, cmdkit.ErrNormal)
95
- return
87
+ return err
88
}
97
- res.SetOutput(&AddPinOutput{Pins: cidsToStrings(added)})
98
- return
89
+ return res.Emit(&AddPinOutput{Pins: cidsToStrings(added)})
90
}
91
92
out := make(chan interface{})
102
- res.SetOutput((<-chan interface{})(out))
93
+ res.Emit(out)
94
+
95
v := new(dag.ProgressTracker)
104
- ctx := v.DeriveContext(req.Context())
96
+ ctx := v.DeriveContext(req.Context)
97
98
type pinResult struct {
99
pins []cid.Cid
@@ -109,7 +101,7 @@ var addPinCmd = &cmds.Command{
101
}
102
ch := make(chan pinResult, 1)
103
go func() {
112
- added, err := corerepo.Pin(n, api, ctx, req.Arguments(), recursive)
104
+ added, err := corerepo.Pin(n, api, ctx, req.Arguments, recursive)
105
ch <- pinResult{pins: added, err: err}
106
}()
107
@@ -120,63 +112,49 @@ var addPinCmd = &cmds.Command{
112
select {
113
case val := <-ch:
114
if val.err != nil {
123
- res.SetError(val.err, cmdkit.ErrNormal)
124
- return
115
+ return val.err
116
}
117
118
if pv := v.Value(); pv != 0 {
119
out <- &AddPinOutput{Progress: v.Value()}
120
}
121
out <- &AddPinOutput{Pins: cidsToStrings(val.pins)}
131
- return
122
+ return nil
123
case <-ticker.C:
124
out <- &AddPinOutput{Progress: v.Value()}
125
case <-ctx.Done():
126
log.Error(ctx.Err())
136
- res.SetError(ctx.Err(), cmdkit.ErrNormal)
137
- return
127
+ return ctx.Err()
128
}
129
}
140
- },
141
- Marshalers: cmds.MarshalerMap{
142
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
143
- v, err := unwrapOutput(res.Output())
144
- if err != nil {
145
- return nil, err
146
- }
130
131
+ return nil
132
+ },
133
+ Encoders: cmds.EncoderMap{
134
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *AddPinOutput) error {
135
var added []string
136
150
- switch out := v.(type) {
151
- case *AddPinOutput:
152
- if out.Pins != nil {
153
- added = out.Pins
154
- } else {
155
- // this can only happen if the progress option is set
156
- fmt.Fprintf(res.Stderr(), "Fetched/Processed %d nodes\r", out.Progress)
157
- }
158
-
159
- if res.Error() != nil {
160
- return nil, res.Error()
161
- }
162
- default:
163
- return nil, e.TypeErr(out, v)
137
+ if out.Pins != nil {
138
+ added = out.Pins
139
+ } else {
140
+ // this can only happen if the progress option is set
141
+ return fmt.Errorf("Fetched/Processed %d nodes\r", out.Progress)
142
}
143
144
var pintype string
167
- rec, found, _ := res.Request().Option("recursive").Bool()
145
+ rec, found := req.Options["recursive"].(bool)
146
if rec || !found {
147
pintype = "recursively"
148
} else {
149
pintype = "directly"
150
}
151
174
- buf := new(bytes.Buffer)
152
for _, k := range added {
176
- fmt.Fprintf(buf, "pinned %s %s\n", k, pintype)
153
+ fmt.Fprintf(w, "pinned %s %s\n", k, pintype)
154
}
178
- return buf, nil
179
- },
155
+
156
+ return nil
157
+ }),
158
},
159
}
160
@@ -196,52 +174,34 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
174
cmdkit.BoolOption(pinRecursiveOptionName, "r", "Recursively unpin the object linked to by the specified object(s).").WithDefault(true),
175
},
176
Type: PinOutput{},
199
- Run: func(req cmds.Request, res cmds.Response) {
200
- n, err := req.InvocContext().GetNode()
177
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
178
+ n, err := cmdenv.GetNode(env)
179
if err != nil {
202
- res.SetError(err, cmdkit.ErrNormal)
203
- return
180
+ return err
181
}
182
206
- api, err := req.InvocContext().GetApi()
183
+ api, err := cmdenv.GetApi(env)
184
if err != nil {
208
- res.SetError(err, cmdkit.ErrNormal)
209
- return
185
+ return err
186
}
187
188
// set recursive flag
213
- recursive, _, err := req.Option(pinRecursiveOptionName).Bool()
214
- if err != nil {
215
- res.SetError(err, cmdkit.ErrNormal)
216
- return
217
- }
189
+ recursive, _ := req.Options[pinRecursiveOptionName].(bool)
190
219
- removed, err := corerepo.Unpin(n, api, req.Context(), req.Arguments(), recursive)
191
+ removed, err := corerepo.Unpin(n, api, req.Context, req.Arguments, recursive)
192
if err != nil {
221
- res.SetError(err, cmdkit.ErrNormal)
222
- return
193
+ return err
194
}
195
225
- res.SetOutput(&PinOutput{cidsToStrings(removed)})
196
+ return res.Emit(&PinOutput{cidsToStrings(removed)})
197
},
227
- Marshalers: cmds.MarshalerMap{
228
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
229
- v, err := unwrapOutput(res.Output())
230
- if err != nil {
231
- return nil, err
232
- }
233
-
234
- added, ok := v.(*PinOutput)
235
- if !ok {
236
- return nil, e.TypeErr(added, v)
237
- }
238
-
239
- buf := new(bytes.Buffer)
240
- for _, k := range added.Pins {
241
- fmt.Fprintf(buf, "unpinned %s\n", k)
198
+ Encoders: cmds.EncoderMap{
199
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
200
+ for _, k := range out.Pins {
201
+ fmt.Fprintf(w, "unpinned %s\n", k)
202
}
243
- return buf, nil
244
- },
203
+ return nil
204
+ }),
205
},
206
}
207
@@ -301,74 +261,58 @@ Example:
261
cmdkit.StringOption(pinTypeOptionName, "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\".").WithDefault("all"),
262
cmdkit.BoolOption(pinQuietOptionName, "q", "Write just hashes of objects."),
263
},
304
- Run: func(req cmds.Request, res cmds.Response) {
305
- n, err := req.InvocContext().GetNode()
264
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
265
+ n, err := cmdenv.GetNode(env)
266
if err != nil {
307
- res.SetError(err, cmdkit.ErrNormal)
308
- return
267
+ return err
268
}
269
311
- api, err := req.InvocContext().GetApi()
270
+ api, err := cmdenv.GetApi(env)
271
if err != nil {
313
- res.SetError(err, cmdkit.ErrNormal)
314
- return
272
+ return err
273
}
274
317
- typeStr, _, err := req.Option(pinTypeOptionName).String()
275
+ typeStr, _ := req.Options[pinTypeOptionName].(string)
276
if err != nil {
319
- res.SetError(err, cmdkit.ErrNormal)
320
- return
277
+ return err
278
}
279
280
switch typeStr {
281
case "all", "direct", "indirect", "recursive":
282
default:
283
err = fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
327
- res.SetError(err, cmdkit.ErrClient)
328
- return
284
+ return err
285
}
286
287
var keys map[string]RefKeyObject
288
333
- if len(req.Arguments()) > 0 {
334
- keys, err = pinLsKeys(req.Context(), req.Arguments(), typeStr, n, api)
289
+ if len(req.Arguments) > 0 {
290
+ keys, err = pinLsKeys(req.Context, req.Arguments, typeStr, n, api)
291
} else {
336
- keys, err = pinLsAll(req.Context(), typeStr, n)
292
+ keys, err = pinLsAll(req.Context, typeStr, n)
293
}
294
295
if err != nil {
340
- res.SetError(err, cmdkit.ErrNormal)
296
+ return err
297
} else {
342
- res.SetOutput(&RefKeyList{Keys: keys})
298
+ return res.Emit(&RefKeyList{Keys: keys})
299
}
300
},
301
Type: RefKeyList{},
346
- Marshalers: cmds.MarshalerMap{
347
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
348
- v, err := unwrapOutput(res.Output())
349
- if err != nil {
350
- return nil, err
351
- }
302
+ Encoders: cmds.EncoderMap{
303
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RefKeyList) error {
304
+ quiet, _ := req.Options[pinQuietOptionName].(bool)
305
353
- quiet, _, err := res.Request().Option(pinQuietOptionName).Bool()
354
- if err != nil {
355
- return nil, err
356
- }
357
-
358
- keys, ok := v.(*RefKeyList)
359
- if !ok {
360
- return nil, e.TypeErr(keys, v)
361
- }
362
- out := new(bytes.Buffer)
363
- for k, v := range keys.Keys {
306
+ for k, v := range out.Keys {
307
if quiet {
365
- fmt.Fprintf(out, "%s\n", k)
308
+ fmt.Fprintf(w, "%s\n", k)
309
} else {
367
- fmt.Fprintf(out, "%s %s\n", k, v.Type)
310
+ fmt.Fprintf(w, "%s %s\n", k, v.Type)
311
}
312
}
370
- return out, nil
371
- },
313
+
314
+ return nil
315
+ }),
316
},
317
}
318
@@ -394,54 +338,36 @@ new pin and removing the old one.
338
cmdkit.BoolOption(pinUnpinOptionName, "Remove the old pin.").WithDefault(true),
339
},
340
Type: PinOutput{},
397
- Run: func(req cmds.Request, res cmds.Response) {
398
- api, err := req.InvocContext().GetApi()
341
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
342
+ api, err := cmdenv.GetApi(env)
343
if err != nil {
400
- res.SetError(err, cmdkit.ErrNormal)
401
- return
344
+ return err
345
}
346
404
- unpin, _, err := req.Option(pinUnpinOptionName).Bool()
405
- if err != nil {
406
- res.SetError(err, cmdkit.ErrNormal)
407
- return
408
- }
347
+ unpin, _ := req.Options[pinUnpinOptionName].(bool)
348
410
- from, err := iface.ParsePath(req.Arguments()[0])
349
+ from, err := iface.ParsePath(req.Arguments[0])
350
if err != nil {
412
- res.SetError(err, cmdkit.ErrNormal)
413
- return
351
+ return err
352
}
353
416
- to, err := iface.ParsePath(req.Arguments()[1])
354
+ to, err := iface.ParsePath(req.Arguments[1])
355
if err != nil {
418
- res.SetError(err, cmdkit.ErrNormal)
419
- return
356
+ return err
357
}
358
422
- err = api.Pin().Update(req.Context(), from, to, options.Pin.Unpin(unpin))
359
+ err = api.Pin().Update(req.Context, from, to, options.Pin.Unpin(unpin))
360
if err != nil {
424
- res.SetError(err, cmdkit.ErrNormal)
425
- return
361
+ return err
362
}
363
428
- res.SetOutput(&PinOutput{Pins: []string{from.String(), to.String()}})
364
+ return res.Emit(&PinOutput{Pins: []string{from.String(), to.String()}})
365
},
430
- Marshalers: cmds.MarshalerMap{
431
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
432
- v, err := unwrapOutput(res.Output())
433
- if err != nil {
434
- return nil, err
435
- }
436
- added, ok := v.(*PinOutput)
437
- if !ok {
438
- return nil, e.TypeErr(added, v)
439
- }
440
-
441
- buf := new(bytes.Buffer)
442
- fmt.Fprintf(buf, "updated %s to %s\n", added.Pins[0], added.Pins[1])
443
- return buf, nil
444
- },
366
+ Encoders: cmds.EncoderMap{
367
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
368
+ fmt.Fprintf(w, "updated %s to %s\n", out.Pins[0], out.Pins[1])
369
+ return nil
370
+ }),
371
},
372
}
373
@@ -457,51 +383,40 @@ var verifyPinCmd = &cmds.Command{
383
cmdkit.BoolOption(pinVerboseOptionName, "Also write the hashes of non-broken pins."),
384
cmdkit.BoolOption(pinQuietOptionName, "q", "Write just hashes of broken pins."),
385
},
460
- Run: func(req cmds.Request, res cmds.Response) {
461
- n, err := req.InvocContext().GetNode()
386
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
387
+ n, err := cmdenv.GetNode(env)
388
if err != nil {
463
- res.SetError(err, cmdkit.ErrNormal)
464
- return
389
+ return err
390
}
391
467
- verbose, _, _ := res.Request().Option(pinVerboseOptionName).Bool()
468
- quiet, _, _ := res.Request().Option(pinQuietOptionName).Bool()
392
+ verbose, _ := req.Options[pinVerboseOptionName].(bool)
393
+ quiet, _ := req.Options[pinQuietOptionName].(bool)
394
395
if verbose && quiet {
471
- res.SetError(fmt.Errorf("the --verbose and --quiet options can not be used at the same time"), cmdkit.ErrNormal)
396
+ return fmt.Errorf("the --verbose and --quiet options can not be used at the same time")
397
}
398
399
opts := pinVerifyOpts{
400
explain: !quiet,
401
includeOk: verbose,
402
}
478
- out := pinVerify(req.Context(), n, opts)
403
+ out := pinVerify(req.Context, n, opts)
404
480
- res.SetOutput(out)
405
+ return res.Emit(out)
406
},
407
Type: PinVerifyRes{},
483
- Marshalers: cmds.MarshalerMap{
484
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
485
- quiet, _, _ := res.Request().Option(pinQuietOptionName).Bool()
486
-
487
- out, err := unwrapOutput(res.Output())
488
- if err != nil {
489
- return nil, err
490
- }
491
- r, ok := out.(*PinVerifyRes)
492
- if !ok {
493
- return nil, e.TypeErr(r, out)
494
- }
408
+ Encoders: cmds.EncoderMap{
409
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinVerifyRes) error {
410
+ quiet, _ := req.Options[pinQuietOptionName].(bool)
411
496
- buf := &bytes.Buffer{}
497
- if quiet && !r.Ok {
498
- fmt.Fprintf(buf, "%s\n", r.Cid)
412
+ if quiet && !out.Ok {
413
+ fmt.Fprintf(w, "%s\n", out.Cid)
414
} else if !quiet {
500
- r.Format(buf)
415
+ out.Format(w)
416
}
417
503
- return buf, nil
504
- },
418
+ return nil
419
+ }),
420
},
421
}
422
core/commands/root.go
+1
-1
@@ -135,7 +135,7 @@ var rootSubcommands = map[string]*cmds.Command{
135
"mount": MountCmd,
136
"name": name.NameCmd,
137
"object": ocmd.ObjectCmd,
138
- "pin": lgc.NewCommand(PinCmd),
138
+ "pin": PinCmd,
139
"ping": PingCmd,
140
"p2p": P2PCmd,
141
"refs": lgc.NewCommand(RefsCmd),