1818package core
1919
2020import (
21- "context"
2221 "errors"
2322 "regexp"
2423 "strings"
@@ -28,37 +27,48 @@ import (
2827 rpc "github.com/arduino/arduino-cli/rpc/commands"
2928)
3029
30+ func match (line , searchArgs string ) bool {
31+ return strings .Contains (strings .ToLower (line ), searchArgs )
32+ }
33+
3134// 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 )
3437 if pm == nil {
3538 return nil , errors .New ("invalid instance" )
3639 }
3740
38- search := req .SearchArgs
39-
4041 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 :]
4344 res = pm .FindPlatformReleaseProvidingBoardsWithVidPid (vid , pid )
4445 } else {
45- match := func (line string ) bool {
46- return strings .Contains (strings .ToLower (line ), search )
47- }
4846 for _ , targetPackage := range pm .Packages {
4947 for _ , platform := range targetPackage .Platforms {
5048 platformRelease := platform .GetLatestRelease ()
5149 if platformRelease == nil {
5250 continue
5351 }
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 {
6058 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+ }
6272 }
6373 }
6474 }
0 commit comments