Commit 89ec480e authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

cmds2: bootstrap command fix add

parent 047d2e2d
package commands package commands
import ( import (
"fmt" "bytes"
"io"
"strings" "strings"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" 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'. ...@@ -38,7 +39,7 @@ Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
Subcommands: map[string]*cmds.Command{ Subcommands: map[string]*cmds.Command{
"list": bootstrapListCmd, "list": bootstrapListCmd,
"add": bootstrapAddCmd, "add": bootstrapAddCmd,
"remove": bootstrapRemoveCmd, "rm": bootstrapRemoveCmd,
}, },
} }
...@@ -79,13 +80,14 @@ in the bootstrap list). ...@@ -79,13 +80,14 @@ in the bootstrap list).
Type: &BootstrapOutput{}, Type: &BootstrapOutput{},
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) ([]byte, error) { cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*BootstrapOutput) v, ok := res.Output().(*BootstrapOutput)
s := fmt.Sprintf("Added %v peers to the bootstrap list:\n", len(v.Peers)) if !ok {
marshalled, err := bootstrapMarshaler(res) return nil, u.ErrCast()
if err != nil {
return nil, err
} }
return append([]byte(s), marshalled...), nil
var buf bytes.Buffer
err := bootstrapWritePeers(&buf, "added ", v.Peers)
return buf.Bytes(), err
}, },
}, },
} }
...@@ -126,13 +128,14 @@ var bootstrapRemoveCmd = &cmds.Command{ ...@@ -126,13 +128,14 @@ var bootstrapRemoveCmd = &cmds.Command{
Type: &BootstrapOutput{}, Type: &BootstrapOutput{},
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) ([]byte, error) { cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*BootstrapOutput) v, ok := res.Output().(*BootstrapOutput)
s := fmt.Sprintf("Removed %v peers from the bootstrap list:\n", len(v.Peers)) if !ok {
marshalled, err := bootstrapMarshaler(res) return nil, u.ErrCast()
if err != nil {
return nil, err
} }
return append([]byte(s), marshalled...), nil
var buf bytes.Buffer
err := bootstrapWritePeers(&buf, "removed ", v.Peers)
return buf.Bytes(), err
}, },
}, },
} }
...@@ -164,15 +167,33 @@ func bootstrapMarshaler(res cmds.Response) ([]byte, error) { ...@@ -164,15 +167,33 @@ func bootstrapMarshaler(res cmds.Response) ([]byte, error) {
return nil, u.ErrCast() return nil, u.ErrCast()
} }
s := "" var buf bytes.Buffer
for _, peer := range v.Peers { err := bootstrapWritePeers(&buf, "", v.Peers)
s += fmt.Sprintf("%s/%s\n", peer.Address, peer.PeerID) return buf.Bytes(), err
} }
func bootstrapWritePeers(w io.Writer, prefix string, peers []*config.BootstrapPeer) error {
return []byte(s), nil for _, peer := range peers {
s := prefix + peer.Address + "/" + peer.PeerID + "\n"
_, err := w.Write([]byte(s))
if err != nil {
return err
}
}
return nil
} }
func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error) { func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error) {
inputAddrs := make([]string, len(input))
for i, v := range input {
addr, ok := v.(string)
if !ok {
return nil, u.ErrCast()
}
inputAddrs[i] = addr
}
split := func(addr string) (string, string) { split := func(addr string) (string, string) {
idx := strings.LastIndex(addr, "/") idx := strings.LastIndex(addr, "/")
if idx == -1 { if idx == -1 {
...@@ -182,12 +203,7 @@ func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error) ...@@ -182,12 +203,7 @@ func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error)
} }
peers := []*config.BootstrapPeer{} peers := []*config.BootstrapPeer{}
for _, v := range input { for _, addr := range inputAddrs {
addr, ok := v.(string)
if !ok {
return nil, u.ErrCast()
}
addrS, peeridS := split(addr) addrS, peeridS := split(addr)
// make sure addrS parses as a multiaddr. // make sure addrS parses as a multiaddr.
...@@ -221,7 +237,7 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []*config.Bootstrap ...@@ -221,7 +237,7 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []*config.Bootstrap
for _, peer := range peers { for _, peer := range peers {
duplicate := false duplicate := false
for _, peer2 := range cfg.Bootstrap { for _, peer2 := range cfg.Bootstrap {
if peer.Address == peer2.Address { if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID {
duplicate = true duplicate = true
break break
} }
...@@ -241,13 +257,13 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []*config.Bootstrap ...@@ -241,13 +257,13 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []*config.Bootstrap
return added, nil return added, nil
} }
func bootstrapRemove(filename string, cfg *config.Config, peers []*config.BootstrapPeer) ([]*config.BootstrapPeer, error) { func bootstrapRemove(filename string, cfg *config.Config, toRemove []*config.BootstrapPeer) ([]*config.BootstrapPeer, error) {
removed := make([]*config.BootstrapPeer, 0, len(peers)) removed := make([]*config.BootstrapPeer, 0, len(toRemove))
keep := make([]*config.BootstrapPeer, 0, len(cfg.Bootstrap)) keep := make([]*config.BootstrapPeer, 0, len(cfg.Bootstrap))
for _, peer := range cfg.Bootstrap { for _, peer := range cfg.Bootstrap {
found := false found := false
for _, peer2 := range peers { for _, peer2 := range toRemove {
if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID { if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID {
found = true found = true
removed = append(removed, peer) removed = append(removed, peer)
......
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