提交 bebc3ca0 作者: Juan Batiz-Benet

cmd/bootstrap: bugfix on add (dedup)

上级 188d3362
...@@ -238,28 +238,40 @@ func bootstrapWritePeers(w io.Writer, prefix string, peers []string) error { ...@@ -238,28 +238,40 @@ func bootstrapWritePeers(w io.Writer, prefix string, peers []string) error {
} }
func bootstrapAdd(r repo.Repo, cfg *config.Config, peers []config.BootstrapPeer) ([]config.BootstrapPeer, error) { func bootstrapAdd(r repo.Repo, cfg *config.Config, peers []config.BootstrapPeer) ([]config.BootstrapPeer, error) {
added := make([]config.BootstrapPeer, 0, len(peers)) addedMap := map[string]struct{}{}
addedList := make([]config.BootstrapPeer, 0, len(peers))
// re-add cfg bootstrap peers to rm dupes
bpeers := cfg.Bootstrap
cfg.Bootstrap = nil
// add new peers
for _, peer := range peers { for _, peer := range peers {
duplicate := false s := peer.String()
for _, peer2 := range cfg.Bootstrap { if _, found := addedMap[s]; found {
if peer.Equal(peer2) { continue
duplicate = true
break
} }
cfg.Bootstrap = append(cfg.Bootstrap, s)
addedList = append(addedList, peer)
addedMap[s] = struct{}{}
} }
if !duplicate { // add back original peers. in this order so that we output them.
cfg.Bootstrap = append(cfg.Bootstrap, peer.String()) for _, s := range bpeers {
added = append(added, peer) if _, found := addedMap[s]; found {
continue
} }
cfg.Bootstrap = append(cfg.Bootstrap, s)
addedMap[s] = struct{}{}
} }
if err := r.SetConfig(cfg); err != nil { if err := r.SetConfig(cfg); err != nil {
return nil, err return nil, err
} }
return added, nil return addedList, nil
} }
func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []config.BootstrapPeer) ([]config.BootstrapPeer, error) { func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []config.BootstrapPeer) ([]config.BootstrapPeer, error) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论