forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
70 lines (56 loc) · 1.71 KB
/
command.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
58
59
60
61
62
63
64
65
66
67
68
69
70
package legacy
import (
"io"
"gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
oldcmds "github.com/ipfs/go-ipfs/commands"
logging "gx/ipfs/QmcVVHfdyv15GVPk7NrxdWjh2hLVccXnoD8j2tyQShiXJb/go-log"
)
var log = logging.Logger("cmds/lgc")
// NewCommand returns a Command from an oldcmds.Command
func NewCommand(oldcmd *oldcmds.Command) *cmds.Command {
if oldcmd == nil {
return nil
}
var cmd *cmds.Command
cmd = &cmds.Command{
Options: oldcmd.Options,
Arguments: oldcmd.Arguments,
Helptext: oldcmd.Helptext,
External: oldcmd.External,
Type: oldcmd.Type,
Subcommands: make(map[string]*cmds.Command),
}
if oldcmd.Run != nil {
cmd.Run = func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) {
oldReq := &requestWrapper{req, OldContext(env)}
res := &fakeResponse{req: oldReq, re: re, wait: make(chan struct{})}
errCh := make(chan error)
go res.Send(errCh)
oldcmd.Run(oldReq, res)
err := <-errCh
if err != nil {
log.Error(err)
}
}
}
if oldcmd.PreRun != nil {
cmd.PreRun = func(req *cmds.Request, env cmds.Environment) error {
oldReq := &requestWrapper{req, OldContext(env)}
return oldcmd.PreRun(oldReq)
}
}
for name, sub := range oldcmd.Subcommands {
cmd.Subcommands[name] = NewCommand(sub)
}
cmd.Encoders = make(cmds.EncoderMap)
for encType, m := range oldcmd.Marshalers {
cmd.Encoders[cmds.EncodingType(encType)] = func(m oldcmds.Marshaler, encType oldcmds.EncodingType) func(req *cmds.Request) func(io.Writer) cmds.Encoder {
return func(req *cmds.Request) func(io.Writer) cmds.Encoder {
return func(w io.Writer) cmds.Encoder {
return NewMarshalerEncoder(req, m, w)
}
}
}(m, encType)
}
return cmd
}