forked from OpenBazaar/openbazaar-go
/
request.go
67 lines (58 loc) · 1.6 KB
/
request.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
package ipfs
import (
"context"
cmds "github.com/ipfs/go-ipfs/commands"
cli "github.com/ipfs/go-ipfs/commands/cli"
commands "github.com/ipfs/go-ipfs/core/commands"
"github.com/op/go-logging"
"time"
)
var log = logging.MustGetLogger("ipfs")
var Root = &cmds.Command{
Options: commands.Root.Options,
Helptext: commands.Root.Helptext,
}
var commandsClientCmd = commands.CommandsCmd(Root)
var localCommands = map[string]*cmds.Command{
"commands": commandsClientCmd,
}
func init() {
Root.Subcommands = localCommands
for k, v := range commands.Root.Subcommands {
if _, found := Root.Subcommands[k]; !found {
Root.Subcommands[k] = v
}
}
}
func NewRequest(ctx cmds.Context, args []string) (cmds.Request, *cmds.Command, error) {
Root.Subcommands = localCommands
for k, v := range commands.Root.Subcommands {
if _, found := Root.Subcommands[k]; !found {
Root.Subcommands[k] = v
}
}
req, cmd, _, err := cli.Parse(args, nil, Root)
cctx := context.Background()
rerr := req.SetRootContext(cctx)
if rerr != nil {
return nil, nil, rerr
}
req.SetInvocContext(ctx)
return req, cmd, err
}
func NewRequestWithTimeout(ctx cmds.Context, args []string, timeout time.Duration) (cmds.Request, *cmds.Command, error) {
Root.Subcommands = localCommands
for k, v := range commands.Root.Subcommands {
if _, found := Root.Subcommands[k]; !found {
Root.Subcommands[k] = v
}
}
req, cmd, _, err := cli.Parse(args, nil, Root)
cctx, _ := context.WithTimeout(context.Background(), timeout)
rerr := req.SetRootContext(cctx)
if rerr != nil {
return nil, nil, rerr
}
req.SetInvocContext(ctx)
return req, cmd, err
}