@cryptotaxi247 / kubo / commits / 1e31aba77

resolve cmd: port to new cmd lib

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Sep 18, 2018 at 02:17 UTC 1e31aba77b8023c596b929d569913ddbb5f22955
2 files changed +22 -32
core/commands/resolve.go
+20 -30
@@ -2,12 +2,13 @@ package commands
2
3 import (
4 "errors"
5 + "fmt"
6 "io"
7 "strings"
8 "time"
9
9 - cmds "github.com/ipfs/go-ipfs/commands"
10 "github.com/ipfs/go-ipfs/core"
11 + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12 e "github.com/ipfs/go-ipfs/core/commands/e"
13 ncmd "github.com/ipfs/go-ipfs/core/commands/name"
14 ns "github.com/ipfs/go-ipfs/namesys"
@@ -15,6 +16,7 @@ import (
16 path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
17
18 uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
19 + "gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
20 "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
21 resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
22 )
@@ -67,29 +69,20 @@ Resolve the value of an IPFS DAG path:
69 cmdkit.UintOption("dht-record-count", "dhtrc", "Number of records to request for DHT resolution."),
70 cmdkit.StringOption("dht-timeout", "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
71 },
70 - Run: func(req cmds.Request, res cmds.Response) {
71 -
72 - n, err := req.InvocContext().GetNode()
72 + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
73 + n, err := cmdenv.GetNode(env)
74 if err != nil {
75 res.SetError(err, cmdkit.ErrNormal)
76 return
77 }
78
78 - if !n.OnlineMode() {
79 - err := n.SetupOfflineRouting()
80 - if err != nil {
81 - res.SetError(err, cmdkit.ErrNormal)
82 - return
83 - }
84 - }
85 -
86 - name := req.Arguments()[0]
87 - recursive, _, _ := req.Option("recursive").Bool()
79 + name := req.Arguments[0]
80 + recursive, _ := req.Options["recursive"].(bool)
81
82 // the case when ipns is resolved step by step
83 if strings.HasPrefix(name, "/ipns/") && !recursive {
91 - rc, rcok, _ := req.Option("dht-record-count").Int()
92 - dhtt, dhttok, _ := req.Option("dht-timeout").String()
84 + rc, rcok := req.Options["dht-record-count"].(int)
85 + dhtt, dhttok := req.Options["dht-timeout"].(string)
86 ropts := []nsopts.ResolveOpt{nsopts.Depth(1)}
87 if rcok {
88 ropts = append(ropts, nsopts.DhtRecordCount(uint(rc)))
@@ -106,13 +99,13 @@ Resolve the value of an IPFS DAG path:
99 }
100 ropts = append(ropts, nsopts.DhtTimeout(d))
101 }
109 - p, err := n.Namesys.Resolve(req.Context(), name, ropts...)
102 + p, err := n.Namesys.Resolve(req.Context, name, ropts...)
103 // ErrResolveRecursion is fine
104 if err != nil && err != ns.ErrResolveRecursion {
105 res.SetError(err, cmdkit.ErrNormal)
106 return
107 }
115 - res.SetOutput(&ncmd.ResolvedPath{Path: p})
108 + cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: p})
109 return
110 }
111
@@ -128,7 +121,7 @@ Resolve the value of an IPFS DAG path:
121 ResolveOnce: uio.ResolveUnixfsOnce,
122 }
123
131 - node, err := core.Resolve(req.Context(), n.Namesys, r, p)
124 + node, err := core.Resolve(req.Context, n.Namesys, r, p)
125 if err != nil {
126 res.SetError(err, cmdkit.ErrNormal)
127 return
@@ -136,21 +129,18 @@ Resolve the value of an IPFS DAG path:
129
130 c := node.Cid()
131
139 - res.SetOutput(&ncmd.ResolvedPath{Path: path.FromCid(c)})
132 + cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.FromCid(c)})
133 },
141 - Marshalers: cmds.MarshalerMap{
142 - cmds.Text: func(res cmds.Response) (io.Reader, error) {
143 - v, err := unwrapOutput(res.Output())
144 - if err != nil {
145 - return nil, err
146 - }
147 -
134 + Encoders: cmds.EncoderMap{
135 + cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
136 output, ok := v.(*ncmd.ResolvedPath)
137 if !ok {
150 - return nil, e.TypeErr(output, v)
138 + return e.TypeErr(output, v)
139 }
152 - return strings.NewReader(output.Path.String() + "\n"), nil
153 - },
140 +
141 + fmt.Fprintln(w, output.Path.String())
142 + return nil
143 + }),
144 },
145 Type: ncmd.ResolvedPath{},
146 }
core/commands/root.go
+2 -2
@@ -135,7 +135,7 @@ var rootSubcommands = map[string]*cmds.Command{
135 "ping": lgc.NewCommand(PingCmd),
136 "p2p": lgc.NewCommand(P2PCmd),
137 "refs": lgc.NewCommand(RefsCmd),
138 - "resolve": lgc.NewCommand(ResolveCmd),
138 + "resolve": ResolveCmd,
139 "swarm": lgc.NewCommand(SwarmCmd),
140 "tar": lgc.NewCommand(TarCmd),
141 "file": lgc.NewCommand(unixfs.UnixFSCmd),
@@ -183,7 +183,7 @@ var rootROSubcommands = map[string]*cmds.Command{
183 "resolve": dag.DagResolveCmd,
184 },
185 }),
186 - "resolve": lgc.NewCommand(ResolveCmd),
186 + "resolve": ResolveCmd,
187 "version": lgc.NewCommand(VersionCmd),
188 }
189