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 Feb 4, 2018
1 parent 1810cdc commit afb490b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 346 deletions.
39 changes: 18 additions & 21 deletions core/commands/object/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ 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"

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

Expand Down Expand Up @@ -52,7 +51,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 +60,37 @@ Example:
a := req.Arguments()[0]
b := req.Arguments()[1]

pa, err := path.ParsePath(a)
pa, err := api.ParsePath(req.Context(), a, api.WithResolve(true))
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

pb, err := path.ParsePath(b)
pb, err := api.ParsePath(req.Context(), b, api.WithResolve(true))
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 afb490b

Please sign in to comment.