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 <[email protected]>
  • Loading branch information
magik6k committed Jul 19, 2018
1 parent a12eb4e commit f271b09
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 356 deletions.
11 changes: 11 additions & 0 deletions core/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo/config"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
)

// GetNode extracts the node from the environment.
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"
dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
path "github.com/ipfs/go-ipfs/path"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"

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 f271b09

Please sign in to comment.