cmds: swarm peer
Juan Batiz-Benet committed
Nov 18, 2014 at 00:48 UTC
4d78c2d7ff14183f105029ba55fcc21808a1e2d8
2 files changed
+82
core/commands2/root.go
+1
@@ -79,6 +79,7 @@ var rootSubcommands = map[string]*cmds.Command{
79
"object": objectCmd,
80
"refs": refsCmd,
81
"id": idCmd,
82
+ "swarm": swarmCmd,
83
}
84
85
func init() {
core/commands2/swarm.go
new
+81
@@ -0,0 +1,81 @@
1
+package commands
2
+
3
+import (
4
+ "bytes"
5
+ "fmt"
6
+
7
+ cmds "github.com/jbenet/go-ipfs/commands"
8
+ errors "github.com/jbenet/go-ipfs/util/debugerror"
9
+)
10
+
11
+type stringList struct {
12
+ Strings []string
13
+}
14
+
15
+var swarmCmd = &cmds.Command{
16
+ Helptext: cmds.HelpText{
17
+ Tagline: "swarm inspection tool",
18
+ Synopsis: `
19
+ipfs swarm peers - List peers with open connections
20
+ipfs swarm connect <address> - Open connection to a given peer
21
+`,
22
+ ShortDescription: `
23
+ipfs swarm is a tool to manipulate the network swarm. The swarm is the
24
+component that opens, listens for, and maintains connections to other
25
+ipfs peers in the internet.
26
+`,
27
+ },
28
+ Subcommands: map[string]*cmds.Command{
29
+ "peers": swarmPeersCmd,
30
+ // "connect": swarmConnectCmd,
31
+ },
32
+}
33
+
34
+var swarmPeersCmd = &cmds.Command{
35
+ Helptext: cmds.HelpText{
36
+ Tagline: "List peers with open connections",
37
+ ShortDescription: `
38
+ipfs swarm peers lists the set of peers this node is connected to.
39
+`,
40
+ },
41
+ Run: func(req cmds.Request) (interface{}, error) {
42
+
43
+ log.Debug("ipfs swarm peers")
44
+ n, err := req.Context().GetNode()
45
+ if err != nil {
46
+ return nil, err
47
+ }
48
+
49
+ if n.Network == nil {
50
+ return nil, errNotOnline
51
+ }
52
+
53
+ conns := n.Network.GetConnections()
54
+ addrs := make([]string, len(conns))
55
+ for i, c := range conns {
56
+ pid := c.RemotePeer().ID()
57
+ addr := c.RemoteMultiaddr()
58
+ addrs[i] = fmt.Sprintf("%s/%s", addr, pid)
59
+ }
60
+
61
+ return &stringList{addrs}, nil
62
+ },
63
+ Marshalers: cmds.MarshalerMap{
64
+ cmds.Text: stringListMarshaler,
65
+ },
66
+ Type: &stringList{},
67
+}
68
+
69
+func stringListMarshaler(res cmds.Response) ([]byte, error) {
70
+ list, ok := res.Output().(*stringList)
71
+ if !ok {
72
+ return nil, errors.New("failed to cast []string")
73
+ }
74
+
75
+ var buf bytes.Buffer
76
+ for _, s := range list.Strings {
77
+ buf.Write([]byte(s))
78
+ buf.Write([]byte("\n"))
79
+ }
80
+ return buf.Bytes(), nil
81
+}