@@ -252,26 +252,40 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
252
252
// Build recipe for upload
253
253
var recipe string
254
254
if programmer != nil {
255
- recipe = uploadProperties .Get ("program.pattern" )
255
+ if err := runTool ("program.pattern" , uploadProperties , outStream , errStream , req .GetVerbose ()); err != nil {
256
+ return nil , fmt .Errorf ("programming error: %s" , err )
257
+ }
256
258
} else {
257
- recipe = uploadProperties .Get ("upload.pattern" )
259
+ if err := runTool ("upload.pattern" , uploadProperties , outStream , errStream , req .GetVerbose ()); err != nil {
260
+ return nil , fmt .Errorf ("uploading error: %s" , err )
261
+ }
258
262
}
259
- cmdLine := uploadProperties .ExpandPropsInString (recipe )
260
- if req .GetVerbose () {
261
- outStream .Write ([]byte (fmt .Sprintln (cmdLine )))
263
+
264
+ logrus .Tracef ("Upload %s on %s successful" , sketch .Name , fqbnIn )
265
+ return & rpc.UploadResp {}, nil
266
+ }
267
+
268
+ func runTool (recipeID string , props * properties.Map , outStream , errStream io.Writer , verbose bool ) error {
269
+ recipe , ok := props .GetOk (recipeID )
270
+ if ! ok {
271
+ return fmt .Errorf ("recipe not found '%s'" , recipeID )
272
+ }
273
+ if strings .TrimSpace (recipe ) == "" {
274
+ return nil // Nothing to run
262
275
}
276
+ cmdLine := props .ExpandPropsInString (recipe )
263
277
cmdArgs , err := properties .SplitQuotedString (cmdLine , `"'` , false )
264
278
if err != nil {
265
- return nil , fmt .Errorf ("invalid recipe '%s': %s" , recipe , err )
279
+ return fmt .Errorf ("invalid recipe '%s': %s" , recipe , err )
266
280
}
267
281
268
282
// Run Tool
269
- if req . GetVerbose () {
283
+ if verbose {
270
284
outStream .Write ([]byte (fmt .Sprintln (cmdLine )))
271
285
}
272
286
cmd , err := executils .Command (cmdArgs )
273
287
if err != nil {
274
- return nil , fmt .Errorf ("cannot execute upload tool: %s" , err )
288
+ return fmt .Errorf ("cannot execute upload tool: %s" , err )
275
289
}
276
290
277
291
executils .AttachStdoutListener (cmd , executils .PrintToStdout )
@@ -280,16 +294,14 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
280
294
cmd .Stderr = errStream
281
295
282
296
if err := cmd .Start (); err != nil {
283
- return nil , fmt .Errorf ("cannot execute upload tool: %s" , err )
297
+ return fmt .Errorf ("cannot execute upload tool: %s" , err )
284
298
}
285
299
286
300
if err := cmd .Wait (); err != nil {
287
- return nil , fmt .Errorf ("uploading error: %s" , err )
301
+ return fmt .Errorf ("uploading error: %s" , err )
288
302
}
289
303
290
- logrus .Tracef ("Upload %s on %s successful" , sketch .Name , fqbnIn )
291
-
292
- return & rpc.UploadResp {}, nil
304
+ return nil
293
305
}
294
306
295
307
func touchSerialPortAt1200bps (port string ) error {
0 commit comments