Skip to content

Commit

Permalink
Cleanup instances of manual resolver construction
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 Oct 2, 2018
1 parent e93aa25 commit 1ed555b
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 190 deletions.
21 changes: 15 additions & 6 deletions core/commands/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"io"
"os"

core "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
"github.com/ipfs/go-ipfs/core/coreapi/interface"

"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
Expand All @@ -35,6 +34,11 @@ var CatCmd = &cmds.Command{
return err
}

api, err := cmdenv.GetApi(env)
if err != nil {
return err
}

if !node.OnlineMode() {
if err := node.SetupOfflineRouting(); err != nil {
return err
Expand Down Expand Up @@ -62,7 +66,7 @@ var CatCmd = &cmds.Command{
return err
}

readers, length, err := cat(req.Context, node, req.Arguments, int64(offset), int64(max))
readers, length, err := cat(req.Context, api, req.Arguments, int64(offset), int64(max))
if err != nil {
return err
}
Expand Down Expand Up @@ -115,14 +119,19 @@ var CatCmd = &cmds.Command{
},
}

func cat(ctx context.Context, node *core.IpfsNode, paths []string, offset int64, max int64) ([]io.Reader, uint64, error) {
func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, max int64) ([]io.Reader, uint64, error) {
readers := make([]io.Reader, 0, len(paths))
length := uint64(0)
if max == 0 {
return nil, 0, nil
}
for _, fpath := range paths {
read, err := coreunix.Cat(ctx, node, fpath)
for _, p := range paths {
fpath, err := iface.ParsePath(p)
if err != nil {
return nil, 0, err
}

read, err := api.Unixfs().Cat(ctx, fpath)
if err != nil {
return nil, 0, err
}
Expand Down
36 changes: 20 additions & 16 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@ import (
core "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
e "github.com/ipfs/go-ipfs/core/commands/e"
ft "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
bservice "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
"github.com/ipfs/go-ipfs/core/coreapi/interface"

humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
ft "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
bservice "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
)

Expand Down Expand Up @@ -120,6 +118,11 @@ var filesStatCmd = &cmds.Command{
return err
}

api, err := cmdenv.GetApi(env)
if err != nil {
return err
}

path, err := checkPath(req.Arguments[0])
if err != nil {
return err
Expand All @@ -138,7 +141,7 @@ var filesStatCmd = &cmds.Command{
dagserv = node.DAG
}

nd, err := getNodeFromPath(req.Context, node, dagserv, path)
nd, err := getNodeFromPath(req.Context, node, api, path)
if err != nil {
return err
}
Expand Down Expand Up @@ -305,6 +308,12 @@ var filesCpCmd = &oldcmds.Command{
return
}

api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

flush, _, _ := req.Option("flush").Bool()

src, err := checkPath(req.Arguments()[0])
Expand All @@ -324,7 +333,7 @@ var filesCpCmd = &oldcmds.Command{
dst += gopath.Base(src)
}

nd, err := getNodeFromPath(req.Context(), node, node.DAG, src)
nd, err := getNodeFromPath(req.Context(), node, api, src)
if err != nil {
res.SetError(fmt.Errorf("cp: cannot get node from path %s: %s", src, err), cmdkit.ErrNormal)
return
Expand All @@ -348,20 +357,15 @@ var filesCpCmd = &oldcmds.Command{
},
}

func getNodeFromPath(ctx context.Context, node *core.IpfsNode, dagservice ipld.DAGService, p string) (ipld.Node, error) {
func getNodeFromPath(ctx context.Context, node *core.IpfsNode, api iface.CoreAPI, p string) (ipld.Node, error) {
switch {
case strings.HasPrefix(p, "/ipfs/"):
np, err := path.ParsePath(p)
np, err := iface.ParsePath(p)
if err != nil {
return nil, err
}

resolver := &resolver.Resolver{
DAG: dagservice,
ResolveOnce: uio.ResolveUnixfsOnce,
}

return core.Resolve(ctx, node.Namesys, resolver, np)
return api.ResolveNode(ctx, np)
default:
fsn, err := mfs.Lookup(node.FilesRoot, p)
if err != nil {
Expand Down
27 changes: 13 additions & 14 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ import (
"text/tabwriter"

cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"

cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
unixfs "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
unixfspb "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/pb"
merkledag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
blockservice "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"

cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
)

Expand Down Expand Up @@ -65,6 +63,12 @@ The JSON output contains type information.
return
}

api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

// get options early -> exit early in case of error
if _, _, err := req.Option("headers").Bool(); err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand All @@ -88,18 +92,13 @@ The JSON output contains type information.

var dagnodes []ipld.Node
for _, fpath := range paths {
p, err := path.ParsePath(fpath)
p, err := iface.ParsePath(fpath)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

r := &resolver.Resolver{
DAG: nd.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

dagnode, err := core.Resolve(req.Context(), nd.Namesys, r, p)
dagnode, err := api.ResolveNode(req.Context(), p)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down
75 changes: 32 additions & 43 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import (
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
pin "github.com/ipfs/go-ipfs/pin"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"

cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
"gx/ipfs/QmVkMRSkXrpjqrroEXWuYBvDBnXCdMMY6gsKicBGVGUqKT/go-verifcid"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
bserv "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
)

var PinCmd = &cmds.Command{
Expand Down Expand Up @@ -68,6 +67,12 @@ var addPinCmd = &cmds.Command{
return
}

api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

defer n.Blockstore.PinLock().Unlock()

// set recursive flag
Expand All @@ -79,7 +84,7 @@ var addPinCmd = &cmds.Command{
showProgress, _, _ := req.Option("progress").Bool()

if !showProgress {
added, err := corerepo.Pin(n, req.Context(), req.Arguments(), recursive)
added, err := corerepo.Pin(n, api, req.Context(), req.Arguments(), recursive)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -99,7 +104,7 @@ var addPinCmd = &cmds.Command{
}
ch := make(chan pinResult, 1)
go func() {
added, err := corerepo.Pin(n, ctx, req.Arguments(), recursive)
added, err := corerepo.Pin(n, api, ctx, req.Arguments(), recursive)
ch <- pinResult{pins: added, err: err}
}()

Expand Down Expand Up @@ -193,14 +198,20 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return
}

api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

// set recursive flag
recursive, _, err := req.Option("recursive").Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive)
removed, err := corerepo.Unpin(n, api, req.Context(), req.Arguments(), recursive)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -287,6 +298,12 @@ Example:
return
}

api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

typeStr, _, err := req.Option("type").String()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand All @@ -304,7 +321,7 @@ Example:
var keys map[string]RefKeyObject

if len(req.Arguments()) > 0 {
keys, err = pinLsKeys(req.Context(), req.Arguments(), typeStr, n)
keys, err = pinLsKeys(req.Context(), req.Arguments(), typeStr, n, api)
} else {
keys, err = pinLsAll(req.Context(), typeStr, n)
}
Expand Down Expand Up @@ -364,7 +381,7 @@ new pin and removing the old one.
},
Type: PinOutput{},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -376,42 +393,19 @@ new pin and removing the old one.
return
}

from, err := path.ParsePath(req.Arguments()[0])
from, err := iface.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

to, err := path.ParsePath(req.Arguments()[1])
to, err := iface.ParsePath(req.Arguments()[1])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

r := &resolver.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

fromc, err := core.ResolveToCid(req.Context(), n.Namesys, r, from)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

toc, err := core.ResolveToCid(req.Context(), n.Namesys, r, to)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

err = n.Pinning.Update(req.Context(), fromc, toc, unpin)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

err = n.Pinning.Flush()
err = api.Pin().Update(req.Context(), from, to, options.Pin.Unpin(unpin))
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -501,7 +495,7 @@ type RefKeyList struct {
Keys map[string]RefKeyObject
}

func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode) (map[string]RefKeyObject, error) {
func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode, api iface.CoreAPI) (map[string]RefKeyObject, error) {

mode, ok := pin.StringToMode(typeStr)
if !ok {
Expand All @@ -510,23 +504,18 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN

keys := make(map[string]RefKeyObject)

r := &resolver.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

for _, p := range args {
pth, err := path.ParsePath(p)
pth, err := iface.ParsePath(p)
if err != nil {
return nil, err
}

c, err := core.ResolveToCid(ctx, n.Namesys, r, pth)
c, err := api.ResolvePath(ctx, pth)
if err != nil {
return nil, err
}

pinType, pinned, err := n.Pinning.IsPinnedWithType(c, mode)
pinType, pinned, err := n.Pinning.IsPinnedWithType(c.Cid(), mode)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 1ed555b

Please sign in to comment.