cmd/bootstrap: bugfix on add (dedup)
Juan Batiz-Benet committed
Feb 1, 2015 at 06:07 UTC
bebc3ca0fd8d96dc29e23edb5f1bc04799618aed
1 file changed
+23
-11
core/commands/bootstrap.go
+23
-11
@@ -238,28 +238,40 @@ func bootstrapWritePeers(w io.Writer, prefix string, peers []string) error {
238
}
239
240
func bootstrapAdd(r repo.Repo, cfg *config.Config, peers []config.BootstrapPeer) ([]config.BootstrapPeer, error) {
241
- added := make([]config.BootstrapPeer, 0, len(peers))
241
+ addedMap := map[string]struct{}{}
242
+ addedList := make([]config.BootstrapPeer, 0, len(peers))
243
244
+ // re-add cfg bootstrap peers to rm dupes
245
+ bpeers := cfg.Bootstrap
246
+ cfg.Bootstrap = nil
247
+
248
+ // add new peers
249
for _, peer := range peers {
244
- duplicate := false
245
- for _, peer2 := range cfg.Bootstrap {
246
- if peer.Equal(peer2) {
247
- duplicate = true
248
- break
249
- }
250
+ s := peer.String()
251
+ if _, found := addedMap[s]; found {
252
+ continue
253
}
254
252
- if !duplicate {
253
- cfg.Bootstrap = append(cfg.Bootstrap, peer.String())
254
- added = append(added, peer)
255
+ cfg.Bootstrap = append(cfg.Bootstrap, s)
256
+ addedList = append(addedList, peer)
257
+ addedMap[s] = struct{}{}
258
+ }
259
+
260
+ // add back original peers. in this order so that we output them.
261
+ for _, s := range bpeers {
262
+ if _, found := addedMap[s]; found {
263
+ continue
264
}
265
+
266
+ cfg.Bootstrap = append(cfg.Bootstrap, s)
267
+ addedMap[s] = struct{}{}
268
}
269
270
if err := r.SetConfig(cfg); err != nil {
271
return nil, err
272
}
273
262
- return added, nil
274
+ return addedList, nil
275
}
276
277
func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []config.BootstrapPeer) ([]config.BootstrapPeer, error) {