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