@@ -252,26 +252,40 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
252252 // Build recipe for upload
253253 var recipe string
254254 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+ }
256258 } 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+ }
258262 }
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
262275 }
276+ cmdLine := props .ExpandPropsInString (recipe )
263277 cmdArgs , err := properties .SplitQuotedString (cmdLine , `"'` , false )
264278 if err != nil {
265- return nil , fmt .Errorf ("invalid recipe '%s': %s" , recipe , err )
279+ return fmt .Errorf ("invalid recipe '%s': %s" , recipe , err )
266280 }
267281
268282 // Run Tool
269- if req . GetVerbose () {
283+ if verbose {
270284 outStream .Write ([]byte (fmt .Sprintln (cmdLine )))
271285 }
272286 cmd , err := executils .Command (cmdArgs )
273287 if err != nil {
274- return nil , fmt .Errorf ("cannot execute upload tool: %s" , err )
288+ return fmt .Errorf ("cannot execute upload tool: %s" , err )
275289 }
276290
277291 executils .AttachStdoutListener (cmd , executils .PrintToStdout )
@@ -280,16 +294,14 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
280294 cmd .Stderr = errStream
281295
282296 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 )
284298 }
285299
286300 if err := cmd .Wait (); err != nil {
287- return nil , fmt .Errorf ("uploading error: %s" , err )
301+ return fmt .Errorf ("uploading error: %s" , err )
288302 }
289303
290- logrus .Tracef ("Upload %s on %s successful" , sketch .Name , fqbnIn )
291-
292- return & rpc.UploadResp {}, nil
304+ return nil
293305}
294306
295307func touchSerialPortAt1200bps (port string ) error {
0 commit comments