Skip to content

Commit

Permalink
commands: switch object commands to CoreAPI
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Jul 30, 2018
1 parent 9315e67 commit 3cf7b34
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 358 deletions.
11 changes: 11 additions & 0 deletions core/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/repo/config"
)

Expand All @@ -18,6 +19,16 @@ func GetNode(env interface{}) (*core.IpfsNode, error) {
return ctx.GetNode()
}

// GetApi extracts CoreAPI instance from the environment.
func GetApi(env interface{}) (coreiface.CoreAPI, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}

return ctx.GetApi()
}

// GetConfig extracts the config from the environment.
func GetConfig(env interface{}) (*config.Config, error) {
ctx, ok := env.(*commands.Context)
Expand Down
40 changes: 19 additions & 21 deletions core/commands/object/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"io"

cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/dagutils"
path "gx/ipfs/QmYKNMEUK7nCVAefgXF1LVtZEZg3uRmBqiae4FJRXDNAyJ/go-path"

cmdkit "gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
)

Expand Down Expand Up @@ -52,7 +52,7 @@ Example:
cmdkit.BoolOption("verbose", "v", "Print extra information."),
},
Run: func(req cmds.Request, res cmds.Response) {
node, err := req.InvocContext().GetNode()
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -61,39 +61,37 @@ Example:
a := req.Arguments()[0]
b := req.Arguments()[1]

pa, err := path.ParsePath(a)
pa, err := coreiface.ParsePath(a)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

pb, err := path.ParsePath(b)
pb, err := coreiface.ParsePath(b)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

ctx := req.Context()
changes, err := api.Object().Diff(req.Context(), pa, pb)

obj_a, err := core.Resolve(ctx, node.Namesys, node.Resolver, pa)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
out := make([]*dagutils.Change, len(changes))
for i, change := range changes {
out[i] = &dagutils.Change{
Type: change.Type,
Path: change.Path,
}

obj_b, err := core.Resolve(ctx, node.Namesys, node.Resolver, pb)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
if change.Before != nil {
out[i].Before = change.Before.Cid()
}

changes, err := dagutils.Diff(ctx, node.DAG, obj_a, obj_b)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
if change.After != nil {
out[i].After = change.After.Cid()
}
}

res.SetOutput(&Changes{changes})
res.SetOutput(&Changes{out})
},
Type: Changes{},
Marshalers: cmds.MarshalerMap{
Expand Down
Loading

0 comments on commit 3cf7b34

Please sign in to comment.