Skip to content

Commit 672f69d

Browse files
committed
Corenet API: Split list subcmd into ls/streams
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 95e4594 commit 672f69d

File tree

2 files changed

+90
-25
lines changed

2 files changed

+90
-25
lines changed

core/commands/corenet.go

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package commands
22

33
import (
4+
"bytes"
45
"errors"
6+
"fmt"
57
"io"
68
"strconv"
9+
"text/tabwriter"
710

811
cmds "github.com/ipfs/go-ipfs/commands"
912
corenet "github.com/ipfs/go-ipfs/core/corenet"
1013

11-
manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
12-
ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
13-
net "gx/ipfs/QmVHSBsn8LEeay8m5ERebgUVuhzw838PsyTttCmP6GMJkg/go-libp2p-net"
1414
peerstore "gx/ipfs/QmNUVzEjq3XWJ89hegahPvyfJbTXgTaom48pLb7YBD9gHQ/go-libp2p-peerstore"
15+
net "gx/ipfs/QmVHSBsn8LEeay8m5ERebgUVuhzw838PsyTttCmP6GMJkg/go-libp2p-net"
16+
ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
1517
peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
18+
manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
1619
)
1720

1821
// Command output types.
1922
type AppInfoOutput struct {
20-
Identity string
2123
Protocol string
2224
Address string
2325
}
@@ -31,8 +33,11 @@ type StreamInfoOutput struct {
3133
RemoteAddress string
3234
}
3335

34-
type ListCommandOutput struct {
35-
Apps []AppInfoOutput
36+
type CorenetLsOutput struct {
37+
Apps []AppInfoOutput
38+
}
39+
40+
type CorenetStreamsOutput struct {
3641
Streams []StreamInfoOutput
3742
}
3843

@@ -144,20 +149,20 @@ var CorenetCmd = &cmds.Command{
144149
},
145150

146151
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,
151157
},
152158
}
153159

154-
var listCmd = &cmds.Command{
160+
var CorenetLsCmd = &cmds.Command{
155161
Helptext: cmds.HelpText{
156-
Tagline: "List active application protocol connections.",
162+
Tagline: "List active application protocol listeners.",
157163
},
158164
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),
161166
},
162167
Run: func(req cmds.Request, res cmds.Response) {
163168
n, err := req.InvocContext().GetNode()
@@ -171,16 +176,59 @@ var listCmd = &cmds.Command{
171176
return
172177
}
173178

174-
var output ListCommandOutput
179+
output := &CorenetLsOutput{}
175180

176181
for _, a := range apps.apps {
177182
output.Apps = append(output.Apps, AppInfoOutput{
178-
Identity: a.identity.Pretty(),
179183
Protocol: a.protocol,
180184
Address: a.address.String(),
181185
})
182186
}
183187

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+
184232
for _, s := range streams.streams {
185233
output.Streams = append(output.Streams, StreamInfoOutput{
186234
HandlerId: strconv.FormatUint(s.handlerId, 10),
@@ -195,11 +243,30 @@ var listCmd = &cmds.Command{
195243
})
196244
}
197245

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+
},
199266
},
200267
}
201268

202-
var listenCmd = &cmds.Command{
269+
var CorenetListenCmd = &cmds.Command{
203270
Helptext: cmds.HelpText{
204271
Tagline: "Create application protocol listener and proxy to network multiaddr.",
205272
},
@@ -251,7 +318,6 @@ var listenCmd = &cmds.Command{
251318

252319
// Successful response.
253320
res.SetOutput(&AppInfoOutput{
254-
Identity: app.identity.Pretty(),
255321
Protocol: proto,
256322
Address: addr.String(),
257323
})
@@ -313,7 +379,7 @@ func startStreaming(stream *cnStreamInfo) {
313379
}()
314380
}
315381

316-
var dialCmd = &cmds.Command{
382+
var CorenetDialCmd = &cmds.Command{
317383
Helptext: cmds.HelpText{
318384
Tagline: "Dial to an application service.",
319385
},
@@ -393,7 +459,6 @@ var dialCmd = &cmds.Command{
393459
}
394460

395461
output := AppInfoOutput{
396-
Identity: app.identity.Pretty(),
397462
Protocol: app.protocol,
398463
Address: app.address.String(),
399464
}
@@ -427,7 +492,7 @@ func doAccept(app *cnAppInfo, remote net.Stream, listener manet.Listener) {
427492
startStreaming(&stream)
428493
}
429494

430-
var closeCmd = &cmds.Command{
495+
var CorenetCloseCmd = &cmds.Command{
431496
Helptext: cmds.HelpText{
432497
Tagline: "Closes an active stream listener or client.",
433498
},

test/dependencies/ma-pipe-unidir/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
7-
ma "github.com/multiformats/go-multiaddr"
8-
manet "github.com/multiformats/go-multiaddr-net"
96
"io"
107
"os"
8+
9+
ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
10+
manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
1111
)
1212

1313
const USAGE = "ma-pipe-unidir [-l|--listen] [-h|--help] <send|recv> <multiaddr>\n"

0 commit comments

Comments
 (0)