提交 36c6fb35 作者: Łukasz Magiera

commands: switch object commands to CoreAPI

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 432ab621
......@@ -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"
)
......@@ -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)
......
......@@ -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"
)
......@@ -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
......@@ -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{
......
......@@ -3,25 +3,18 @@ package objectcmd
import (
"fmt"
"io"
"io/ioutil"
"strings"
oldcmds "github.com/ipfs/go-ipfs/commands"
lgc "github.com/ipfs/go-ipfs/commands/legacy"
core "github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
"github.com/ipfs/go-ipfs/dagutils"
dag "gx/ipfs/QmRy4Qk9hbgFX9NGJRm8rBThrA8PZhNCitMgeRYyZ67s59/go-merkledag"
ft "gx/ipfs/QmSaz8Qg77gGqvDvLKeSAY7ivDEnramSWF6T7TcRwFpHtP/go-unixfs"
path "gx/ipfs/QmYKNMEUK7nCVAefgXF1LVtZEZg3uRmBqiae4FJRXDNAyJ/go-path"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
cmds "gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
logging "gx/ipfs/QmcVVHfdyv15GVPk7NrxdWjh2hLVccXnoD8j2tyQShiXJb/go-log"
cmdkit "gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
)
var log = logging.Logger("core/commands/object")
var ObjectPatchCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Create a new merkledag object based on an existing one.",
......@@ -74,51 +67,31 @@ the limit will not be respected by the network.
cmdkit.FileArg("data", true, false, "Data to append.").EnableStdin(),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) {
nd, err := GetNode(env)
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
}
root, err := path.ParsePath(req.Arguments[0])
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
}
rootnd, err := core.Resolve(req.Context, nd.Namesys, nd.Resolver, root)
api, err := GetApi(env)
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
}
rtpb, ok := rootnd.(*dag.ProtoNode)
if !ok {
re.SetError(dag.ErrNotProtobuf, cmdkit.ErrNormal)
return
}
fi, err := req.Files.NextFile()
root, err := coreiface.ParsePath(req.Arguments[0])
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
}
data, err := ioutil.ReadAll(fi)
data, err := req.Files.NextFile()
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
}
rtpb.SetData(append(rtpb.Data(), data...))
err = nd.DAG.Add(req.Context, rtpb)
p, err := api.Object().AppendData(req.Context, root, data)
if err != nil {
re.SetError(err, cmdkit.ErrNormal)
return
}
cmds.EmitOnce(re, &Object{Hash: rtpb.Cid().String()})
cmds.EmitOnce(re, &Object{Hash: p.Cid().String()})
},
Type: Object{},
Encoders: cmds.EncoderMap{
......@@ -145,51 +118,27 @@ Example:
cmdkit.FileArg("data", true, false, "The data to set the object to.").EnableStdin(),
},
Run: func(req oldcmds.Request, res oldcmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
api, err := req.InvocContext().GetApi()
rp, err := path.ParsePath(req.StringArguments()[0])
root, err := coreiface.ParsePath(req.StringArguments()[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, rp)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
rtpb, ok := root.(*dag.ProtoNode)
if !ok {
res.SetError(dag.ErrNotProtobuf, cmdkit.ErrNormal)
return
}
fi, err := req.Files().NextFile()
data, err := req.Files().NextFile()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
data, err := ioutil.ReadAll(fi)
p, err := api.Object().SetData(req.Context(), root, data)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
rtpb.SetData(data)
err = nd.DAG.Add(req.Context(), rtpb)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
res.SetOutput(&Object{Hash: rtpb.Cid().String()})
res.SetOutput(&Object{Hash: p.Cid().String()})
},
Type: Object{},
Marshalers: oldcmds.MarshalerMap{
......@@ -209,49 +158,26 @@ Removes a link by the given name from root.
cmdkit.StringArg("link", true, false, "Name of the link to remove."),
},
Run: func(req oldcmds.Request, res oldcmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
rootp, err := path.ParsePath(req.Arguments()[0])
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, rootp)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
rtpb, ok := root.(*dag.ProtoNode)
if !ok {
res.SetError(dag.ErrNotProtobuf, cmdkit.ErrNormal)
return
}
path := req.Arguments()[1]
e := dagutils.NewDagEditor(rtpb, nd.DAG)
err = e.RmLink(req.Context(), path)
root, err := coreiface.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
nnode, err := e.Finalize(req.Context(), nd.DAG)
link := req.Arguments()[1]
p, err := api.Object().RmLink(req.Context(), root, link)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
nc := nnode.Cid()
res.SetOutput(&Object{Hash: nc.String()})
res.SetOutput(&Object{Hash: p.Cid().String()})
},
Type: Object{},
Marshalers: oldcmds.MarshalerMap{
......@@ -284,32 +210,21 @@ to a file containing 'bar', and returns the hash of the new object.
cmdkit.BoolOption("create", "p", "Create intermediary nodes."),
},
Run: func(req oldcmds.Request, res oldcmds.Response) {
nd, err := req.InvocContext().GetNode()
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
rootp, err := path.ParsePath(req.Arguments()[0])
root, err := coreiface.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, rootp)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
name := req.Arguments()[1]
rtpb, ok := root.(*dag.ProtoNode)
if !ok {
res.SetError(dag.ErrNotProtobuf, cmdkit.ErrNormal)
return
}
npath := req.Arguments()[1]
childp, err := path.ParsePath(req.Arguments()[2])
child, err := coreiface.ParsePath(req.Arguments()[2])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
......@@ -321,34 +236,14 @@ to a file containing 'bar', and returns the hash of the new object.
return
}
var createfunc func() *dag.ProtoNode
if create {
createfunc = ft.EmptyDirNode
}
e := dagutils.NewDagEditor(rtpb, nd.DAG)
childnd, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, childp)
p, err := api.Object().AddLink(req.Context(), root, name, child,
options.Object.Create(create))
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
err = e.InsertNodeAtPath(req.Context(), npath, childnd, createfunc)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
nnode, err := e.Finalize(req.Context(), nd.DAG)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
nc := nnode.Cid()
res.SetOutput(&Object{Hash: nc.String()})
res.SetOutput(&Object{Hash: p.Cid().String()})
},
Type: Object{},
Marshalers: oldcmds.MarshalerMap{
......@@ -356,13 +251,14 @@ to a file containing 'bar', and returns the hash of the new object.
},
}
// TODO: fix import loop with core/commands so we don't need that
// COPIED FROM ONE LEVEL UP
// GetNode extracts the node from the environment.
func GetNode(env interface{}) (*core.IpfsNode, error) {
// GetApi extracts CoreAPI instance from the environment.
func GetApi(env interface{}) (coreiface.CoreAPI, error) {
ctx, ok := env.(*oldcmds.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}
return ctx.GetNode()
return ctx.GetApi()
}
......@@ -31,7 +31,6 @@ type ObjectStat struct {
CumulativeSize int
}
const (
// DiffAdd is a Type of ObjectChange where a link was added to the graph
DiffAdd = iota
......@@ -57,11 +56,11 @@ type ObjectChange struct {
// Before holds the link path before the change. Note that when a link is
// added, this will be nil.
Before Path
Before ResolvedPath
// After holds the link path after the change. Note that when a link is
// removed, this will be nil.
After Path
After ResolvedPath
}
// ObjectAPI specifies the interface to MerkleDAG and contains useful utilities
......
......@@ -105,9 +105,9 @@ func (objectOpts) DataType(t string) ObjectPutOption {
}
}
// WithPin is an option for Object.Put which specifies whether to pin the added
// Pin is an option for Object.Put which specifies whether to pin the added
// objects, default is false
func (objectOpts) WithPin(pin bool) ObjectPutOption {
func (objectOpts) Pin(pin bool) ObjectPutOption {
return func(settings *ObjectPutSettings) error {
settings.Pin = pin
return nil
......
......@@ -316,10 +316,16 @@ func (api *ObjectAPI) Diff(ctx context.Context, before coreiface.Path, after cor
out := make([]coreiface.ObjectChange, len(changes))
for i, change := range changes {
out[i] = coreiface.ObjectChange{
Type: change.Type,
Path: change.Path,
Before: coreiface.IpfsPath(change.Before),
After: coreiface.IpfsPath(change.After),
Type: change.Type,
Path: change.Path,
}
if change.Before != nil {
out[i].Before = coreiface.IpfsPath(change.Before)
}
if change.After != nil {
out[i].After = coreiface.IpfsPath(change.After)
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论