Commit 2a15fe8e authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

refactor(commands2/cat) readability

parent c67e3771
package commands
import (
"errors"
"io"
cmds "github.com/jbenet/go-ipfs/commands"
core "github.com/jbenet/go-ipfs/core"
uio "github.com/jbenet/go-ipfs/unixfs/io"
)
......@@ -14,26 +16,41 @@ var catCmd = &cmds.Command{
Help: "TODO",
Run: func(res cmds.Response, req cmds.Request) {
node := req.Context().Node
paths := make([]string, 0, len(req.Arguments()))
readers := make([]io.Reader, 0, len(req.Arguments()))
for _, arg := range req.Arguments() {
path := arg.(string)
dagnode, err := node.Resolver.ResolvePath(path)
if err != nil {
res.SetError(err, cmds.ErrNormal)
path, ok := arg.(string)
if !ok {
res.SetError(errors.New("cast error"), cmds.ErrNormal)
return
}
paths = append(paths, path)
}
read, err := uio.NewDagReader(dagnode, node.DAG)
readers, err := cat(node, paths)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
readers = append(readers, read)
}
reader := io.MultiReader(readers...)
res.SetOutput(reader)
},
}
func cat(node *core.IpfsNode, paths []string) ([]io.Reader, error) {
readers := make([]io.Reader, 0, len(paths))
for _, path := range paths {
dagnode, err := node.Resolver.ResolvePath(path)
if err != nil {
return nil, err
}
read, err := uio.NewDagReader(dagnode, node.DAG)
if err != nil {
return nil, err
}
readers = append(readers, read)
}
return readers, nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment