commands/ping: use new cmds lib
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Oct 27, 2018 at 12:37 UTC
cec3af28ca08d8b9434e48f867d2fd056cb11464
2 files changed
+28
-49
core/commands/ping.go
+27
-48
@@ -1,7 +1,6 @@
1
package commands
2
3
import (
4
- "bytes"
4
"context"
5
"errors"
6
"fmt"
@@ -9,14 +8,14 @@ import (
8
"strings"
9
"time"
10
12
- cmds "github.com/ipfs/go-ipfs/commands"
11
"github.com/ipfs/go-ipfs/core"
12
+ "github.com/ipfs/go-ipfs/core/commands/cmdenv"
13
15
- u "gx/ipfs/QmPdKqUcHGFdeSpvjVoaTRPPstGif9GBZb5Q56RVw9o69A/go-ipfs-util"
14
+ cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds"
15
ma "gx/ipfs/QmT4U94DnD8FRfqr21obWY32HLM5VExccPKMjQHofeYqr9/go-multiaddr"
16
"gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer"
17
pstore "gx/ipfs/QmTTJcDL3gsnGDALjh2fDGg1onGRUdVgNL2hU2WEZcVrMX/go-libp2p-peerstore"
19
- "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
18
+ cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
19
)
20
21
const kPingTimeout = 10 * time.Second
@@ -49,72 +48,52 @@ trip latency information.
48
Options: []cmdkit.Option{
49
cmdkit.IntOption(pingCountOptionName, "n", "Number of ping messages to send.").WithDefault(10),
50
},
52
- Marshalers: cmds.MarshalerMap{
53
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
54
- v, err := unwrapOutput(res.Output())
55
- if err != nil {
56
- return nil, err
57
- }
58
-
59
- obj, ok := v.(*PingResult)
60
- if !ok {
61
- return nil, u.ErrCast()
62
- }
63
-
64
- buf := new(bytes.Buffer)
65
- if len(obj.Text) > 0 {
66
- buf = bytes.NewBufferString(obj.Text + "\n")
67
- } else if obj.Success {
68
- fmt.Fprintf(buf, "Pong received: time=%.2f ms\n", obj.Time.Seconds()*1000)
69
- } else {
70
- fmt.Fprintf(buf, "Pong failed\n")
71
- }
72
- return buf, nil
73
- },
74
- },
75
- Run: func(req cmds.Request, res cmds.Response) {
76
- ctx := req.Context()
77
- n, err := req.InvocContext().GetNode()
51
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
52
+ n, err := cmdenv.GetNode(env)
53
if err != nil {
79
- res.SetError(err, cmdkit.ErrNormal)
80
- return
54
+ return err
55
}
56
57
// Must be online!
58
if !n.OnlineMode() {
85
- res.SetError(ErrNotOnline, cmdkit.ErrClient)
86
- return
59
+ return ErrNotOnline
60
}
61
89
- addr, peerID, err := ParsePeerParam(req.Arguments()[0])
62
+ addr, peerID, err := ParsePeerParam(req.Arguments[0])
63
if err != nil {
91
- res.SetError(fmt.Errorf("failed to parse peer address '%s': %s", req.Arguments()[0], err), cmdkit.ErrNormal)
92
- return
64
+ return fmt.Errorf("failed to parse peer address '%s': %s", req.Arguments[0], err)
65
}
66
67
if peerID == n.Identity {
96
- res.SetError(ErrPingSelf, cmdkit.ErrNormal)
97
- return
68
+ return ErrPingSelf
69
}
70
71
if addr != nil {
72
n.Peerstore.AddAddr(peerID, addr, pstore.TempAddrTTL) // temporary
73
}
74
104
- numPings, _, err := req.Option(pingCountOptionName).Int()
105
- if err != nil {
106
- res.SetError(err, cmdkit.ErrNormal)
107
- return
108
- }
109
-
75
+ numPings, _ := req.Options[pingCountOptionName].(int)
76
if numPings <= 0 {
111
- res.SetError(fmt.Errorf("error: ping count must be greater than 0, was %d", numPings), cmdkit.ErrNormal)
77
+ return fmt.Errorf("error: ping count must be greater than 0, was %d", numPings)
78
}
79
114
- outChan := pingPeer(ctx, n, peerID, numPings)
115
- res.SetOutput(outChan)
80
+ outChan := pingPeer(req.Context, n, peerID, numPings)
81
+
82
+ return res.Emit(outChan)
83
},
84
Type: PingResult{},
85
+ Encoders: cmds.EncoderMap{
86
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PingResult) error {
87
+ if len(out.Text) > 0 {
88
+ fmt.Fprintln(w, out.Text)
89
+ } else if out.Success {
90
+ fmt.Fprintf(w, "Pong received: time=%.2f ms\n", out.Time.Seconds()*1000)
91
+ } else {
92
+ fmt.Fprintf(w, "Pong failed\n")
93
+ }
94
+ return nil
95
+ }),
96
+ },
97
}
98
99
func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int) <-chan interface{} {
core/commands/root.go
+1
-1
@@ -136,7 +136,7 @@ var rootSubcommands = map[string]*cmds.Command{
136
"name": name.NameCmd,
137
"object": ocmd.ObjectCmd,
138
"pin": lgc.NewCommand(PinCmd),
139
- "ping": lgc.NewCommand(PingCmd),
139
+ "ping": PingCmd,
140
"p2p": lgc.NewCommand(P2PCmd),
141
"refs": lgc.NewCommand(RefsCmd),
142
"resolve": ResolveCmd,