@cryptotaxi247 / kubo / commits / 43d93cb01

refactor p2p command code

License: MIT Signed-off-by: Kejie Zhang <601172892@qq.com>

Kejie Zhang committed Sep 27, 2018 at 19:53 UTC 43d93cb01bbff95fabbaa2582649256f42ffe328
1 file changed +20 -12
core/commands/p2p.go
+20 -12
@@ -49,6 +49,14 @@ type P2PStreamsOutput struct {
49 Streams []P2PStreamInfoOutput
50 }
51
52 +var (
53 + allowCustomProtocolOptionName = "allow-custom-protocol"
54 + allOptionName = "all"
55 + protocolOptionName = "protocol"
56 + listenAddressOptionName = "listen-address"
57 + targetAddressOptionName = "target-address"
58 +)
59 +
60 // P2PCmd is the 'ipfs p2p' command
61 var P2PCmd = &cmds.Command{
62 Helptext: cmdkit.HelpText{
@@ -91,7 +99,7 @@ Example:
99 cmdkit.StringArg("target-address", true, false, "Target endpoint."),
100 },
101 Options: []cmdkit.Option{
94 - cmdkit.BoolOption("allow-custom-protocol", "Don't require /x/ prefix"),
102 + cmdkit.BoolOption(allowCustomProtocolOptionName, "Don't require /x/ prefix"),
103 },
104 Run: func(req cmds.Request, res cmds.Response) {
105 n, err := p2pGetNode(req)
@@ -118,7 +126,7 @@ Example:
126 return
127 }
128
121 - allowCustom, _, err := req.Option("allow-custom-protocol").Bool()
129 + allowCustom, _, err := req.Option(allowCustomProtocolOptionName).Bool()
130 if err != nil {
131 res.SetError(err, cmdkit.ErrNormal)
132 return
@@ -156,7 +164,7 @@ Example:
164 cmdkit.StringArg("target-address", true, false, "Target endpoint."),
165 },
166 Options: []cmdkit.Option{
159 - cmdkit.BoolOption("allow-custom-protocol", "Don't require /x/ prefix"),
167 + cmdkit.BoolOption(allowCustomProtocolOptionName, "Don't require /x/ prefix"),
168 },
169 Run: func(req cmds.Request, res cmds.Response) {
170 n, err := p2pGetNode(req)
@@ -176,7 +184,7 @@ Example:
184 return
185 }
186
179 - allowCustom, _, err := req.Option("allow-custom-protocol").Bool()
187 + allowCustom, _, err := req.Option(allowCustomProtocolOptionName).Bool()
188 if err != nil {
189 res.SetError(err, cmdkit.ErrNormal)
190 return
@@ -283,10 +291,10 @@ var p2pCloseCmd = &cmds.Command{
291 Tagline: "Stop listening for new connections to forward.",
292 },
293 Options: []cmdkit.Option{
286 - cmdkit.BoolOption("all", "a", "Close all listeners."),
287 - cmdkit.StringOption("protocol", "p", "Match protocol name"),
288 - cmdkit.StringOption("listen-address", "l", "Match listen address"),
289 - cmdkit.StringOption("target-address", "t", "Match target address"),
294 + cmdkit.BoolOption(allOptionName, "a", "Close all listeners."),
295 + cmdkit.StringOption(protocolOptionName, "p", "Match protocol name"),
296 + cmdkit.StringOption(listenAddressOptionName, "l", "Match listen address"),
297 + cmdkit.StringOption(targetAddressOptionName, "t", "Match target address"),
298 },
299 Run: func(req cmds.Request, res cmds.Response) {
300 n, err := p2pGetNode(req)
@@ -295,10 +303,10 @@ var p2pCloseCmd = &cmds.Command{
303 return
304 }
305
298 - closeAll, _, _ := req.Option("all").Bool()
299 - protoOpt, p, _ := req.Option("protocol").String()
300 - listenOpt, l, _ := req.Option("listen-address").String()
301 - targetOpt, t, _ := req.Option("target-address").String()
306 + closeAll, _, _ := req.Option(allOptionName).Bool()
307 + protoOpt, p, _ := req.Option(protocolOptionName).String()
308 + listenOpt, l, _ := req.Option(listenAddressOptionName).String()
309 + targetOpt, t, _ := req.Option(targetAddressOptionName).String()
310
311 proto := protocol.ID(protoOpt)
312