Skip to content

Commit 0f5c408

Browse files
committed
Improve readability
1 parent 9e817ea commit 0f5c408

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

command/ota/upload.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,37 +116,42 @@ func idsGivenTags(iotClient iot.Client, tags map[string]string) ([]string, error
116116
}
117117

118118
func run(iotClient iot.Client, ids []string, file *os.File, expiration int) error {
119-
idsToProcess := make(chan string, 2000)
120-
idsFailed := make(chan string, 2000)
119+
targets := make(chan string, len(ids))
120+
type result struct {
121+
id string
122+
err error
123+
}
124+
results := make(chan result, len(ids))
125+
121126
for _, id := range ids {
122-
idsToProcess <- id
127+
targets <- id
123128
}
124-
close(idsToProcess)
129+
close(targets)
125130

126131
for i := 0; i < numConcurrentUploads; i++ {
127132
go func() {
128-
for id := range idsToProcess {
133+
for id := range targets {
129134
err := iotClient.DeviceOTA(id, file, expiration)
130-
fail := ""
131-
if err != nil {
132-
fail = id
133-
}
134-
idsFailed <- fail
135+
results <- result{id: id, err: err}
135136
}
136137
}()
137138
}
138139

139-
failMsg := ""
140+
var fails []string
141+
var details []string
140142
for range ids {
141-
i := <-idsFailed
142-
if i != "" {
143-
failMsg = strings.Join([]string{i, failMsg}, ",")
143+
r := <-results
144+
if r.err != nil {
145+
fails = append(fails, r.id)
146+
details = append(details, fmt.Sprintf("%s: %s", r.id, r.err.Error()))
144147
}
145148
}
146149

147-
if failMsg != "" {
148-
failMsg = strings.TrimRight(failMsg, ",")
149-
return fmt.Errorf("failed to update these devices: %s", failMsg)
150+
if len(fails) > 0 {
151+
f := strings.Join(fails, ",")
152+
f = strings.TrimRight(f, ",")
153+
d := strings.Join(details, "\n")
154+
return fmt.Errorf("failed to update these devices: %s\nreasons:\n%s", f, d)
150155
}
151156
return nil
152157
}

0 commit comments

Comments
 (0)