From 008de00fcbbd11343ef8d0ea39490f5747b7eae5 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Sun, 16 Oct 2016 01:44:09 -0400 Subject: [PATCH] Minor refactor of "block rm" command. License: MIT Signed-off-by: Kevin Atkinson --- core/commands/block.go | 60 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/core/commands/block.go b/core/commands/block.go index 4fa7a238e8f..12e40aa95ff 100644 --- a/core/commands/block.go +++ b/core/commands/block.go @@ -240,34 +240,7 @@ It takes a list of base58 encoded multihashs to remove. cmds.BoolOption("quiet", "q", "Write minimal output.").Default(false), }, Run: func(req cmds.Request, res cmds.Response) { - n, err := req.InvocContext().GetNode() - if err != nil { - res.SetError(err, cmds.ErrNormal) - return - } - hashes := req.Arguments() - force, _, _ := req.Option("force").Bool() - quiet, _, _ := req.Option("quiet").Bool() - cids := make([]*cid.Cid, 0, len(hashes)) - for _, hash := range hashes { - c, err := cid.Decode(hash) - if err != nil { - res.SetError(fmt.Errorf("invalid content id: %s (%s)", hash, err), cmds.ErrNormal) - return - } - - cids = append(cids, c) - } - outChan := make(chan interface{}) - err = util.RmBlocks(n.Blockstore, n.Pinning, outChan, cids, util.RmBlocksOpts{ - Quiet: quiet, - Force: force, - }) - if err != nil { - res.SetError(err, cmds.ErrNormal) - return - } - res.SetOutput((<-chan interface{})(outChan)) + blockRmRun(req, res, "") }, PostRun: func(req cmds.Request, res cmds.Response) { if res.Error() != nil { @@ -288,6 +261,37 @@ It takes a list of base58 encoded multihashs to remove. Type: util.RemovedBlock{}, } +func blockRmRun(req cmds.Request, res cmds.Response, prefix string) { + n, err := req.InvocContext().GetNode() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + hashes := req.Arguments() + force, _, _ := req.Option("force").Bool() + quiet, _, _ := req.Option("quiet").Bool() + cids := make([]*cid.Cid, 0, len(hashes)) + for _, hash := range hashes { + c, err := cid.Decode(hash) + if err != nil { + res.SetError(fmt.Errorf("invalid content id: %s (%s)", hash, err), cmds.ErrNormal) + return + } + cids = append(cids, c) + } + outChan := make(chan interface{}) + err = util.RmBlocks(n.Blockstore, n.Pinning, outChan, cids, util.RmBlocksOpts{ + Quiet: quiet, + Force: force, + Prefix: prefix, + }) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + res.SetOutput((<-chan interface{})(outChan)) +} + type BlockLocateRes struct { Key string Res []bs.LocateInfo