/
env.go
72 lines (59 loc) · 1.8 KB
/
env.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
71
72
package cmdenv
import (
"fmt"
"strings"
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
cmds "github.com/ipfs/go-ipfs-cmds"
config "github.com/ipfs/go-ipfs-config"
logging "github.com/ipfs/go-log"
coreiface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
)
var log = logging.Logger("core/commands/cmdenv")
// GetNode extracts the node from the environment.
func GetNode(env interface{}) (*core.IpfsNode, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}
return ctx.GetNode()
}
// GetApi extracts CoreAPI instance from the environment.
func GetApi(env cmds.Environment, req *cmds.Request) (coreiface.CoreAPI, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}
offline, _ := req.Options["offline"].(bool)
if !offline {
offline, _ = req.Options["local"].(bool)
if offline {
log.Errorf("Command '%s', --local is deprecated, use --offline instead", strings.Join(req.Path, " "))
}
}
api, err := ctx.GetAPI()
if err != nil {
return nil, err
}
if offline {
return api.WithOptions(options.Api.Offline(offline))
}
return api, nil
}
// GetConfig extracts the config from the environment.
func GetConfig(env cmds.Environment) (*config.Config, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}
return ctx.GetConfig()
}
// GetConfigRoot extracts the config root from the environment
func GetConfigRoot(env cmds.Environment) (string, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return "", fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}
return ctx.ConfigRoot, nil
}