Commit 4d78c2d7 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

cmds: swarm peer

parent faab9842
...@@ -79,6 +79,7 @@ var rootSubcommands = map[string]*cmds.Command{ ...@@ -79,6 +79,7 @@ var rootSubcommands = map[string]*cmds.Command{
"object": objectCmd, "object": objectCmd,
"refs": refsCmd, "refs": refsCmd,
"id": idCmd, "id": idCmd,
"swarm": swarmCmd,
} }
func init() { func init() {
......
package commands
import (
"bytes"
"fmt"
cmds "github.com/jbenet/go-ipfs/commands"
errors "github.com/jbenet/go-ipfs/util/debugerror"
)
type stringList struct {
Strings []string
}
var swarmCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "swarm inspection tool",
Synopsis: `
ipfs swarm peers - List peers with open connections
ipfs swarm connect <address> - Open connection to a given peer
`,
ShortDescription: `
ipfs swarm is a tool to manipulate the network swarm. The swarm is the
component that opens, listens for, and maintains connections to other
ipfs peers in the internet.
`,
},
Subcommands: map[string]*cmds.Command{
"peers": swarmPeersCmd,
// "connect": swarmConnectCmd,
},
}
var swarmPeersCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List peers with open connections",
ShortDescription: `
ipfs swarm peers lists the set of peers this node is connected to.
`,
},
Run: func(req cmds.Request) (interface{}, error) {
log.Debug("ipfs swarm peers")
n, err := req.Context().GetNode()
if err != nil {
return nil, err
}
if n.Network == nil {
return nil, errNotOnline
}
conns := n.Network.GetConnections()
addrs := make([]string, len(conns))
for i, c := range conns {
pid := c.RemotePeer().ID()
addr := c.RemoteMultiaddr()
addrs[i] = fmt.Sprintf("%s/%s", addr, pid)
}
return &stringList{addrs}, nil
},
Marshalers: cmds.MarshalerMap{
cmds.Text: stringListMarshaler,
},
Type: &stringList{},
}
func stringListMarshaler(res cmds.Response) ([]byte, error) {
list, ok := res.Output().(*stringList)
if !ok {
return nil, errors.New("failed to cast []string")
}
var buf bytes.Buffer
for _, s := range list.Strings {
buf.Write([]byte(s))
buf.Write([]byte("\n"))
}
return buf.Bytes(), nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment