From 6ee5b0954fbcd437430f0fb3d0ad917d67e6707b Mon Sep 17 00:00:00 2001 From: Overbool Date: Wed, 7 Nov 2018 17:11:33 +0800 Subject: [PATCH] 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 9506e9e64172..e6ed4d2e9a8c 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 { @@ -63,6 +63,10 @@ in the bootstrap list). }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + if err := req.ParseBodyArgs(); err != nil { + return err + } + deflt, _ := req.Options[defaultOptionName].(bool) var inputPeers []config.BootstrapPeer @@ -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{ @@ -173,10 +185,18 @@ var bootstrapRemoveCmd = &cmds.Command{ "all": bootstrapRemoveAllCmd, }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + if err := req.ParseBodyArgs(); err != nil { + return err + } + 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 } @@ -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{