Skip to content

Commit

Permalink
Minor refactor of "block rm" command.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <[email protected]>
  • Loading branch information
kevina committed Oct 16, 2016
1 parent 5f25198 commit 1be4ba9
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions core/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 1be4ba9

Please sign in to comment.