Skip to content

Commit

Permalink
cmds/bootstrap: use EmitOnce directly
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 Nov 7, 2018
1 parent bde9507 commit 6ee5b09
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions core/commands/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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{
Expand All @@ -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
}
Expand All @@ -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{
Expand Down Expand Up @@ -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
}
Expand All @@ -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{
Expand All @@ -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
}
Expand All @@ -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{
Expand All @@ -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
}
Expand All @@ -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{
Expand Down

0 comments on commit 6ee5b09

Please sign in to comment.