feat: cmd/ipfs: Nicer to use BuildEnv
Łukasz Magiera committed
Mar 30, 2023 at 19:36 UTC
a8a12789f2f53849950ddcc1c28b940d84a01341
1 file changed
+39
-37
cmd/ipfs/kubo/start.go
+39
-37
@@ -110,50 +110,52 @@ func newUUID(key string) logging.Metadata {
110
}
111
112
func BuildDefaultEnv(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
113
- return BuildEnv(ctx, req, nil)
113
+ return BuildEnv(nil)(ctx, req)
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)
116
+func BuildEnv(pl PluginPreloader) func(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
117
+ return func(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
118
+ checkDebug(req)
119
+ repoPath, err := GetRepoPath(req)
120
+ if err != nil {
121
+ return nil, err
122
+ }
123
+ log.Debugf("config path is %s", repoPath)
124
124
- plugins, err := LoadPlugins(repoPath, pl)
125
- if err != nil {
126
- return nil, err
127
- }
125
+ plugins, err := LoadPlugins(repoPath, pl)
126
+ if err != nil {
127
+ return nil, err
128
+ }
129
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
- }
130
+ // this sets up the function that will initialize the node
131
+ // this is so that we can construct the node lazily.
132
+ return &oldcmds.Context{
133
+ ConfigRoot: repoPath,
134
+ ReqLog: &oldcmds.ReqLog{},
135
+ Plugins: plugins,
136
+ ConstructNode: func() (n *core.IpfsNode, err error) {
137
+ if req == nil {
138
+ return nil, errors.New("constructing node without a request")
139
+ }
140
140
- r, err := fsrepo.Open(repoPath)
141
- if err != nil { // repo is owned by the node
142
- return nil, err
143
- }
141
+ r, err := fsrepo.Open(repoPath)
142
+ if err != nil { // repo is owned by the node
143
+ return nil, err
144
+ }
145
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
- }
146
+ // ok everything is good. set it on the invocation (for ownership)
147
+ // and return it.
148
+ n, err = core.NewNode(ctx, &core.BuildCfg{
149
+ Repo: r,
150
+ })
151
+ if err != nil {
152
+ return nil, err
153
+ }
154
154
- return n, nil
155
- },
156
- }, nil
155
+ return n, nil
156
+ },
157
+ }, nil
158
+ }
159
}
160
161
func Start(buildEnv func(ctx context.Context, req *cmds.Request) (cmds.Environment, error)) (exitCode int) {