From 1be4ba9828317157286a5fb66b3be74e2355e2e6 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 b7ab54922a3..486c294312a 100644 --- a/core/commands/block.go +++ b/core/commands/block.go @@ -204,34 +204,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 { @@ -252,6 +225,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