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 Sep 19, 2018
1 parent 3538257 commit 094dc98
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 205 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"

cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
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
52 changes: 28 additions & 24 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@ import (

oldcmds "github.com/ipfs/go-ipfs/commands"
lgc "github.com/ipfs/go-ipfs/commands/legacy"
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/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs"
uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
dag "gx/ipfs/QmXv5mwmQ74r4aiHcNeQ4GAmfB3aWJuqaE4WyDfDfvkgLM/go-merkledag"
bservice "gx/ipfs/Qma2KhbQarYTkmSJAeaMGRAg8HAXAhEWK8ge4SReG7ZSD3/go-blockservice"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
"github.com/ipfs/go-ipfs/core/commands/e"
"github.com/ipfs/go-ipfs/core/coreapi/interface"

humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
ft "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs"
"gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
"gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
"gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
logging "gx/ipfs/QmRREK2CAZ5Re2Bd9zZFG6FeYDppUWt5cMgsoUEp3ktgSr/go-log"
mfs "gx/ipfs/QmRkrpnhZqDxTxwGCsDbuZMr7uCFZHH6SGfrcjgEQwxF3t/go-mfs"
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
"gx/ipfs/QmRkrpnhZqDxTxwGCsDbuZMr7uCFZHH6SGfrcjgEQwxF3t/go-mfs"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
dag "gx/ipfs/QmXv5mwmQ74r4aiHcNeQ4GAmfB3aWJuqaE4WyDfDfvkgLM/go-merkledag"
bservice "gx/ipfs/Qma2KhbQarYTkmSJAeaMGRAg8HAXAhEWK8ge4SReG7ZSD3/go-blockservice"
offline "gx/ipfs/QmcRC35JF2pJQneAxa5LdQBQRumWggccWErogSrCkS1h8T/go-ipfs-exchange-offline"
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
33 changes: 16 additions & 17 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"
unixfs "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs"
"github.com/ipfs/go-ipfs/core/commands/e"
"github.com/ipfs/go-ipfs/core/coreapi/interface"

"gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs"
uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
unixfspb "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/pb"
path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
merkledag "gx/ipfs/QmXv5mwmQ74r4aiHcNeQ4GAmfB3aWJuqaE4WyDfDfvkgLM/go-merkledag"
blockservice "gx/ipfs/Qma2KhbQarYTkmSJAeaMGRAg8HAXAhEWK8ge4SReG7ZSD3/go-blockservice"

cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
"gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
offline "gx/ipfs/QmcRC35JF2pJQneAxa5LdQBQRumWggccWErogSrCkS1h8T/go-ipfs-exchange-offline"
"gx/ipfs/QmXv5mwmQ74r4aiHcNeQ4GAmfB3aWJuqaE4WyDfDfvkgLM/go-merkledag"
"gx/ipfs/Qma2KhbQarYTkmSJAeaMGRAg8HAXAhEWK8ge4SReG7ZSD3/go-blockservice"
"gx/ipfs/QmcRC35JF2pJQneAxa5LdQBQRumWggccWErogSrCkS1h8T/go-ipfs-exchange-offline"
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
Loading

0 comments on commit 094dc98

Please sign in to comment.