1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-03 01:38:00 +08:00
Files
kubo/commands/legacy/legacy.go
Steven Allen 3eba14aa24 gx update
Updates:

* go-kad-dht: Query performance improvements, DHT client fixes, validates
  records on *local* put.
* go-libp2p-swarm/go-libp2p-transport: Timeout improvements.
* go-multiaddr-net: Exposes useful Conn methods (CloseWrite, CloseRead, etc.)
* go-log: fixes possible panic when enabling/disabling events.
* go-multiaddr: fixes possible panic when stringifying malformed multiaddrs,
  adds support for consuming /p2p/ multiaddrs.

fixes #5113
unblocks #4895

License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
2018-06-26 17:11:33 -07:00

58 lines
1.2 KiB
Go

package legacy
import (
"io"
"runtime/debug"
"gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
oldcmds "github.com/ipfs/go-ipfs/commands"
)
// MarshalerEncoder implements Encoder from a Marshaler
type MarshalerEncoder struct {
m oldcmds.Marshaler
w io.Writer
req *cmds.Request
}
// NewMarshalerEncoder returns a new MarshalerEncoder
func NewMarshalerEncoder(req *cmds.Request, m oldcmds.Marshaler, w io.Writer) *MarshalerEncoder {
me := &MarshalerEncoder{
m: m,
w: w,
req: req,
}
return me
}
// Encode encodes v onto the io.Writer w using Marshaler m, with both m and w passed in NewMarshalerEncoder
func (me *MarshalerEncoder) Encode(v interface{}) error {
re, res := cmds.NewChanResponsePair(me.req)
go re.Emit(v)
r, err := me.m(&responseWrapper{Response: res})
if err != nil {
return err
}
if r == nil {
// behave like empty reader
return nil
}
_, err = io.Copy(me.w, r)
return err
}
// OldContext tries to cast the environment as a legacy command context,
// returning nil on failure.
func OldContext(env interface{}) *oldcmds.Context {
ctx, ok := env.(*oldcmds.Context)
if !ok {
log.Errorf("OldContext: env passed is not %T but %T\n%s", ctx, env, debug.Stack())
}
return ctx
}