Skip to content

Commit

Permalink
commands: use EmitOnce for single object
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 31, 2018
1 parent 9bffede commit be67020
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/commands/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Lists running and recently run commands.
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := env.(*oldcmds.Context)
return res.Emit(ctx.ReqLog.Report())
return cmds.EmitOnce(res, ctx.ReqLog.Report())
},
Options: []cmdkit.Option{
cmdkit.BoolOption("verbose", verboseOptionName, "Print extra information."),
Expand Down
4 changes: 2 additions & 2 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ NOTE: For security reasons, this command will omit your private key. If you woul
return err
}

return res.Emit(&cfg)
return cmds.EmitOnce(res, &cfg)
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *map[string]interface{}) error {
Expand Down Expand Up @@ -319,7 +319,7 @@ var configProfileApplyCmd = &cmds.Command{
return err
}

return res.Emit(&ConfigUpdateOutput{
return cmds.EmitOnce(res, &ConfigUpdateOutput{
OldCfg: oldCfgMap,
NewCfg: newCfgMap,
})
Expand Down
5 changes: 3 additions & 2 deletions core/commands/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ the event log.

s := fmt.Sprintf("Changed log level of '%s' to '%s'\n", subsystem, level)
log.Info(s)
return res.Emit(&MessageOutput{s})

return cmds.EmitOnce(res, &MessageOutput{s})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *MessageOutput) error {
Expand All @@ -83,7 +84,7 @@ subsystems of a running daemon.
`,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
return res.Emit(&stringList{logging.GetSubsystems()})
return cmds.EmitOnce(res, &stringList{logging.GetSubsystems()})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, list *stringList) error {
Expand Down
15 changes: 10 additions & 5 deletions core/commands/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

const (
mountIPFSPathOptionName = "ipfs-path"
mountIPNSPathOptionName = "ipns-path"
)

var MountCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Mounts IPFS to the filesystem (read-only).",
Expand Down Expand Up @@ -72,8 +77,8 @@ baz
`,
},
Options: []cmdkit.Option{
cmdkit.StringOption("ipfs-path", "f", "The path where IPFS should be mounted."),
cmdkit.StringOption("ipns-path", "n", "The path where IPNS should be mounted."),
cmdkit.StringOption(mountIPFSPathOptionName, "f", "The path where IPFS should be mounted."),
cmdkit.StringOption(mountIPNSPathOptionName, "n", "The path where IPNS should be mounted."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cfg, err := cmdenv.GetConfig(env)
Expand All @@ -91,13 +96,13 @@ baz
return err
}

fsdir, found := req.Options["f"].(string)
fsdir, found := req.Options[mountIPFSPathOptionName].(string)
if !found {
fsdir = cfg.Mounts.IPFS // use default value
}

// get default mount points
nsdir, found := req.Options["n"].(string)
nsdir, found := req.Options[mountIPNSPathOptionName].(string)
if !found {
nsdir = cfg.Mounts.IPNS // NB: be sure to not redeclare!
}
Expand All @@ -110,7 +115,7 @@ baz
var output config.Mounts
output.IPFS = fsdir
output.IPNS = nsdir
return res.Emit(&output)
return cmds.EmitOnce(res, &output)
},
Type: config.Mounts{},
Encoders: cmds.EncoderMap{
Expand Down
2 changes: 1 addition & 1 deletion core/commands/sysdiag.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Prints out information about your computer to aid in easier debugging.

info["ipfs_version"] = version.CurrentVersionNumber
info["ipfs_commit"] = version.CurrentCommit
return res.Emit(info)
return cmds.EmitOnce(res, info)
},
}

Expand Down

0 comments on commit be67020

Please sign in to comment.