Skip to content

Commit 268c857

Browse files
committed
Simplified callbacks in commands.Init
1 parent 5fc1b3e commit 268c857

File tree

1 file changed

+40
-73
lines changed

1 file changed

+40
-73
lines changed

commands/instances.go

Lines changed: 40 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,41 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
173173
// Failures don't stop the loading process, in case of loading failure the Platform or library
174174
// is simply skipped and an error gRPC status is sent to responseCallback.
175175
func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) error {
176-
if responseCallback == nil {
177-
responseCallback = func(r *rpc.InitResponse) {}
178-
}
179176
instance := instances[req.Instance.Id]
180177
if instance == nil {
181178
return &arduino.InvalidInstanceError{}
182179
}
183180

181+
// Setup callback functions
182+
if responseCallback == nil {
183+
responseCallback = func(r *rpc.InitResponse) {}
184+
}
185+
responseError := func(st *status.Status) {
186+
responseCallback(&rpc.InitResponse{
187+
Message: &rpc.InitResponse_Error{
188+
Error: st.Proto(),
189+
},
190+
})
191+
}
192+
taskCallback := func(msg *rpc.TaskProgress) {
193+
responseCallback(&rpc.InitResponse{
194+
Message: &rpc.InitResponse_InitProgress{
195+
InitProgress: &rpc.InitResponse_Progress{
196+
TaskProgress: msg,
197+
},
198+
},
199+
})
200+
}
201+
downloadCallback := func(msg *rpc.DownloadProgress) {
202+
responseCallback(&rpc.InitResponse{
203+
Message: &rpc.InitResponse_InitProgress{
204+
InitProgress: &rpc.InitResponse_Progress{
205+
DownloadProgress: msg,
206+
},
207+
},
208+
})
209+
}
210+
184211
// We need to clear the PackageManager currently in use by this instance
185212
// in case this is not the first Init on this instances, that might happen
186213
// after reinitializing an instance after installing or uninstalling a core.
@@ -195,11 +222,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
195222
URL, err := utils.URLParse(u)
196223
if err != nil {
197224
s := status.Newf(codes.InvalidArgument, tr("Invalid additional URL: %v"), err)
198-
responseCallback(&rpc.InitResponse{
199-
Message: &rpc.InitResponse_Error{
200-
Error: s.Proto(),
201-
},
202-
})
225+
responseError(s)
203226
continue
204227
}
205228

@@ -209,22 +232,14 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
209232
_, err := instance.PackageManager.LoadPackageIndexFromFile(indexFile)
210233
if err != nil {
211234
s := status.Newf(codes.FailedPrecondition, tr("Loading index file: %v"), err)
212-
responseCallback(&rpc.InitResponse{
213-
Message: &rpc.InitResponse_Error{
214-
Error: s.Proto(),
215-
},
216-
})
235+
responseError(s)
217236
}
218237
continue
219238
}
220239

221240
if err := instance.PackageManager.LoadPackageIndex(URL); err != nil {
222241
s := status.Newf(codes.FailedPrecondition, tr("Loading index file: %v"), err)
223-
responseCallback(&rpc.InitResponse{
224-
Message: &rpc.InitResponse_Error{
225-
Error: s.Proto(),
226-
},
227-
})
242+
responseError(s)
228243
}
229244
}
230245

@@ -233,31 +248,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
233248
// and they would never get reloaded.
234249
for _, err := range instance.PackageManager.LoadHardware() {
235250
s := &arduino.PlatformLoadingError{Cause: err}
236-
responseCallback(&rpc.InitResponse{
237-
Message: &rpc.InitResponse_Error{
238-
Error: s.ToRPCStatus().Proto(),
239-
},
240-
})
241-
}
242-
243-
taskCallback := func(msg *rpc.TaskProgress) {
244-
responseCallback(&rpc.InitResponse{
245-
Message: &rpc.InitResponse_InitProgress{
246-
InitProgress: &rpc.InitResponse_Progress{
247-
TaskProgress: msg,
248-
},
249-
},
250-
})
251-
}
252-
253-
downloadCallback := func(msg *rpc.DownloadProgress) {
254-
responseCallback(&rpc.InitResponse{
255-
Message: &rpc.InitResponse_InitProgress{
256-
InitProgress: &rpc.InitResponse_Progress{
257-
DownloadProgress: msg,
258-
},
259-
},
260-
})
251+
responseError(s.ToRPCStatus())
261252
}
262253

263254
// Get builtin tools
@@ -266,11 +257,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
266257
latestRelease := tool.LatestRelease()
267258
if latestRelease == nil {
268259
s := status.Newf(codes.Internal, tr("can't find latest release of tool %s", name))
269-
responseCallback(&rpc.InitResponse{
270-
Message: &rpc.InitResponse_Error{
271-
Error: s.Proto(),
272-
},
273-
})
260+
responseError(s)
274261
continue
275262
}
276263
builtinToolReleases = append(builtinToolReleases, latestRelease)
@@ -282,11 +269,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
282269
installed, err := instance.installToolIfMissing(toolRelease, downloadCallback, taskCallback)
283270
if err != nil {
284271
s := status.Newf(codes.Internal, err.Error())
285-
responseCallback(&rpc.InitResponse{
286-
Message: &rpc.InitResponse_Error{
287-
Error: s.Proto(),
288-
},
289-
})
272+
responseError(s)
290273
continue
291274
}
292275
toolsHaveBeenInstalled = toolsHaveBeenInstalled || installed
@@ -297,21 +280,13 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
297280
// so we must reload again otherwise we would never found them.
298281
for _, err := range instance.PackageManager.LoadHardware() {
299282
s := &arduino.PlatformLoadingError{Cause: err}
300-
responseCallback(&rpc.InitResponse{
301-
Message: &rpc.InitResponse_Error{
302-
Error: s.ToRPCStatus().Proto(),
303-
},
304-
})
283+
responseError(s.ToRPCStatus())
305284
}
306285
}
307286

308287
for _, err := range instance.PackageManager.LoadDiscoveries() {
309288
s := &arduino.PlatformLoadingError{Cause: err}
310-
responseCallback(&rpc.InitResponse{
311-
Message: &rpc.InitResponse_Error{
312-
Error: s.ToRPCStatus().Proto(),
313-
},
314-
})
289+
responseError(s.ToRPCStatus())
315290
}
316291

317292
// Load libraries
@@ -325,20 +300,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
325300

326301
if err := instance.lm.LoadIndex(); err != nil {
327302
s := status.Newf(codes.FailedPrecondition, tr("Loading index file: %v"), err)
328-
responseCallback(&rpc.InitResponse{
329-
Message: &rpc.InitResponse_Error{
330-
Error: s.Proto(),
331-
},
332-
})
303+
responseError(s)
333304
}
334305

335306
for _, err := range instance.lm.RescanLibraries() {
336307
s := status.Newf(codes.FailedPrecondition, tr("Loading libraries: %v"), err)
337-
responseCallback(&rpc.InitResponse{
338-
Message: &rpc.InitResponse_Error{
339-
Error: s.Proto(),
340-
},
341-
})
308+
responseError(s)
342309
}
343310

344311
// Refreshes the locale used, this will change the

0 commit comments

Comments
 (0)