@cryptotaxi247 / kubo / commits / daf6fbd34

feat(command): add direction flag for swarm peers command

License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>

Overbool committed Sep 14, 2018 at 08:46 UTC daf6fbd347e4f55456ad1e152dd1f5e1725e081d
1 file changed +9 -6
core/commands/swarm.go
+9 -6
@@ -62,6 +62,7 @@ var swarmPeersCmd = &cmds.Command{
62 cmdkit.BoolOption("verbose", "v", "display all extra information"),
63 cmdkit.BoolOption("streams", "Also list information about open streams for each peer"),
64 cmdkit.BoolOption("latency", "Also list information about latency to each peer"),
65 + cmdkit.BoolOption("direction", "Also list information about the direction of connection"),
66 },
67 Run: func(req cmds.Request, res cmds.Response) {
68
@@ -79,6 +80,7 @@ var swarmPeersCmd = &cmds.Command{
80 verbose, _, _ := req.Option("verbose").Bool()
81 latency, _, _ := req.Option("latency").Bool()
82 streams, _, _ := req.Option("streams").Bool()
83 + direction, _, _ := req.Option("direction").Bool()
84
85 conns := n.PeerHost.Network().Conns()
86 var out connInfos
@@ -86,9 +88,8 @@ var swarmPeersCmd = &cmds.Command{
88 pid := c.RemotePeer()
89 addr := c.RemoteMultiaddr()
90 ci := connInfo{
89 - Addr: addr.String(),
90 - Peer: pid.Pretty(),
91 - Direction: inet.Direction(-1),
91 + Addr: addr.String(),
92 + Peer: pid.Pretty(),
93 }
94
95 /*
@@ -99,10 +100,12 @@ var swarmPeersCmd = &cmds.Command{
100 }
101 */
102
102 - if verbose || latency {
103 + if verbose || direction {
104 // set direction
105 ci.Direction = c.Stat().Direction
106 + }
107
108 + if verbose || latency {
109 lat := n.Peerstore.LatencyEWMA(pid)
110 if lat == 0 {
111 ci.Latency = "n/a"
@@ -149,7 +152,7 @@ var swarmPeersCmd = &cmds.Command{
152 fmt.Fprintf(buf, " %s", info.Latency)
153 }
154
152 - if info.Direction >= 0 {
155 + if info.Direction != inet.DirUnknown {
156 fmt.Fprintf(buf, " %s", directionString(info.Direction))
157 }
158
@@ -219,7 +222,7 @@ func directionString(d inet.Direction) string {
222 case inet.DirOutbound:
223 return "outbound"
224 default:
222 - return "unknown"
225 + return ""
226 }
227 }
228