提交 43d93cb0 作者: Kejie Zhang 提交者: Steven Allen

refactor p2p command code

License: MIT
Signed-off-by: 's avatarKejie Zhang <601172892@qq.com>
上级 05b3b817
...@@ -49,6 +49,14 @@ type P2PStreamsOutput struct { ...@@ -49,6 +49,14 @@ type P2PStreamsOutput struct {
Streams []P2PStreamInfoOutput Streams []P2PStreamInfoOutput
} }
var (
allowCustomProtocolOptionName = "allow-custom-protocol"
allOptionName = "all"
protocolOptionName = "protocol"
listenAddressOptionName = "listen-address"
targetAddressOptionName = "target-address"
)
// P2PCmd is the 'ipfs p2p' command // P2PCmd is the 'ipfs p2p' command
var P2PCmd = &cmds.Command{ var P2PCmd = &cmds.Command{
Helptext: cmdkit.HelpText{ Helptext: cmdkit.HelpText{
...@@ -91,7 +99,7 @@ Example: ...@@ -91,7 +99,7 @@ Example:
cmdkit.StringArg("target-address", true, false, "Target endpoint."), cmdkit.StringArg("target-address", true, false, "Target endpoint."),
}, },
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.BoolOption("allow-custom-protocol", "Don't require /x/ prefix"), cmdkit.BoolOption(allowCustomProtocolOptionName, "Don't require /x/ prefix"),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
n, err := p2pGetNode(req) n, err := p2pGetNode(req)
...@@ -118,7 +126,7 @@ Example: ...@@ -118,7 +126,7 @@ Example:
return return
} }
allowCustom, _, err := req.Option("allow-custom-protocol").Bool() allowCustom, _, err := req.Option(allowCustomProtocolOptionName).Bool()
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
return return
...@@ -156,7 +164,7 @@ Example: ...@@ -156,7 +164,7 @@ Example:
cmdkit.StringArg("target-address", true, false, "Target endpoint."), cmdkit.StringArg("target-address", true, false, "Target endpoint."),
}, },
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.BoolOption("allow-custom-protocol", "Don't require /x/ prefix"), cmdkit.BoolOption(allowCustomProtocolOptionName, "Don't require /x/ prefix"),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
n, err := p2pGetNode(req) n, err := p2pGetNode(req)
...@@ -176,7 +184,7 @@ Example: ...@@ -176,7 +184,7 @@ Example:
return return
} }
allowCustom, _, err := req.Option("allow-custom-protocol").Bool() allowCustom, _, err := req.Option(allowCustomProtocolOptionName).Bool()
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
return return
...@@ -283,10 +291,10 @@ var p2pCloseCmd = &cmds.Command{ ...@@ -283,10 +291,10 @@ var p2pCloseCmd = &cmds.Command{
Tagline: "Stop listening for new connections to forward.", Tagline: "Stop listening for new connections to forward.",
}, },
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.BoolOption("all", "a", "Close all listeners."), cmdkit.BoolOption(allOptionName, "a", "Close all listeners."),
cmdkit.StringOption("protocol", "p", "Match protocol name"), cmdkit.StringOption(protocolOptionName, "p", "Match protocol name"),
cmdkit.StringOption("listen-address", "l", "Match listen address"), cmdkit.StringOption(listenAddressOptionName, "l", "Match listen address"),
cmdkit.StringOption("target-address", "t", "Match target address"), cmdkit.StringOption(targetAddressOptionName, "t", "Match target address"),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
n, err := p2pGetNode(req) n, err := p2pGetNode(req)
...@@ -295,10 +303,10 @@ var p2pCloseCmd = &cmds.Command{ ...@@ -295,10 +303,10 @@ var p2pCloseCmd = &cmds.Command{
return return
} }
closeAll, _, _ := req.Option("all").Bool() closeAll, _, _ := req.Option(allOptionName).Bool()
protoOpt, p, _ := req.Option("protocol").String() protoOpt, p, _ := req.Option(protocolOptionName).String()
listenOpt, l, _ := req.Option("listen-address").String() listenOpt, l, _ := req.Option(listenAddressOptionName).String()
targetOpt, t, _ := req.Option("target-address").String() targetOpt, t, _ := req.Option(targetAddressOptionName).String()
proto := protocol.ID(protoOpt) proto := protocol.ID(protoOpt)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论