refact(cmd/repo): repo verify uses new cmd lib
License: MIT Signed-off-by: chenminjian <727180553@qq.com>
chenminjian committed
Oct 28, 2018 at 11:46 UTC
5afa7ebfdcd425fb9e50d5f34605b2e984d98aaa
1 file changed
+24
-51
core/commands/repo.go
+24
-51
@@ -1,7 +1,6 @@
1
package commands
2
3
import (
4
- "bytes"
4
"context"
5
"errors"
6
"fmt"
@@ -13,8 +12,6 @@ import (
12
"sync"
13
"text/tabwriter"
14
16
- oldcmds "github.com/ipfs/go-ipfs/commands"
17
- lgc "github.com/ipfs/go-ipfs/commands/legacy"
15
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
16
e "github.com/ipfs/go-ipfs/core/commands/e"
17
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
@@ -44,7 +41,7 @@ var RepoCmd = &cmds.Command{
41
"gc": repoGcCmd,
42
"fsck": RepoFsckCmd,
43
"version": repoVersionCmd,
47
- "verify": lgc.NewCommand(repoVerifyCmd),
44
+ "verify": repoVerifyCmd,
45
},
46
}
47
@@ -323,91 +320,67 @@ func verifyResultChan(ctx context.Context, keys <-chan cid.Cid, bs bstore.Blocks
320
return results
321
}
322
326
-var repoVerifyCmd = &oldcmds.Command{
323
+var repoVerifyCmd = &cmds.Command{
324
Helptext: cmdkit.HelpText{
325
Tagline: "Verify all blocks in repo are not corrupted.",
326
},
330
- Run: func(req oldcmds.Request, res oldcmds.Response) {
331
- nd, err := req.InvocContext().GetNode()
327
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
328
+ nd, err := cmdenv.GetNode(env)
329
if err != nil {
333
- res.SetError(err, cmdkit.ErrNormal)
334
- return
330
+ return err
331
}
332
337
- out := make(chan interface{})
338
- res.SetOutput((<-chan interface{})(out))
339
- defer close(out)
340
-
333
bs := bstore.NewBlockstore(nd.Repo.Datastore())
334
bs.HashOnRead(true)
335
344
- keys, err := bs.AllKeysChan(req.Context())
336
+ keys, err := bs.AllKeysChan(req.Context)
337
if err != nil {
338
log.Error(err)
347
- return
339
+ return err
340
}
341
350
- results := verifyResultChan(req.Context(), keys, bs)
342
+ results := verifyResultChan(req.Context, keys, bs)
343
344
var fails int
345
var i int
346
for msg := range results {
347
if msg != "" {
356
- select {
357
- case out <- &VerifyProgress{Msg: msg}:
358
- case <-req.Context().Done():
359
- return
348
+ if err := res.Emit(&VerifyProgress{Msg: msg}); err != nil {
349
+ return err
350
}
351
fails++
352
}
353
i++
364
- select {
365
- case out <- &VerifyProgress{Progress: i}:
366
- case <-req.Context().Done():
367
- return
354
+ if err := res.Emit(&VerifyProgress{Progress: i}); err != nil {
355
+ return err
356
}
357
}
358
371
- if fails == 0 {
372
- select {
373
- case out <- &VerifyProgress{Msg: "verify complete, all blocks validated."}:
374
- case <-req.Context().Done():
375
- return
376
- }
377
- } else {
378
- res.SetError(fmt.Errorf("verify complete, some blocks were corrupt"), cmdkit.ErrNormal)
359
+ if fails != 0 {
360
+ return errors.New("verify complete, some blocks were corrupt")
361
}
362
+
363
+ return res.Emit(&VerifyProgress{Msg: "verify complete, all blocks validated."})
364
},
365
Type: &VerifyProgress{},
382
- Marshalers: oldcmds.MarshalerMap{
383
- oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
384
- v, err := unwrapOutput(res.Output())
385
- if err != nil {
386
- return nil, err
387
- }
388
-
389
- obj, ok := v.(*VerifyProgress)
390
- if !ok {
391
- return nil, e.TypeErr(obj, v)
392
- }
393
-
394
- buf := new(bytes.Buffer)
366
+ Encoders: cmds.EncoderMap{
367
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, obj *VerifyProgress) error {
368
if strings.Contains(obj.Msg, "was corrupt") {
369
fmt.Fprintln(os.Stdout, obj.Msg)
397
- return buf, nil
370
+ return nil
371
}
372
373
if obj.Msg != "" {
374
if len(obj.Msg) < 20 {
375
obj.Msg += " "
376
}
404
- fmt.Fprintln(buf, obj.Msg)
405
- return buf, nil
377
+ fmt.Fprintln(w, obj.Msg)
378
+ return nil
379
}
380
408
- fmt.Fprintf(buf, "%d blocks processed.\r", obj.Progress)
409
- return buf, nil
410
- },
381
+ fmt.Fprintf(w, "%d blocks processed.\r", obj.Progress)
382
+ return nil
383
+ }),
384
},
385
}
386