Skip to content

Commit b92263d

Browse files
committed
Code refactoring
1 parent bc31bd0 commit b92263d

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

command/ota/massupload.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
"github.com/sirupsen/logrus"
2425
"os"
2526
"path/filepath"
2627

@@ -54,24 +55,25 @@ type Result struct {
5455
OtaStatus otaapi.Ota
5556
}
5657

57-
func buildOtaFile(params *MassUploadParams) (string, error) {
58+
func buildOtaFile(params *MassUploadParams) (string, string, error) {
5859
var otaFile string
60+
var otaDir string
61+
var err error
5962
if params.DoNotApplyHeader {
6063
otaFile = params.File
6164
} else {
62-
otaDir, err := os.MkdirTemp("", "")
65+
otaDir, err = os.MkdirTemp("", "")
6366
if err != nil {
64-
return "", fmt.Errorf("%s: %w", "cannot create temporary folder", err)
67+
return "", "", fmt.Errorf("%s: %w", "cannot create temporary folder", err)
6568
}
6669
otaFile = filepath.Join(otaDir, "temp.ota")
67-
defer os.RemoveAll(otaDir)
6870

6971
err = Generate(params.File, otaFile, params.FQBN)
7072
if err != nil {
71-
return "", fmt.Errorf("%s: %w", "cannot generate .ota file", err)
73+
return "", "", fmt.Errorf("%s: %w", "cannot generate .ota file", err)
7274
}
7375
}
74-
return otaFile, nil
76+
return otaFile, otaDir, nil
7577
}
7678

7779
// MassUpload command is used to mass upload a firmware OTA,
@@ -84,6 +86,7 @@ func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Cred
8486
}
8587

8688
// Generate .ota file
89+
logrus.Infoln("Uploading binary", params.File)
8790
_, err := os.Stat(params.File)
8891
if err != nil {
8992
return nil, fmt.Errorf("file %s does not exists: %w", params.File, err)
@@ -98,10 +101,13 @@ func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Cred
98101
}
99102

100103
// Generate .ota file
101-
otaFile, err := buildOtaFile(params)
104+
otaFile, otaDir, err := buildOtaFile(params)
102105
if err != nil {
103106
return nil, err
104107
}
108+
if otaDir != "" {
109+
defer os.RemoveAll(otaDir)
110+
}
105111

106112
iotClient, err := iot.NewClient(cred)
107113
if err != nil {
@@ -204,6 +210,7 @@ func run(ctx context.Context, uploader otaUploader, otapi otaStatusGetter, ids [
204210
for _, id := range ids {
205211
file, err := os.Open(otaFile)
206212
if err != nil {
213+
logrus.Error("cannot open ota file:", otaFile)
207214
r := Result{ID: id, Err: fmt.Errorf("cannot open ota file")}
208215
results = append(results, r)
209216
continue
@@ -213,6 +220,7 @@ func run(ctx context.Context, uploader otaUploader, otapi otaStatusGetter, ids [
213220
}
214221
close(jobs)
215222

223+
logrus.Infoln("Uploading firmware to devices...")
216224
for i := 0; i < numConcurrentUploads; i++ {
217225
go func() {
218226
for job := range jobs {

0 commit comments

Comments
 (0)