@cryptotaxi247 / kubo / commits / 9414e73c3

Corenet API: Split list subcmd into ls/streams

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

Łukasz Magiera committed May 26, 2017 at 22:52 UTC 9414e73c34d998c84ba6448946f83012c2eec9b9
2 files changed +90 -25
core/commands/corenet.go
+87 -22
@@ -1,23 +1,25 @@
1 package commands
2
3 import (
4 + "bytes"
5 "errors"
6 + "fmt"
7 "io"
8 "strconv"
9 + "text/tabwriter"
10
11 cmds "github.com/ipfs/go-ipfs/commands"
12 corenet "github.com/ipfs/go-ipfs/core/corenet"
13
11 - manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
12 - ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
13 - net "gx/ipfs/QmVHSBsn8LEeay8m5ERebgUVuhzw838PsyTttCmP6GMJkg/go-libp2p-net"
14 peerstore "gx/ipfs/QmNUVzEjq3XWJ89hegahPvyfJbTXgTaom48pLb7YBD9gHQ/go-libp2p-peerstore"
15 + net "gx/ipfs/QmVHSBsn8LEeay8m5ERebgUVuhzw838PsyTttCmP6GMJkg/go-libp2p-net"
16 + ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
17 peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
18 + manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
19 )
20
21 // Command output types.
22 type AppInfoOutput struct {
20 - Identity string
23 Protocol string
24 Address string
25 }
@@ -31,8 +33,11 @@ type StreamInfoOutput struct {
33 RemoteAddress string
34 }
35
34 -type ListCommandOutput struct {
35 - Apps []AppInfoOutput
36 +type CorenetLsOutput struct {
37 + Apps []AppInfoOutput
38 +}
39 +
40 +type CorenetStreamsOutput struct {
41 Streams []StreamInfoOutput
42 }
43
@@ -144,20 +149,20 @@ var CorenetCmd = &cmds.Command{
149 },
150
151 Subcommands: map[string]*cmds.Command{
147 - "list": listCmd,
148 - "dial": dialCmd,
149 - "listen": listenCmd,
150 - "close": closeCmd,
152 + "ls": CorenetLsCmd,
153 + "streams": CorenetStreamsCmd,
154 + "dial": CorenetDialCmd,
155 + "listen": CorenetListenCmd,
156 + "close": CorenetCloseCmd,
157 },
158 }
159
154 -var listCmd = &cmds.Command{
160 +var CorenetLsCmd = &cmds.Command{
161 Helptext: cmds.HelpText{
156 - Tagline: "List active application protocol connections.",
162 + Tagline: "List active application protocol listeners.",
163 },
164 Options: []cmds.Option{
159 - cmds.BoolOption("apps", "a", "Display only local application protocol listeners.").Default(false),
160 - cmds.BoolOption("streams", "s", "Display active application protocol streams.").Default(false),
165 + cmds.BoolOption("headers", "v", "Print table headers (HandlerId, Protocol, Local, Remote).").Default(false),
166 },
167 Run: func(req cmds.Request, res cmds.Response) {
168 n, err := req.InvocContext().GetNode()
@@ -171,16 +176,59 @@ var listCmd = &cmds.Command{
176 return
177 }
178
174 - var output ListCommandOutput
179 + output := &CorenetLsOutput{}
180
181 for _, a := range apps.apps {
182 output.Apps = append(output.Apps, AppInfoOutput{
178 - Identity: a.identity.Pretty(),
183 Protocol: a.protocol,
184 Address: a.address.String(),
185 })
186 }
187
188 + res.SetOutput(output)
189 + },
190 + Type: CorenetLsOutput{},
191 + Marshalers: cmds.MarshalerMap{
192 + cmds.Text: func(res cmds.Response) (io.Reader, error) {
193 + headers, _, _ := res.Request().Option("headers").Bool()
194 + list, _ := res.Output().(*CorenetLsOutput)
195 + buf := new(bytes.Buffer)
196 + w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
197 + for _, app := range list.Apps {
198 + if headers {
199 + fmt.Fprintln(w, "Address\tProtocol")
200 + }
201 +
202 + fmt.Fprintf(w, "%s\t%s\n", app.Address, app.Protocol)
203 + }
204 + w.Flush()
205 +
206 + return buf, nil
207 + },
208 + },
209 +}
210 +
211 +var CorenetStreamsCmd = &cmds.Command{
212 + Helptext: cmds.HelpText{
213 + Tagline: "List active application protocol connections.",
214 + },
215 + Options: []cmds.Option{
216 + cmds.BoolOption("headers", "v", "Print table headers (HandlerId, Protocol, Local, Remote).").Default(false),
217 + },
218 + Run: func(req cmds.Request, res cmds.Response) {
219 + n, err := req.InvocContext().GetNode()
220 + if err != nil {
221 + res.SetError(err, cmds.ErrNormal)
222 + return
223 + }
224 +
225 + if !n.OnlineMode() {
226 + res.SetError(errNotOnline, cmds.ErrClient)
227 + return
228 + }
229 +
230 + output := &CorenetStreamsOutput{}
231 +
232 for _, s := range streams.streams {
233 output.Streams = append(output.Streams, StreamInfoOutput{
234 HandlerId: strconv.FormatUint(s.handlerId, 10),
@@ -195,11 +243,30 @@ var listCmd = &cmds.Command{
243 })
244 }
245
198 - res.SetOutput(&output)
246 + res.SetOutput(output)
247 + },
248 + Type: CorenetStreamsOutput{},
249 + Marshalers: cmds.MarshalerMap{
250 + cmds.Text: func(res cmds.Response) (io.Reader, error) {
251 + headers, _, _ := res.Request().Option("headers").Bool()
252 + list, _ := res.Output().(*CorenetStreamsOutput)
253 + buf := new(bytes.Buffer)
254 + w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
255 + for _, stream := range list.Streams {
256 + if headers {
257 + fmt.Fprintln(w, "HandlerId\tProtocol\tLocal\tRemote")
258 + }
259 +
260 + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", stream.HandlerId, stream.Protocol, stream.LocalAddress, stream.RemotePeer)
261 + }
262 + w.Flush()
263 +
264 + return buf, nil
265 + },
266 },
267 }
268
202 -var listenCmd = &cmds.Command{
269 +var CorenetListenCmd = &cmds.Command{
270 Helptext: cmds.HelpText{
271 Tagline: "Create application protocol listener and proxy to network multiaddr.",
272 },
@@ -251,7 +318,6 @@ var listenCmd = &cmds.Command{
318
319 // Successful response.
320 res.SetOutput(&AppInfoOutput{
254 - Identity: app.identity.Pretty(),
321 Protocol: proto,
322 Address: addr.String(),
323 })
@@ -313,7 +379,7 @@ func startStreaming(stream *cnStreamInfo) {
379 }()
380 }
381
316 -var dialCmd = &cmds.Command{
382 +var CorenetDialCmd = &cmds.Command{
383 Helptext: cmds.HelpText{
384 Tagline: "Dial to an application service.",
385 },
@@ -393,7 +459,6 @@ var dialCmd = &cmds.Command{
459 }
460
461 output := AppInfoOutput{
396 - Identity: app.identity.Pretty(),
462 Protocol: app.protocol,
463 Address: app.address.String(),
464 }
@@ -427,7 +492,7 @@ func doAccept(app *cnAppInfo, remote net.Stream, listener manet.Listener) {
492 startStreaming(&stream)
493 }
494
430 -var closeCmd = &cmds.Command{
495 +var CorenetCloseCmd = &cmds.Command{
496 Helptext: cmds.HelpText{
497 Tagline: "Closes an active stream listener or client.",
498 },
test/dependencies/ma-pipe-unidir/main.go
+3 -3
@@ -3,11 +3,11 @@ package main
3 import (
4 "flag"
5 "fmt"
6 -
7 - ma "github.com/multiformats/go-multiaddr"
8 - manet "github.com/multiformats/go-multiaddr-net"
6 "io"
7 "os"
8 +
9 + ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
10 + manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
11 )
12
13 const USAGE = "ma-pipe-unidir [-l|--listen] [-h|--help] <send|recv> <multiaddr>\n"