Skip to content

Commit 2f3e812

Browse files
author
Massimiliano Pippi
committed
add an option to show all the versions
1 parent ef6463b commit 2f3e812

File tree

5 files changed

+51
-29
lines changed

5 files changed

+51
-29
lines changed

arduino/cores/cores.go

+10
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ func (platform *Platform) GetLatestRelease() *PlatformRelease {
142142
return platform.FindReleaseWithVersion(latestVersion)
143143
}
144144

145+
// GetAllReleases TODO
146+
func (platform *Platform) GetAllReleases() []*PlatformRelease {
147+
retVal := []*PlatformRelease{}
148+
for _, v := range platform.GetAllReleasesVersions() {
149+
retVal = append(retVal, platform.FindReleaseWithVersion(v))
150+
}
151+
152+
return retVal
153+
}
154+
145155
// GetAllReleasesVersions returns all the version numbers in this Platform Package.
146156
func (platform *Platform) GetAllReleasesVersions() []*semver.Version {
147157
versions := []*semver.Version{}

cli/core/search.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package core
1919

2020
import (
21-
"context"
2221
"os"
2322
"sort"
2423
"strings"
@@ -33,15 +32,21 @@ import (
3332
"github.com/spf13/cobra"
3433
)
3534

35+
var (
36+
allVersions bool
37+
)
38+
3639
func initSearchCommand() *cobra.Command {
3740
searchCommand := &cobra.Command{
3841
Use: "search <keywords...>",
3942
Short: "Search for a core in the package index.",
4043
Long: "Search for a core in the package index using the specified keywords.",
41-
Example: " " + os.Args[0] + " core search MKRZero -v",
44+
Example: " " + os.Args[0] + " core search MKRZero -a -v",
4245
Args: cobra.ArbitraryArgs,
4346
Run: runSearchCommand,
4447
}
48+
searchCommand.Flags().BoolVarP(&allVersions, "all", "a", false, "Show all available core versions.")
49+
4550
return searchCommand
4651
}
4752

@@ -50,10 +55,11 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
5055
logrus.Info("Executing `arduino core search`")
5156

5257
arguments := strings.ToLower(strings.Join(args, " "))
53-
resp, err := core.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
54-
Instance: instance,
55-
SearchArgs: arguments,
56-
})
58+
// resp, err := core.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
59+
// Instance: instance,
60+
// SearchArgs: arguments,
61+
// })
62+
resp, err := core.PlatformSearch(instance.GetId(), arguments, allVersions)
5763
if err != nil {
5864
feedback.Errorf("Error saerching for platforms: %v", err)
5965
os.Exit(errorcodes.ErrGeneric)

commands/core/core.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ func PlatformReleaseToRPC(platformRelease *cores.PlatformRelease) *rpc.Platform
5959
Website: platformRelease.Platform.Package.WebsiteURL,
6060
Email: platformRelease.Platform.Package.Email,
6161
Boards: boards,
62-
}
63-
64-
latest := platformRelease.Platform.GetLatestRelease()
65-
if latest != nil {
66-
result.Latest = latest.Version.String()
62+
Latest: platformRelease.Version.String(),
6763
}
6864

6965
return result

commands/core/search.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package core
1919

2020
import (
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
}

commands/daemon/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (s *ArduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeReq, str
196196

197197
// PlatformSearch FIXMEDOC
198198
func (s *ArduinoCoreServerImpl) PlatformSearch(ctx context.Context, req *rpc.PlatformSearchReq) (*rpc.PlatformSearchResp, error) {
199-
return core.PlatformSearch(ctx, req)
199+
return core.PlatformSearch(req.GetInstance().GetId(), req.GetSearchArgs(), false)
200200
}
201201

202202
// PlatformList FIXMEDOC

0 commit comments

Comments
 (0)