Commit 500f5130 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

ipfs swarm addrs local - show local addrs

Add a command to return local addresses.

License: MIT
Signed-off-by: default avatarJuan Batiz-Benet <juan@benet.ai>
parent c5caccba
......@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"path"
"sort"
cmds "github.com/ipfs/go-ipfs/commands"
......@@ -91,6 +92,9 @@ var swarmAddrsCmd = &cmds.Command{
ipfs swarm addrs lists all addresses this node is aware of.
`,
},
Subcommands: map[string]*cmds.Command{
"local": swarmAddrsLocalCmd,
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.Context().GetNode()
......@@ -144,6 +148,50 @@ ipfs swarm addrs lists all addresses this node is aware of.
Type: addrMap{},
}
var swarmAddrsLocalCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List local addresses.",
ShortDescription: `
ipfs swarm addrs local lists all local addresses the node is listening on.
`,
},
Options: []cmds.Option{
cmds.BoolOption("id", "Show peer ID in addresses"),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.Context().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if n.PeerHost == nil {
res.SetError(errNotOnline, cmds.ErrClient)
return
}
showid, _, _ := req.Option("id").Bool()
id := n.Identity.Pretty()
var addrs []string
for _, addr := range n.PeerHost.Addrs() {
saddr := addr.String()
if showid {
saddr = path.Join(saddr, "ipfs", id)
}
addrs = append(addrs, saddr)
}
sort.Sort(sort.StringSlice(addrs))
res.SetOutput(&stringList{addrs})
},
Type: stringList{},
Marshalers: cmds.MarshalerMap{
cmds.Text: stringListMarshaler,
},
}
var swarmConnectCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Open connection to a given address",
......
#!/bin/sh
#
# Copyright (c) 2014 Jeromy Johnson
# MIT Licensed; see the LICENSE file in this repository.
#
test_description="Test ipfs swarm command"
. lib/test-lib.sh
test_init_ipfs
test_launch_ipfs_daemon
test_expect_success 'disconnected: peers is empty' '
ipfs swarm peers >actual &&
test_must_be_empty actual
'
test_expect_success 'disconnected: addrs local has localhost' '
ipfs swarm addrs local >actual &&
grep "/ip4/127.0.0.1" actual
'
test_expect_success 'disconnected: addrs local matches ipfs id' '
ipfs id -f="<addrs>\\n" | sort >expected &&
ipfs swarm addrs local --id | sort >actual &&
test_cmp expected actual
'
test_kill_ipfs_daemon
test_done
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