Skip to content

Commit

Permalink
refactor(commands2/cat) readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Tiger Chow committed Nov 4, 2014
1 parent 4ec0f40 commit 8eaa405
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions core/commands2/cat.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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"
)

Expand All @@ -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)
return
}

read, err := uio.NewDagReader(dagnode, node.DAG)
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)
}

readers = append(readers, read)
readers, err := cat(node, paths)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

reader := io.MultiReader(readers...)
res.SetOutput(reader)
},
}

func cat(node *core.IpfsNode, paths []string) ([]io.Reader, error) {

This comment has been minimized.

Copy link
@btc

btc Nov 4, 2014

Contributor

@jbenet @mappum

I know this is a bit more verbose than the previous version, but for large commands, I think it would be wise for us to break apart the unmarshalling in the framework from the core operation.

In exchange for the extra keystrokes, the clear demarcation makes the code more readable and maintainable in the long run. Much easier to refactor the commands package without having to futz with the abstract logic of the operation.

This comment has been minimized.

Copy link
@jbenet

jbenet Nov 4, 2014

Member

very much 👍

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
}

0 comments on commit 8eaa405

Please sign in to comment.