ipfs swarm addrs local - show local addrs
Add a command to return local addresses. License: MIT Signed-off-by: Juan Batiz-Benet <juan@benet.ai>
Juan Batiz-Benet committed
Jun 19, 2015 at 04:20 UTC
500f51300d26f5928bb0f3a9a63e454f698b45cb
2 files changed
+81
core/commands/swarm.go
+48
@@ -5,6 +5,7 @@ import (
5
"errors"
6
"fmt"
7
"io"
8
+ "path"
9
"sort"
10
11
cmds "github.com/ipfs/go-ipfs/commands"
@@ -91,6 +92,9 @@ var swarmAddrsCmd = &cmds.Command{
92
ipfs swarm addrs lists all addresses this node is aware of.
93
`,
94
},
95
+ Subcommands: map[string]*cmds.Command{
96
+ "local": swarmAddrsLocalCmd,
97
+ },
98
Run: func(req cmds.Request, res cmds.Response) {
99
100
n, err := req.Context().GetNode()
@@ -144,6 +148,50 @@ ipfs swarm addrs lists all addresses this node is aware of.
148
Type: addrMap{},
149
}
150
151
+var swarmAddrsLocalCmd = &cmds.Command{
152
+ Helptext: cmds.HelpText{
153
+ Tagline: "List local addresses.",
154
+ ShortDescription: `
155
+ipfs swarm addrs local lists all local addresses the node is listening on.
156
+`,
157
+ },
158
+ Options: []cmds.Option{
159
+ cmds.BoolOption("id", "Show peer ID in addresses"),
160
+ },
161
+ Run: func(req cmds.Request, res cmds.Response) {
162
+
163
+ n, err := req.Context().GetNode()
164
+ if err != nil {
165
+ res.SetError(err, cmds.ErrNormal)
166
+ return
167
+ }
168
+
169
+ if n.PeerHost == nil {
170
+ res.SetError(errNotOnline, cmds.ErrClient)
171
+ return
172
+ }
173
+
174
+ showid, _, _ := req.Option("id").Bool()
175
+ id := n.Identity.Pretty()
176
+
177
+ var addrs []string
178
+ for _, addr := range n.PeerHost.Addrs() {
179
+ saddr := addr.String()
180
+ if showid {
181
+ saddr = path.Join(saddr, "ipfs", id)
182
+ }
183
+ addrs = append(addrs, saddr)
184
+ }
185
+ sort.Sort(sort.StringSlice(addrs))
186
+
187
+ res.SetOutput(&stringList{addrs})
188
+ },
189
+ Type: stringList{},
190
+ Marshalers: cmds.MarshalerMap{
191
+ cmds.Text: stringListMarshaler,
192
+ },
193
+}
194
+
195
var swarmConnectCmd = &cmds.Command{
196
Helptext: cmds.HelpText{
197
Tagline: "Open connection to a given address",
test/sharness/t0140-swarm.sh
new
+33
@@ -0,0 +1,33 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2014 Jeromy Johnson
4
+# MIT Licensed; see the LICENSE file in this repository.
5
+#
6
+
7
+test_description="Test ipfs swarm command"
8
+
9
+. lib/test-lib.sh
10
+
11
+test_init_ipfs
12
+
13
+test_launch_ipfs_daemon
14
+
15
+test_expect_success 'disconnected: peers is empty' '
16
+ ipfs swarm peers >actual &&
17
+ test_must_be_empty actual
18
+'
19
+
20
+test_expect_success 'disconnected: addrs local has localhost' '
21
+ ipfs swarm addrs local >actual &&
22
+ grep "/ip4/127.0.0.1" actual
23
+'
24
+
25
+test_expect_success 'disconnected: addrs local matches ipfs id' '
26
+ ipfs id -f="<addrs>\\n" | sort >expected &&
27
+ ipfs swarm addrs local --id | sort >actual &&
28
+ test_cmp expected actual
29
+'
30
+
31
+test_kill_ipfs_daemon
32
+
33
+test_done