cmds2: bootstrap command fix add
Juan Batiz-Benet committed
Nov 13, 2014 at 01:20 UTC
89ec480ef1ec9cd80e84a84e0658e3fce77d77f1
1 file changed
+45
-29
core/commands2/bootstrap.go
+45
-29
@@ -1,7 +1,8 @@
1
package commands
2
3
import (
4
- "fmt"
4
+ "bytes"
5
+ "io"
6
"strings"
7
8
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
@@ -38,7 +39,7 @@ Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
39
Subcommands: map[string]*cmds.Command{
40
"list": bootstrapListCmd,
41
"add": bootstrapAddCmd,
41
- "remove": bootstrapRemoveCmd,
42
+ "rm": bootstrapRemoveCmd,
43
},
44
}
45
@@ -79,13 +80,14 @@ in the bootstrap list).
80
Type: &BootstrapOutput{},
81
Marshalers: cmds.MarshalerMap{
82
cmds.Text: func(res cmds.Response) ([]byte, error) {
82
- v := res.Output().(*BootstrapOutput)
83
- s := fmt.Sprintf("Added %v peers to the bootstrap list:\n", len(v.Peers))
84
- marshalled, err := bootstrapMarshaler(res)
85
- if err != nil {
86
- return nil, err
83
+ v, ok := res.Output().(*BootstrapOutput)
84
+ if !ok {
85
+ return nil, u.ErrCast()
86
}
88
- return append([]byte(s), marshalled...), nil
87
+
88
+ var buf bytes.Buffer
89
+ err := bootstrapWritePeers(&buf, "added ", v.Peers)
90
+ return buf.Bytes(), err
91
},
92
},
93
}
@@ -126,13 +128,14 @@ var bootstrapRemoveCmd = &cmds.Command{
128
Type: &BootstrapOutput{},
129
Marshalers: cmds.MarshalerMap{
130
cmds.Text: func(res cmds.Response) ([]byte, error) {
129
- v := res.Output().(*BootstrapOutput)
130
- s := fmt.Sprintf("Removed %v peers from the bootstrap list:\n", len(v.Peers))
131
- marshalled, err := bootstrapMarshaler(res)
132
- if err != nil {
133
- return nil, err
131
+ v, ok := res.Output().(*BootstrapOutput)
132
+ if !ok {
133
+ return nil, u.ErrCast()
134
}
135
- return append([]byte(s), marshalled...), nil
135
+
136
+ var buf bytes.Buffer
137
+ err := bootstrapWritePeers(&buf, "removed ", v.Peers)
138
+ return buf.Bytes(), err
139
},
140
},
141
}
@@ -164,15 +167,33 @@ func bootstrapMarshaler(res cmds.Response) ([]byte, error) {
167
return nil, u.ErrCast()
168
}
169
167
- s := ""
168
- for _, peer := range v.Peers {
169
- s += fmt.Sprintf("%s/%s\n", peer.Address, peer.PeerID)
170
- }
170
+ var buf bytes.Buffer
171
+ err := bootstrapWritePeers(&buf, "", v.Peers)
172
+ return buf.Bytes(), err
173
+}
174
+
175
+func bootstrapWritePeers(w io.Writer, prefix string, peers []*config.BootstrapPeer) error {
176
172
- return []byte(s), nil
177
+ for _, peer := range peers {
178
+ s := prefix + peer.Address + "/" + peer.PeerID + "\n"
179
+ _, err := w.Write([]byte(s))
180
+ if err != nil {
181
+ return err
182
+ }
183
+ }
184
+ return nil
185
}
186
187
func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error) {
188
+ inputAddrs := make([]string, len(input))
189
+ for i, v := range input {
190
+ addr, ok := v.(string)
191
+ if !ok {
192
+ return nil, u.ErrCast()
193
+ }
194
+ inputAddrs[i] = addr
195
+ }
196
+
197
split := func(addr string) (string, string) {
198
idx := strings.LastIndex(addr, "/")
199
if idx == -1 {
@@ -182,12 +203,7 @@ func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error)
203
}
204
205
peers := []*config.BootstrapPeer{}
185
- for _, v := range input {
186
- addr, ok := v.(string)
187
- if !ok {
188
- return nil, u.ErrCast()
189
- }
190
-
206
+ for _, addr := range inputAddrs {
207
addrS, peeridS := split(addr)
208
209
// make sure addrS parses as a multiaddr.
@@ -221,7 +237,7 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []*config.Bootstrap
237
for _, peer := range peers {
238
duplicate := false
239
for _, peer2 := range cfg.Bootstrap {
224
- if peer.Address == peer2.Address {
240
+ if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID {
241
duplicate = true
242
break
243
}
@@ -241,13 +257,13 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []*config.Bootstrap
257
return added, nil
258
}
259
244
-func bootstrapRemove(filename string, cfg *config.Config, peers []*config.BootstrapPeer) ([]*config.BootstrapPeer, error) {
245
- removed := make([]*config.BootstrapPeer, 0, len(peers))
260
+func bootstrapRemove(filename string, cfg *config.Config, toRemove []*config.BootstrapPeer) ([]*config.BootstrapPeer, error) {
261
+ removed := make([]*config.BootstrapPeer, 0, len(toRemove))
262
keep := make([]*config.BootstrapPeer, 0, len(cfg.Bootstrap))
263
264
for _, peer := range cfg.Bootstrap {
265
found := false
250
- for _, peer2 := range peers {
266
+ for _, peer2 := range toRemove {
267
if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID {
268
found = true
269
removed = append(removed, peer)