From bde95077c7e0175558fc92f11eebb59f8a07c947 Mon Sep 17 00:00:00 2001 From: Overbool Date: Sat, 27 Oct 2018 14:26:43 +0800 Subject: [PATCH 1/2] commands/boostrap: use new cmds License: MIT Signed-off-by: Overbool --- core/commands/bootstrap.go | 234 +++++++++++-------------------------- core/commands/root.go | 2 +- 2 files changed, 72 insertions(+), 164 deletions(-) diff --git a/core/commands/bootstrap.go b/core/commands/bootstrap.go index 5e63b9c1801..9506e9e6417 100644 --- a/core/commands/bootstrap.go +++ b/core/commands/bootstrap.go @@ -1,16 +1,15 @@ 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" + cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds" config "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config" "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) @@ -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, @@ -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) + }), }, } @@ -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) + }), }, } @@ -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) + }), }, } @@ -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) + }), }, } @@ -326,50 +250,34 @@ var bootstrapListCmd = &cmds.Command{ ShortDescription: "Peers are output in the format '/'.", }, - 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")) diff --git a/core/commands/root.go b/core/commands/root.go index 654b0b7279e..b3a54a24900 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -122,7 +122,7 @@ var rootSubcommands = map[string]*cmds.Command{ "pubsub": PubsubCmd, "repo": RepoCmd, "stats": StatsCmd, - "bootstrap": lgc.NewCommand(BootstrapCmd), + "bootstrap": BootstrapCmd, "config": ConfigCmd, "dag": dag.DagCmd, "dht": DhtCmd, From bc4a9ef7c280e377621908343073486aa6dcea3e Mon Sep 17 00:00:00 2001 From: Overbool Date: Wed, 7 Nov 2018 17:11:33 +0800 Subject: [PATCH 2/2] cmds/bootstrap: use EmitOnce directly License: MIT Signed-off-by: Overbool --- core/commands/bootstrap.go | 64 +++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/core/commands/bootstrap.go b/core/commands/bootstrap.go index 9506e9e6417..aa955aa3623 100644 --- a/core/commands/bootstrap.go +++ b/core/commands/bootstrap.go @@ -5,13 +5,13 @@ import ( "io" "sort" - oldcmds "github.com/ipfs/go-ipfs/commands" + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" repo "github.com/ipfs/go-ipfs/repo" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" - cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds" + cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds" config "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config" - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) type BootstrapOutput struct { @@ -75,6 +75,10 @@ in the bootstrap list). inputPeers = defltPeers } else { + if err := req.ParseBodyArgs(); err != nil { + return err + } + parsedPeers, err := config.ParseBootstrapPeers(req.Arguments) if err != nil { return err @@ -87,8 +91,12 @@ in the bootstrap list). return errors.New("no bootstrap peers to add") } - ctx := env.(*oldcmds.Context) - r, err := fsrepo.Open(ctx.ConfigRoot) + cfgRoot, err := cmdenv.GetConfigRoot(env) + if err != nil { + return err + } + + r, err := fsrepo.Open(cfgRoot) if err != nil { return err } @@ -103,7 +111,7 @@ in the bootstrap list). return err } - return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(added)}) + return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(added)}) }, Type: BootstrapOutput{}, Encoders: cmds.EncoderMap{ @@ -125,8 +133,12 @@ in the bootstrap list).`, return err } - ctx := env.(*oldcmds.Context) - r, err := fsrepo.Open(ctx.ConfigRoot) + cfgRoot, err := cmdenv.GetConfigRoot(env) + if err != nil { + return err + } + + r, err := fsrepo.Open(cfgRoot) if err != nil { return err } @@ -142,7 +154,7 @@ in the bootstrap list).`, return err } - return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(added)}) + return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(added)}) }, Type: BootstrapOutput{}, Encoders: cmds.EncoderMap{ @@ -175,8 +187,12 @@ var bootstrapRemoveCmd = &cmds.Command{ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { all, _ := req.Options[bootstrapAllOptionName].(bool) - ctx := env.(*oldcmds.Context) - r, err := fsrepo.Open(ctx.ConfigRoot) + cfgRoot, err := cmdenv.GetConfigRoot(env) + if err != nil { + return err + } + + r, err := fsrepo.Open(cfgRoot) if err != nil { return err } @@ -190,6 +206,10 @@ var bootstrapRemoveCmd = &cmds.Command{ if all { removed, err = bootstrapRemoveAll(r, cfg) } else { + if err := req.ParseBodyArgs(); err != nil { + return err + } + input, perr := config.ParseBootstrapPeers(req.Arguments) if perr != nil { return err @@ -201,7 +221,7 @@ var bootstrapRemoveCmd = &cmds.Command{ return err } - return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(removed)}) + return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(removed)}) }, Type: BootstrapOutput{}, Encoders: cmds.EncoderMap{ @@ -218,8 +238,12 @@ var bootstrapRemoveAllCmd = &cmds.Command{ }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { - ctx := env.(*oldcmds.Context) - r, err := fsrepo.Open(ctx.ConfigRoot) + cfgRoot, err := cmdenv.GetConfigRoot(env) + if err != nil { + return err + } + + r, err := fsrepo.Open(cfgRoot) if err != nil { return err } @@ -234,7 +258,7 @@ var bootstrapRemoveAllCmd = &cmds.Command{ return err } - return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(removed)}) + return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(removed)}) }, Type: BootstrapOutput{}, Encoders: cmds.EncoderMap{ @@ -251,8 +275,12 @@ var bootstrapListCmd = &cmds.Command{ }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { - ctx := env.(*oldcmds.Context) - r, err := fsrepo.Open(ctx.ConfigRoot) + cfgRoot, err := cmdenv.GetConfigRoot(env) + if err != nil { + return err + } + + r, err := fsrepo.Open(cfgRoot) if err != nil { return err } @@ -267,7 +295,7 @@ var bootstrapListCmd = &cmds.Command{ return err } - return res.Emit(&BootstrapOutput{config.BootstrapPeerStrings(peers)}) + return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(peers)}) }, Type: BootstrapOutput{}, Encoders: cmds.EncoderMap{