Skip to content

Commit

Permalink
commands/boostrap: use new cmds
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Oct 27, 2018
1 parent f65a5ea commit 25cb875
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 165 deletions.
236 changes: 72 additions & 164 deletions core/commands/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package commands

import (
"bytes"
"errors"
"io"
"sort"

cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
oldcmds "github.com/ipfs/go-ipfs/commands"
repo "github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/repo/fsrepo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

config "gx/ipfs/QmPEpj17FDRpc7K1aArKZp3RsHtzRMKykeK9GVgn4WQGPR/go-ipfs-config"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

type BootstrapOutput struct {
Expand All @@ -29,9 +28,9 @@ Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
` + bootstrapSecurityWarning,
},

Run: bootstrapListCmd.Run,
Marshalers: bootstrapListCmd.Marshalers,
Type: bootstrapListCmd.Type,
Run: bootstrapListCmd.Run,
Encoders: bootstrapListCmd.Encoders,
Type: bootstrapListCmd.Type,

Subcommands: map[string]*cmds.Command{
"list": bootstrapListCmd,
Expand Down Expand Up @@ -63,78 +62,54 @@ in the bootstrap list).
"default": bootstrapAddDefaultCmd,
},

Run: func(req cmds.Request, res cmds.Response) {
deflt, _, err := req.Option(defaultOptionName).Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
deflt, _ := req.Options[defaultOptionName].(bool)

var inputPeers []config.BootstrapPeer
if deflt {
// parse separately for meaningful, correct error.
defltPeers, err := config.DefaultBootstrapPeers()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

inputPeers = defltPeers
} else {
parsedPeers, err := config.ParseBootstrapPeers(req.Arguments())
parsedPeers, err := config.ParseBootstrapPeers(req.Arguments)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

inputPeers = parsedPeers
}

if len(inputPeers) == 0 {
res.SetError(errors.New("no bootstrap peers to add"), cmdkit.ErrClient)
return
return errors.New("no bootstrap peers to add")
}

r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
ctx := env.(*oldcmds.Context)
r, err := fsrepo.Open(ctx.ConfigRoot)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}
defer r.Close()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

added, err := bootstrapAdd(r, cfg, inputPeers)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

res.SetOutput(&BootstrapOutput{config.BootstrapPeerStrings(added)})
return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(added)})
},
Type: BootstrapOutput{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}

out, ok := v.(*BootstrapOutput)
if !ok {
return nil, e.TypeErr(out, v)
}

buf := new(bytes.Buffer)
if err := bootstrapWritePeers(buf, "added ", out.Peers); err != nil {
return nil, err
}

return buf, nil
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *BootstrapOutput) error {
return bootstrapWritePeers(w, "added ", out.Peers)
}),
},
}

Expand All @@ -144,54 +119,36 @@ var bootstrapAddDefaultCmd = &cmds.Command{
ShortDescription: `Outputs a list of peers that were added (that weren't already
in the bootstrap list).`,
},
Run: func(req cmds.Request, res cmds.Response) {
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
defltPeers, err := config.DefaultBootstrapPeers()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
ctx := env.(*oldcmds.Context)
r, err := fsrepo.Open(ctx.ConfigRoot)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

defer r.Close()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

added, err := bootstrapAdd(r, cfg, defltPeers)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

res.SetOutput(&BootstrapOutput{config.BootstrapPeerStrings(added)})
return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(added)})
},
Type: BootstrapOutput{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}

out, ok := v.(*BootstrapOutput)
if !ok {
return nil, e.TypeErr(out, v)
}

buf := new(bytes.Buffer)
if err := bootstrapWritePeers(buf, "added ", out.Peers); err != nil {
return nil, err
}

return buf, nil
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *BootstrapOutput) error {
return bootstrapWritePeers(w, "added ", out.Peers)
}),
},
}

Expand All @@ -215,61 +172,42 @@ var bootstrapRemoveCmd = &cmds.Command{
Subcommands: map[string]*cmds.Command{
"all": bootstrapRemoveAllCmd,
},
Run: func(req cmds.Request, res cmds.Response) {
all, _, err := req.Option(bootstrapAllOptionName).Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
all, _ := req.Options[bootstrapAllOptionName].(bool)

r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
ctx := env.(*oldcmds.Context)
r, err := fsrepo.Open(ctx.ConfigRoot)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}
defer r.Close()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

var removed []config.BootstrapPeer
if all {
removed, err = bootstrapRemoveAll(r, cfg)
} else {
input, perr := config.ParseBootstrapPeers(req.Arguments())
input, perr := config.ParseBootstrapPeers(req.Arguments)
if perr != nil {
res.SetError(perr, cmdkit.ErrNormal)
return
return err
}

removed, err = bootstrapRemove(r, cfg, input)
}
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

res.SetOutput(&BootstrapOutput{config.BootstrapPeerStrings(removed)})
return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(removed)})
},
Type: BootstrapOutput{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}

out, ok := v.(*BootstrapOutput)
if !ok {
return nil, e.TypeErr(out, v)
}

buf := new(bytes.Buffer)
err = bootstrapWritePeers(buf, "removed ", out.Peers)
return buf, err
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *BootstrapOutput) error {
return bootstrapWritePeers(w, "removed ", out.Peers)
}),
},
}

Expand All @@ -279,44 +217,30 @@ var bootstrapRemoveAllCmd = &cmds.Command{
ShortDescription: `Outputs the list of peers that were removed.`,
},

Run: func(req cmds.Request, res cmds.Response) {
r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := env.(*oldcmds.Context)
r, err := fsrepo.Open(ctx.ConfigRoot)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}
defer r.Close()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

removed, err := bootstrapRemoveAll(r, cfg)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

res.SetOutput(&BootstrapOutput{config.BootstrapPeerStrings(removed)})
return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(removed)})
},
Type: BootstrapOutput{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}

out, ok := v.(*BootstrapOutput)
if !ok {
return nil, e.TypeErr(out, v)
}

buf := new(bytes.Buffer)
err = bootstrapWritePeers(buf, "removed ", out.Peers)
return buf, err
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *BootstrapOutput) error {
return bootstrapWritePeers(w, "removed ", out.Peers)
}),
},
}

Expand All @@ -326,50 +250,34 @@ var bootstrapListCmd = &cmds.Command{
ShortDescription: "Peers are output in the format '<multiaddr>/<peerID>'.",
},

Run: func(req cmds.Request, res cmds.Response) {
r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := env.(*oldcmds.Context)
r, err := fsrepo.Open(ctx.ConfigRoot)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}
defer r.Close()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

peers, err := cfg.BootstrapPeers()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}
res.SetOutput(&BootstrapOutput{config.BootstrapPeerStrings(peers)})

return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(peers)})
},
Type: BootstrapOutput{},
Marshalers: cmds.MarshalerMap{
cmds.Text: bootstrapMarshaler,
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *BootstrapOutput) error {
return bootstrapWritePeers(w, "", out.Peers)
}),
},
}

func bootstrapMarshaler(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}

out, ok := v.(*BootstrapOutput)
if !ok {
return nil, e.TypeErr(out, v)
}

buf := new(bytes.Buffer)
err = bootstrapWritePeers(buf, "", out.Peers)
return buf, err
}

func bootstrapWritePeers(w io.Writer, prefix string, peers []string) error {

sort.Stable(sort.StringSlice(peers))
for _, peer := range peers {
_, err := w.Write([]byte(prefix + peer + "\n"))
Expand Down
Loading

0 comments on commit 25cb875

Please sign in to comment.