Skip to content

Commit

Permalink
refactor p2p command code
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kejie Zhang <601172892@qq.com>
  • Loading branch information
kjzz committed Sep 27, 2018
1 parent 1e0d53f commit 2a53f1d
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions core/commands/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ type P2PStreamsOutput struct {
Streams []P2PStreamInfoOutput
}

var (
allowCustomProtocolOptionName = "allow-custom-protocol"
allOptionName = "all"
protocolOptionName = "protocol"
listenAddressOptionName = "listen-address"
targetAddressOptionName = "target-address"
)

// P2PCmd is the 'ipfs p2p' command
var P2PCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Expand Down Expand Up @@ -91,7 +99,7 @@ Example:
cmdkit.StringArg("target-address", true, false, "Target endpoint."),
},
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) {
n, err := p2pGetNode(req)
Expand All @@ -118,7 +126,7 @@ Example:
return
}

allowCustom, _, err := req.Option("allow-custom-protocol").Bool()
allowCustom, _, err := req.Option(allowCustomProtocolOptionName).Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -156,7 +164,7 @@ Example:
cmdkit.StringArg("target-address", true, false, "Target endpoint."),
},
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) {
n, err := p2pGetNode(req)
Expand All @@ -176,7 +184,7 @@ Example:
return
}

allowCustom, _, err := req.Option("allow-custom-protocol").Bool()
allowCustom, _, err := req.Option(allowCustomProtocolOptionName).Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -283,10 +291,10 @@ var p2pCloseCmd = &cmds.Command{
Tagline: "Stop listening for new connections to forward.",
},
Options: []cmdkit.Option{
cmdkit.BoolOption("all", "a", "Close all listeners."),
cmdkit.StringOption("protocol", "p", "Match protocol name"),
cmdkit.StringOption("listen-address", "l", "Match listen address"),
cmdkit.StringOption("target-address", "t", "Match target address"),
cmdkit.BoolOption(allOptionName, "a", "Close all listeners."),
cmdkit.StringOption(protocolOptionName, "p", "Match protocol name"),
cmdkit.StringOption(listenAddressOptionName, "l", "Match listen address"),
cmdkit.StringOption(targetAddressOptionName, "t", "Match target address"),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := p2pGetNode(req)
Expand All @@ -295,10 +303,10 @@ var p2pCloseCmd = &cmds.Command{
return
}

closeAll, _, _ := req.Option("all").Bool()
protoOpt, p, _ := req.Option("protocol").String()
listenOpt, l, _ := req.Option("listen-address").String()
targetOpt, t, _ := req.Option("target-address").String()
closeAll, _, _ := req.Option(allOptionName).Bool()
protoOpt, p, _ := req.Option(protocolOptionName).String()
listenOpt, l, _ := req.Option(listenAddressOptionName).String()
targetOpt, t, _ := req.Option(targetAddressOptionName).String()

proto := protocol.ID(protoOpt)

Expand Down

0 comments on commit 2a53f1d

Please sign in to comment.