forked from TRON-US/go-btfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
135 lines (118 loc) · 3.29 KB
/
context.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package commands
import (
"context"
"errors"
"strings"
"time"
core "github.com/TRON-US/go-btfs/core"
coreapi "github.com/TRON-US/go-btfs/core/coreapi"
loader "github.com/TRON-US/go-btfs/plugin/loader"
"github.com/TRON-US/go-btfs-cmds"
config "github.com/TRON-US/go-btfs-config"
coreiface "github.com/TRON-US/interface-go-btfs-core"
options "github.com/TRON-US/interface-go-btfs-core/options"
logging "github.com/ipfs/go-log"
)
var log = logging.Logger("command")
// Context represents request context
type Context struct {
ConfigRoot string
ReqLog *ReqLog
Plugins *loader.PluginLoader
config *config.Config
LoadConfig func(path string) (*config.Config, error)
Gateway bool
api coreiface.CoreAPI
node *core.IpfsNode
ConstructNode func() (*core.IpfsNode, error)
}
// GetConfig returns the config of the current Command execution
// context. It may load it with the provided function.
func (c *Context) GetConfig() (*config.Config, error) {
var err error
if c.config == nil {
if c.LoadConfig == nil {
return nil, errors.New("nil LoadConfig function")
}
c.config, err = c.LoadConfig(c.ConfigRoot)
}
return c.config, err
}
// GetNode returns the node of the current Command execution
// context. It may construct it with the provided function.
func (c *Context) GetNode() (*core.IpfsNode, error) {
var err error
if c.node == nil {
if c.ConstructNode == nil {
return nil, errors.New("nil ConstructNode function")
}
c.node, err = c.ConstructNode()
if err == nil {
// Pre-load the config from the repo to avoid re-parsing it from disk.
if cfg, err := c.node.Repo.Config(); err != nil {
c.config = cfg
}
}
}
return c.node, err
}
// GetAPI returns CoreAPI instance backed by ipfs node.
// It may construct the node with the provided function
func (c *Context) GetAPI() (coreiface.CoreAPI, error) {
if c.api == nil {
n, err := c.GetNode()
if err != nil {
return nil, err
}
fetchBlocks := true
if c.Gateway {
cfg, err := c.GetConfig()
if err != nil {
return nil, err
}
fetchBlocks = !cfg.Gateway.NoFetch
}
c.api, err = coreapi.NewCoreAPI(n, options.Api.FetchBlocks(fetchBlocks))
if err != nil {
return nil, err
}
}
return c.api, nil
}
// Context returns the node's context.
func (c *Context) Context() context.Context {
n, err := c.GetNode()
if err != nil {
log.Debug("error getting node: ", err)
return context.Background()
}
return n.Context()
}
// LogRequest adds the passed request to the request log and
// returns a function that should be called when the request
// lifetime is over.
func (c *Context) LogRequest(req *cmds.Request) func() {
rle := &ReqLogEntry{
StartTime: time.Now(),
Active: true,
Command: strings.Join(req.Path, "/"),
Options: req.Options,
Args: req.Arguments,
ID: c.ReqLog.nextID,
log: c.ReqLog,
}
c.ReqLog.AddEntry(rle)
return func() {
c.ReqLog.Finish(rle)
}
}
// Close cleans up the application state.
func (c *Context) Close() {
// let's not forget teardown. If a node was initialized, we must close it.
// Note that this means the underlying req.Context().Node variable is exposed.
// this is gross, and should be changed when we extract out the exec Context.
if c.node != nil {
log.Info("Shutting down node...")
c.node.Close()
}
}