From cec3af28ca08d8b9434e48f867d2fd056cb11464 Mon Sep 17 00:00:00 2001 From: Overbool Date: Sat, 27 Oct 2018 12:37:10 +0800 Subject: [PATCH] commands/ping: use new cmds lib License: MIT Signed-off-by: Overbool --- core/commands/ping.go | 75 ++++++++++++++++--------------------------- core/commands/root.go | 2 +- 2 files changed, 28 insertions(+), 49 deletions(-) diff --git a/core/commands/ping.go b/core/commands/ping.go index 8234b7b1fc7..6383af77666 100644 --- a/core/commands/ping.go +++ b/core/commands/ping.go @@ -1,7 +1,6 @@ package commands import ( - "bytes" "context" "errors" "fmt" @@ -9,14 +8,14 @@ import ( "strings" "time" - cmds "github.com/ipfs/go-ipfs/commands" "github.com/ipfs/go-ipfs/core" + "github.com/ipfs/go-ipfs/core/commands/cmdenv" - u "gx/ipfs/QmPdKqUcHGFdeSpvjVoaTRPPstGif9GBZb5Q56RVw9o69A/go-ipfs-util" + cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds" ma "gx/ipfs/QmT4U94DnD8FRfqr21obWY32HLM5VExccPKMjQHofeYqr9/go-multiaddr" "gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer" pstore "gx/ipfs/QmTTJcDL3gsnGDALjh2fDGg1onGRUdVgNL2hU2WEZcVrMX/go-libp2p-peerstore" - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) const kPingTimeout = 10 * time.Second @@ -49,72 +48,52 @@ trip latency information. Options: []cmdkit.Option{ cmdkit.IntOption(pingCountOptionName, "n", "Number of ping messages to send.").WithDefault(10), }, - Marshalers: cmds.MarshalerMap{ - cmds.Text: func(res cmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - obj, ok := v.(*PingResult) - if !ok { - return nil, u.ErrCast() - } - - buf := new(bytes.Buffer) - if len(obj.Text) > 0 { - buf = bytes.NewBufferString(obj.Text + "\n") - } else if obj.Success { - fmt.Fprintf(buf, "Pong received: time=%.2f ms\n", obj.Time.Seconds()*1000) - } else { - fmt.Fprintf(buf, "Pong failed\n") - } - return buf, nil - }, - }, - Run: func(req cmds.Request, res cmds.Response) { - ctx := req.Context() - n, err := req.InvocContext().GetNode() + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + n, err := cmdenv.GetNode(env) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } // Must be online! if !n.OnlineMode() { - res.SetError(ErrNotOnline, cmdkit.ErrClient) - return + return ErrNotOnline } - addr, peerID, err := ParsePeerParam(req.Arguments()[0]) + addr, peerID, err := ParsePeerParam(req.Arguments[0]) if err != nil { - res.SetError(fmt.Errorf("failed to parse peer address '%s': %s", req.Arguments()[0], err), cmdkit.ErrNormal) - return + return fmt.Errorf("failed to parse peer address '%s': %s", req.Arguments[0], err) } if peerID == n.Identity { - res.SetError(ErrPingSelf, cmdkit.ErrNormal) - return + return ErrPingSelf } if addr != nil { n.Peerstore.AddAddr(peerID, addr, pstore.TempAddrTTL) // temporary } - numPings, _, err := req.Option(pingCountOptionName).Int() - if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return - } - + numPings, _ := req.Options[pingCountOptionName].(int) if numPings <= 0 { - res.SetError(fmt.Errorf("error: ping count must be greater than 0, was %d", numPings), cmdkit.ErrNormal) + return fmt.Errorf("error: ping count must be greater than 0, was %d", numPings) } - outChan := pingPeer(ctx, n, peerID, numPings) - res.SetOutput(outChan) + outChan := pingPeer(req.Context, n, peerID, numPings) + + return res.Emit(outChan) }, Type: PingResult{}, + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PingResult) error { + if len(out.Text) > 0 { + fmt.Fprintln(w, out.Text) + } else if out.Success { + fmt.Fprintf(w, "Pong received: time=%.2f ms\n", out.Time.Seconds()*1000) + } else { + fmt.Fprintf(w, "Pong failed\n") + } + return nil + }), + }, } func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int) <-chan interface{} { diff --git a/core/commands/root.go b/core/commands/root.go index 5013d39de30..9ddd227ed6c 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -136,7 +136,7 @@ var rootSubcommands = map[string]*cmds.Command{ "name": name.NameCmd, "object": ocmd.ObjectCmd, "pin": lgc.NewCommand(PinCmd), - "ping": lgc.NewCommand(PingCmd), + "ping": PingCmd, "p2p": lgc.NewCommand(P2PCmd), "refs": lgc.NewCommand(RefsCmd), "resolve": ResolveCmd,