Skip to content

Commit 5f8cb4b

Browse files
authored
Allow case-insensitive ok or OK replies from pluggable discoveries and monitors (#1633)
* Allow case-insensitive 'ok'/'OK' replies from discoveries and monitors * Factor the same messages to reduce translations strings * Better error messages for discoveries and monitor
1 parent 569e194 commit 5f8cb4b

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

arduino/discovery/discovery.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,11 @@ func (disc *PluggableDiscovery) Run() (err error) {
311311
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
312312
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "HELLO", err)
313313
} else if msg.EventType != "hello" {
314-
return errors.Errorf(tr("communication out of sync, expected 'hello', received '%s'"), msg.EventType)
315-
} else if msg.Message != "OK" || msg.Error {
314+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "hello", msg.EventType)
315+
} else if msg.Error {
316316
return errors.Errorf(tr("command failed: %s"), msg.Message)
317+
} else if strings.ToUpper(msg.Message) != "OK" {
318+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
317319
} else if msg.ProtocolVersion > 1 {
318320
return errors.Errorf(tr("protocol version not supported: requested 1, got %d"), msg.ProtocolVersion)
319321
}
@@ -332,9 +334,11 @@ func (disc *PluggableDiscovery) Start() error {
332334
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
333335
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "START", err)
334336
} else if msg.EventType != "start" {
335-
return errors.Errorf(tr("communication out of sync, expected 'start', received '%s'"), msg.EventType)
336-
} else if msg.Message != "OK" || msg.Error {
337+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start", msg.EventType)
338+
} else if msg.Error {
337339
return errors.Errorf(tr("command failed: %s"), msg.Message)
340+
} else if strings.ToUpper(msg.Message) != "OK" {
341+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
338342
}
339343
disc.statusMutex.Lock()
340344
defer disc.statusMutex.Unlock()
@@ -352,9 +356,11 @@ func (disc *PluggableDiscovery) Stop() error {
352356
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
353357
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "STOP", err)
354358
} else if msg.EventType != "stop" {
355-
return errors.Errorf(tr("communication out of sync, expected 'stop', received '%s'"), msg.EventType)
356-
} else if msg.Message != "OK" || msg.Error {
359+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "stop", msg.EventType)
360+
} else if msg.Error {
357361
return errors.Errorf(tr("command failed: %s"), msg.Message)
362+
} else if strings.ToUpper(msg.Message) != "OK" {
363+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
358364
}
359365
disc.statusMutex.Lock()
360366
defer disc.statusMutex.Unlock()
@@ -375,9 +381,11 @@ func (disc *PluggableDiscovery) Quit() error {
375381
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
376382
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "QUIT", err)
377383
} else if msg.EventType != "quit" {
378-
return errors.Errorf(tr("communication out of sync, expected 'quit', received '%s'"), msg.EventType)
379-
} else if msg.Message != "OK" || msg.Error {
384+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "quit", msg.EventType)
385+
} else if msg.Error {
380386
return errors.Errorf(tr("command failed: %s"), msg.Message)
387+
} else if strings.ToUpper(msg.Message) != "OK" {
388+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
381389
}
382390
disc.killProcess()
383391
return nil
@@ -392,7 +400,7 @@ func (disc *PluggableDiscovery) List() ([]*Port, error) {
392400
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
393401
return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "LIST", err)
394402
} else if msg.EventType != "list" {
395-
return nil, errors.Errorf(tr("communication out of sync, expected 'list', received '%s'"), msg.EventType)
403+
return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "list", msg.EventType)
396404
} else if msg.Error {
397405
return nil, errors.Errorf(tr("command failed: %s"), msg.Message)
398406
} else {
@@ -415,9 +423,11 @@ func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) {
415423
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
416424
return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "START_SYNC", err)
417425
} else if msg.EventType != "start_sync" {
418-
return nil, errors.Errorf(tr("communication out of sync, expected 'start_sync', received '%s'"), msg.EventType)
419-
} else if msg.Message != "OK" || msg.Error {
426+
return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start_sync", msg.EventType)
427+
} else if msg.Error {
420428
return nil, errors.Errorf(tr("command failed: %s"), msg.Message)
429+
} else if strings.ToUpper(msg.Message) != "OK" {
430+
return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
421431
}
422432

423433
disc.statusMutex.Lock()

arduino/monitor/monitor.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,12 @@ func (mon *PluggableMonitor) waitMessage(timeout time.Duration, expectedEvt stri
145145
if msg.EventType != expectedEvt {
146146
return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), expectedEvt, msg.EventType)
147147
}
148-
if msg.Message != "OK" || msg.Error {
148+
if msg.Error {
149149
return msg, fmt.Errorf(tr("command '%[1]s' failed: %[2]s"), expectedEvt, msg.Message)
150150
}
151+
if strings.ToUpper(msg.Message) != "OK" {
152+
return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
153+
}
151154
return msg, nil
152155
}
153156

0 commit comments

Comments
 (0)