mirror of
https://github.com/ipfs/kubo.git
synced 2025-10-16 11:13:21 +08:00
resolve cmd: port to new cmd lib
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -2,12 +2,13 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
cmds "github.com/ipfs/go-ipfs/commands"
|
|
||||||
"github.com/ipfs/go-ipfs/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"
|
e "github.com/ipfs/go-ipfs/core/commands/e"
|
||||||
ncmd "github.com/ipfs/go-ipfs/core/commands/name"
|
ncmd "github.com/ipfs/go-ipfs/core/commands/name"
|
||||||
ns "github.com/ipfs/go-ipfs/namesys"
|
ns "github.com/ipfs/go-ipfs/namesys"
|
||||||
@ -15,6 +16,7 @@ import (
|
|||||||
path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
|
path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
|
||||||
|
|
||||||
uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
|
uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
|
||||||
|
"gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
|
||||||
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
|
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
|
||||||
resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
|
resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
|
||||||
)
|
)
|
||||||
@ -67,29 +69,20 @@ Resolve the value of an IPFS DAG path:
|
|||||||
cmdkit.UintOption("dht-record-count", "dhtrc", "Number of records to request for DHT resolution."),
|
cmdkit.UintOption("dht-record-count", "dhtrc", "Number of records to request for DHT resolution."),
|
||||||
cmdkit.StringOption("dht-timeout", "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
|
cmdkit.StringOption("dht-timeout", "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
|
||||||
},
|
},
|
||||||
Run: func(req cmds.Request, res cmds.Response) {
|
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
|
||||||
|
n, err := cmdenv.GetNode(env)
|
||||||
n, err := req.InvocContext().GetNode()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
res.SetError(err, cmdkit.ErrNormal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !n.OnlineMode() {
|
name := req.Arguments[0]
|
||||||
err := n.SetupOfflineRouting()
|
recursive, _ := req.Options["recursive"].(bool)
|
||||||
if err != nil {
|
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
name := req.Arguments()[0]
|
|
||||||
recursive, _, _ := req.Option("recursive").Bool()
|
|
||||||
|
|
||||||
// the case when ipns is resolved step by step
|
// the case when ipns is resolved step by step
|
||||||
if strings.HasPrefix(name, "/ipns/") && !recursive {
|
if strings.HasPrefix(name, "/ipns/") && !recursive {
|
||||||
rc, rcok, _ := req.Option("dht-record-count").Int()
|
rc, rcok := req.Options["dht-record-count"].(int)
|
||||||
dhtt, dhttok, _ := req.Option("dht-timeout").String()
|
dhtt, dhttok := req.Options["dht-timeout"].(string)
|
||||||
ropts := []nsopts.ResolveOpt{nsopts.Depth(1)}
|
ropts := []nsopts.ResolveOpt{nsopts.Depth(1)}
|
||||||
if rcok {
|
if rcok {
|
||||||
ropts = append(ropts, nsopts.DhtRecordCount(uint(rc)))
|
ropts = append(ropts, nsopts.DhtRecordCount(uint(rc)))
|
||||||
@ -106,13 +99,13 @@ Resolve the value of an IPFS DAG path:
|
|||||||
}
|
}
|
||||||
ropts = append(ropts, nsopts.DhtTimeout(d))
|
ropts = append(ropts, nsopts.DhtTimeout(d))
|
||||||
}
|
}
|
||||||
p, err := n.Namesys.Resolve(req.Context(), name, ropts...)
|
p, err := n.Namesys.Resolve(req.Context, name, ropts...)
|
||||||
// ErrResolveRecursion is fine
|
// ErrResolveRecursion is fine
|
||||||
if err != nil && err != ns.ErrResolveRecursion {
|
if err != nil && err != ns.ErrResolveRecursion {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
res.SetError(err, cmdkit.ErrNormal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res.SetOutput(&ncmd.ResolvedPath{Path: p})
|
cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: p})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +121,7 @@ Resolve the value of an IPFS DAG path:
|
|||||||
ResolveOnce: uio.ResolveUnixfsOnce,
|
ResolveOnce: uio.ResolveUnixfsOnce,
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := core.Resolve(req.Context(), n.Namesys, r, p)
|
node, err := core.Resolve(req.Context, n.Namesys, r, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
res.SetError(err, cmdkit.ErrNormal)
|
||||||
return
|
return
|
||||||
@ -136,21 +129,18 @@ Resolve the value of an IPFS DAG path:
|
|||||||
|
|
||||||
c := node.Cid()
|
c := node.Cid()
|
||||||
|
|
||||||
res.SetOutput(&ncmd.ResolvedPath{Path: path.FromCid(c)})
|
cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.FromCid(c)})
|
||||||
},
|
},
|
||||||
Marshalers: cmds.MarshalerMap{
|
Encoders: cmds.EncoderMap{
|
||||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
|
||||||
v, err := unwrapOutput(res.Output())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
output, ok := v.(*ncmd.ResolvedPath)
|
output, ok := v.(*ncmd.ResolvedPath)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, e.TypeErr(output, v)
|
return e.TypeErr(output, v)
|
||||||
}
|
}
|
||||||
return strings.NewReader(output.Path.String() + "\n"), nil
|
|
||||||
},
|
fmt.Fprintln(w, output.Path.String())
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
Type: ncmd.ResolvedPath{},
|
Type: ncmd.ResolvedPath{},
|
||||||
}
|
}
|
||||||
|
@ -135,7 +135,7 @@ var rootSubcommands = map[string]*cmds.Command{
|
|||||||
"ping": lgc.NewCommand(PingCmd),
|
"ping": lgc.NewCommand(PingCmd),
|
||||||
"p2p": lgc.NewCommand(P2PCmd),
|
"p2p": lgc.NewCommand(P2PCmd),
|
||||||
"refs": lgc.NewCommand(RefsCmd),
|
"refs": lgc.NewCommand(RefsCmd),
|
||||||
"resolve": lgc.NewCommand(ResolveCmd),
|
"resolve": ResolveCmd,
|
||||||
"swarm": lgc.NewCommand(SwarmCmd),
|
"swarm": lgc.NewCommand(SwarmCmd),
|
||||||
"tar": lgc.NewCommand(TarCmd),
|
"tar": lgc.NewCommand(TarCmd),
|
||||||
"file": lgc.NewCommand(unixfs.UnixFSCmd),
|
"file": lgc.NewCommand(unixfs.UnixFSCmd),
|
||||||
@ -183,7 +183,7 @@ var rootROSubcommands = map[string]*cmds.Command{
|
|||||||
"resolve": dag.DagResolveCmd,
|
"resolve": dag.DagResolveCmd,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
"resolve": lgc.NewCommand(ResolveCmd),
|
"resolve": ResolveCmd,
|
||||||
"version": lgc.NewCommand(VersionCmd),
|
"version": lgc.NewCommand(VersionCmd),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user