@@ -21,6 +21,7 @@ import (
2121 "context"
2222 "errors"
2323 "fmt"
24+ "github.com/sirupsen/logrus"
2425 "os"
2526 "path/filepath"
2627
@@ -54,6 +55,27 @@ type Result struct {
5455 OtaStatus otaapi.Ota
5556}
5657
58+ func buildOtaFile (params * MassUploadParams ) (string , string , error ) {
59+ var otaFile string
60+ var otaDir string
61+ var err error
62+ if params .DoNotApplyHeader {
63+ otaFile = params .File
64+ } else {
65+ otaDir , err = os .MkdirTemp ("" , "" )
66+ if err != nil {
67+ return "" , "" , fmt .Errorf ("%s: %w" , "cannot create temporary folder" , err )
68+ }
69+ otaFile = filepath .Join (otaDir , "temp.ota" )
70+
71+ err = Generate (params .File , otaFile , params .FQBN )
72+ if err != nil {
73+ return "" , "" , fmt .Errorf ("%s: %w" , "cannot generate .ota file" , err )
74+ }
75+ }
76+ return otaFile , otaDir , nil
77+ }
78+
5779// MassUpload command is used to mass upload a firmware OTA,
5880// on devices of Arduino IoT Cloud.
5981func MassUpload (ctx context.Context , params * MassUploadParams , cred * config.Credentials ) ([]Result , error ) {
@@ -64,6 +86,7 @@ func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Cred
6486 }
6587
6688 // Generate .ota file
89+ logrus .Infoln ("Uploading binary" , params .File )
6790 _ , err := os .Stat (params .File )
6891 if err != nil {
6992 return nil , fmt .Errorf ("file %s does not exists: %w" , params .File , err )
@@ -78,21 +101,12 @@ func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Cred
78101 }
79102
80103 // Generate .ota file
81- var otaFile string
82- if params .DoNotApplyHeader {
83- otaFile = params .File
84- } else {
85- otaDir , err := os .MkdirTemp ("" , "" )
86- if err != nil {
87- return nil , fmt .Errorf ("%s: %w" , "cannot create temporary folder" , err )
88- }
89- otaFile := filepath .Join (otaDir , "temp.ota" )
104+ otaFile , otaDir , err := buildOtaFile (params )
105+ if err != nil {
106+ return nil , err
107+ }
108+ if otaDir != "" {
90109 defer os .RemoveAll (otaDir )
91-
92- err = Generate (params .File , otaFile , params .FQBN )
93- if err != nil {
94- return nil , fmt .Errorf ("%s: %w" , "cannot generate .ota file" , err )
95- }
96110 }
97111
98112 iotClient , err := iot .NewClient (cred )
@@ -196,6 +210,7 @@ func run(ctx context.Context, uploader otaUploader, otapi otaStatusGetter, ids [
196210 for _ , id := range ids {
197211 file , err := os .Open (otaFile )
198212 if err != nil {
213+ logrus .Error ("cannot open ota file:" , otaFile )
199214 r := Result {ID : id , Err : fmt .Errorf ("cannot open ota file" )}
200215 results = append (results , r )
201216 continue
@@ -205,6 +220,7 @@ func run(ctx context.Context, uploader otaUploader, otapi otaStatusGetter, ids [
205220 }
206221 close (jobs )
207222
223+ logrus .Infoln ("Uploading firmware to devices..." )
208224 for i := 0 ; i < numConcurrentUploads ; i ++ {
209225 go func () {
210226 for job := range jobs {
0 commit comments