feat: cmd/ipfs: Allow passing custom BuildEnv to main
Łukasz Magiera committed
Mar 30, 2023 at 19:11 UTC
86b73f61fc9bc1b9ddd140e77ab890ee2a4e5092
2 files changed
+60
-48
cmd/ipfs/main.go
+59
-47
@@ -60,12 +60,20 @@ const (
60
heapProfile = "ipfs.memprof"
61
)
62
63
-func loadPlugins(repoPath string) (*loader.PluginLoader, error) {
63
+type PluginPreloader func(*loader.PluginLoader) error
64
+
65
+func LoadPlugins(repoPath string, preload PluginPreloader) (*loader.PluginLoader, error) {
66
plugins, err := loader.NewPluginLoader(repoPath)
67
if err != nil {
68
return nil, fmt.Errorf("error loading plugins: %s", err)
69
}
70
71
+ if preload != nil {
72
+ if err := preload(plugins); err != nil {
73
+ return nil, fmt.Errorf("error loading plugins (preload): %s", err)
74
+ }
75
+ }
76
+
77
if err := plugins.Initialize(); err != nil {
78
return nil, fmt.Errorf("error initializing plugins: %s", err)
79
}
@@ -83,7 +91,7 @@ func loadPlugins(repoPath string) (*loader.PluginLoader, error) {
91
// - output the response
92
// - if anything fails, print error, maybe with help.
93
func main() {
86
- os.Exit(mainRet())
94
+ os.Exit(Start(BuildDefaultEnv))
95
}
96
97
func printErr(err error) int {
@@ -101,7 +109,54 @@ func newUUID(key string) logging.Metadata {
109
}
110
}
111
104
-func mainRet() (exitCode int) {
112
+func BuildDefaultEnv(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
113
+ return BuildEnv(ctx, req, nil)
114
+}
115
+
116
+func BuildEnv(ctx context.Context, req *cmds.Request, pl PluginPreloader) (cmds.Environment, error) {
117
+ checkDebug(req)
118
+ repoPath, err := GetRepoPath(req)
119
+ if err != nil {
120
+ return nil, err
121
+ }
122
+ log.Debugf("config path is %s", repoPath)
123
+
124
+ plugins, err := LoadPlugins(repoPath, pl)
125
+ if err != nil {
126
+ return nil, err
127
+ }
128
+
129
+ // this sets up the function that will initialize the node
130
+ // this is so that we can construct the node lazily.
131
+ return &oldcmds.Context{
132
+ ConfigRoot: repoPath,
133
+ ReqLog: &oldcmds.ReqLog{},
134
+ Plugins: plugins,
135
+ ConstructNode: func() (n *core.IpfsNode, err error) {
136
+ if req == nil {
137
+ return nil, errors.New("constructing node without a request")
138
+ }
139
+
140
+ r, err := fsrepo.Open(repoPath)
141
+ if err != nil { // repo is owned by the node
142
+ return nil, err
143
+ }
144
+
145
+ // ok everything is good. set it on the invocation (for ownership)
146
+ // and return it.
147
+ n, err = core.NewNode(ctx, &core.BuildCfg{
148
+ Repo: r,
149
+ })
150
+ if err != nil {
151
+ return nil, err
152
+ }
153
+
154
+ return n, nil
155
+ },
156
+ }, nil
157
+}
158
+
159
+func Start(buildEnv func(ctx context.Context, req *cmds.Request) (cmds.Environment, error)) (exitCode int) {
160
ctx := logging.ContextWithLoggable(context.Background(), newUUID("session"))
161
162
tp, err := tracing.NewTracerProvider(ctx)
@@ -155,49 +210,6 @@ func mainRet() (exitCode int) {
210
// so we need to make sure it's stable
211
os.Args[0] = "ipfs"
212
158
- buildEnv := func(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
159
- checkDebug(req)
160
- repoPath, err := getRepoPath(req)
161
- if err != nil {
162
- return nil, err
163
- }
164
- log.Debugf("config path is %s", repoPath)
165
-
166
- plugins, err := loadPlugins(repoPath)
167
- if err != nil {
168
- return nil, err
169
- }
170
-
171
- // this sets up the function that will initialize the node
172
- // this is so that we can construct the node lazily.
173
- return &oldcmds.Context{
174
- ConfigRoot: repoPath,
175
- ReqLog: &oldcmds.ReqLog{},
176
- Plugins: plugins,
177
- ConstructNode: func() (n *core.IpfsNode, err error) {
178
- if req == nil {
179
- return nil, errors.New("constructing node without a request")
180
- }
181
-
182
- r, err := fsrepo.Open(repoPath)
183
- if err != nil { // repo is owned by the node
184
- return nil, err
185
- }
186
-
187
- // ok everything is good. set it on the invocation (for ownership)
188
- // and return it.
189
- n, err = core.NewNode(ctx, &core.BuildCfg{
190
- Repo: r,
191
- })
192
- if err != nil {
193
- return nil, err
194
- }
195
-
196
- return n, nil
197
- },
198
- }, nil
199
- }
200
-
213
err = cli.Run(ctx, Root, os.Args, os.Stdin, os.Stdout, os.Stderr, buildEnv, makeExecutor)
214
if err != nil {
215
return 1
@@ -364,7 +376,7 @@ func (twe tracingWrappedExecutor) Execute(req *cmds.Request, re cmds.ResponseEmi
376
return err
377
}
378
367
-func getRepoPath(req *cmds.Request) (string, error) {
379
+func GetRepoPath(req *cmds.Request) (string, error) {
380
repoOpt, found := req.Options[corecmds.RepoDirOption].(string)
381
if found && repoOpt != "" {
382
return repoOpt, nil
cmd/ipfs/runmain_test.go
+1
-1
@@ -16,7 +16,7 @@ import (
16
func TestRunMain(t *testing.T) {
17
args := flag.Args()
18
os.Args = append([]string{os.Args[0]}, args...)
19
- ret := mainRet()
19
+ ret := Start()
20
21
p := os.Getenv("IPFS_COVER_RET_FILE")
22
if len(p) != 0 {