Skip to content

Commit 1c41822

Browse files
commands: push always on top the arduino vendor
1 parent d56acae commit 1c41822

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

commands/service_board_identify.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"net/http"
2525
"regexp"
26+
"slices"
2627
"sort"
2728
"strings"
2829
"time"
@@ -216,5 +217,14 @@ func apiByVidPid(ctx context.Context, vid, pid string, settings *configuration.S
216217
}
217218
response[i] = &rpc.BoardListItem{Name: v.Name, Fqbn: v.FQBN}
218219
}
220+
221+
// In case multiple platform matches the same vid-pid we put on top
222+
// the arduino ones
223+
slices.SortFunc(response, func(a, b *rpc.BoardListItem) int {
224+
if strings.HasPrefix(a.Fqbn, "arduino") {
225+
return -1
226+
}
227+
return 0
228+
})
219229
return response, nil
220230
}

commands/service_board_identify_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,41 @@ func TestGetByVidPid(t *testing.T) {
6363
require.NotNil(t, err)
6464
}
6565

66+
func TestGetByVidPidPutArduinoOnTop(t *testing.T) {
67+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
68+
fmt.Fprintln(w, `
69+
{
70+
"items": [
71+
{
72+
"fqbn": "esp32:esp32:nano_nora",
73+
"vendor": "esp32",
74+
"architecture": "esp32",
75+
"board_id": "nano_nora",
76+
"name": "Arduino Nano ESP32"
77+
},
78+
{
79+
"fqbn": "arduino:esp32:nano_nora",
80+
"vendor": "arduino",
81+
"architecture": "esp32",
82+
"board_id": "nano_nora",
83+
"name": "Arduino Nano ESP32"
84+
}
85+
]
86+
}
87+
`)
88+
}))
89+
defer ts.Close()
90+
91+
vidPidURL = ts.URL
92+
settings := configuration.NewSettings()
93+
res, err := apiByVidPid(context.Background(), "0x2341", "0x0070", settings)
94+
require.Nil(t, err)
95+
require.Len(t, res, 2)
96+
require.Equal(t, "Arduino Nano ESP32", res[0].GetName())
97+
require.Equal(t, "arduino:esp32:nano_nora", res[0].GetFqbn())
98+
require.Equal(t, "esp32:esp32:nano_nora", res[1].GetFqbn())
99+
}
100+
66101
func TestGetByVidPidNotFound(t *testing.T) {
67102
settings := configuration.NewSettings()
68103

0 commit comments

Comments
 (0)