18
18
package core
19
19
20
20
import (
21
- "context"
22
21
"errors"
23
22
"regexp"
24
23
"strings"
@@ -28,37 +27,48 @@ import (
28
27
rpc "github.com/arduino/arduino-cli/rpc/commands"
29
28
)
30
29
30
+ func match (line , searchArgs string ) bool {
31
+ return strings .Contains (strings .ToLower (line ), searchArgs )
32
+ }
33
+
31
34
// PlatformSearch FIXMEDOC
32
- func PlatformSearch (ctx context. Context , req * rpc. PlatformSearchReq ) (* rpc.PlatformSearchResp , error ) {
33
- pm := commands .GetPackageManager (req . GetInstance (). GetId () )
35
+ func PlatformSearch (instanceID int32 , searchArgs string , allVersions bool ) (* rpc.PlatformSearchResp , error ) {
36
+ pm := commands .GetPackageManager (instanceID )
34
37
if pm == nil {
35
38
return nil , errors .New ("invalid instance" )
36
39
}
37
40
38
- search := req .SearchArgs
39
-
40
41
res := []* cores.PlatformRelease {}
41
- if isUsb , _ := regexp .MatchString ("[0-9a-f]{4}:[0-9a-f]{4}" , search ); isUsb {
42
- vid , pid := search [:4 ], search [5 :]
42
+ if isUsb , _ := regexp .MatchString ("[0-9a-f]{4}:[0-9a-f]{4}" , searchArgs ); isUsb {
43
+ vid , pid := searchArgs [:4 ], searchArgs [5 :]
43
44
res = pm .FindPlatformReleaseProvidingBoardsWithVidPid (vid , pid )
44
45
} else {
45
- match := func (line string ) bool {
46
- return strings .Contains (strings .ToLower (line ), search )
47
- }
48
46
for _ , targetPackage := range pm .Packages {
49
47
for _ , platform := range targetPackage .Platforms {
50
48
platformRelease := platform .GetLatestRelease ()
51
49
if platformRelease == nil {
52
50
continue
53
51
}
54
- if match ( platform . Name ) || match ( platform . Architecture ) {
55
- res = append ( res , platformRelease )
56
- continue
57
- }
58
- for _ , board := range platformRelease . BoardsManifest {
59
- if match ( board . Name ) {
52
+
53
+ // platform has a release, check if it matches the search arguments
54
+ if match ( platform . Name , searchArgs ) || match ( platform . Architecture , searchArgs ) {
55
+ if allVersions {
56
+ res = append ( res , platform . GetAllReleases () ... )
57
+ } else {
60
58
res = append (res , platformRelease )
61
- break
59
+ }
60
+ } else {
61
+ // if we didn't find a match in the platform data, search for
62
+ // a match in the boards manifest
63
+ for _ , board := range platformRelease .BoardsManifest {
64
+ if match (board .Name , searchArgs ) {
65
+ if allVersions {
66
+ res = append (res , platform .GetAllReleases ()... )
67
+ } else {
68
+ res = append (res , platformRelease )
69
+ }
70
+ break
71
+ }
62
72
}
63
73
}
64
74
}
0 commit comments