/
legacy.go
57 lines (46 loc) · 1.23 KB
/
legacy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package legacy
import (
"io"
"runtime/debug"
"gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/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
}