Skip to content

Add "--all" option to "core search" to show all available versions #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions arduino/cores/cores.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ func (platform *Platform) GetLatestRelease() *PlatformRelease {
return platform.FindReleaseWithVersion(latestVersion)
}

// GetAllReleases TODO
func (platform *Platform) GetAllReleases() []*PlatformRelease {
retVal := []*PlatformRelease{}
for _, v := range platform.GetAllReleasesVersions() {
retVal = append(retVal, platform.FindReleaseWithVersion(v))
}

return retVal
}

// GetAllReleasesVersions returns all the version numbers in this Platform Package.
func (platform *Platform) GetAllReleasesVersions() []*semver.Version {
versions := []*semver.Version{}
Expand Down
18 changes: 12 additions & 6 deletions cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package core

import (
"context"
"os"
"sort"
"strings"
Expand All @@ -33,15 +32,21 @@ import (
"github.com/spf13/cobra"
)

var (
allVersions bool
)

func initSearchCommand() *cobra.Command {
searchCommand := &cobra.Command{
Use: "search <keywords...>",
Short: "Search for a core in the package index.",
Long: "Search for a core in the package index using the specified keywords.",
Example: " " + os.Args[0] + " core search MKRZero -v",
Example: " " + os.Args[0] + " core search MKRZero -a -v",
Args: cobra.ArbitraryArgs,
Run: runSearchCommand,
}
searchCommand.Flags().BoolVarP(&allVersions, "all", "a", false, "Show all available core versions.")

return searchCommand
}

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

arguments := strings.ToLower(strings.Join(args, " "))
resp, err := core.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
Instance: instance,
SearchArgs: arguments,
})
// resp, err := core.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
// Instance: instance,
// SearchArgs: arguments,
// })
resp, err := core.PlatformSearch(instance.GetId(), arguments, allVersions)
if err != nil {
feedback.Errorf("Error saerching for platforms: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
6 changes: 1 addition & 5 deletions commands/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ func PlatformReleaseToRPC(platformRelease *cores.PlatformRelease) *rpc.Platform
Website: platformRelease.Platform.Package.WebsiteURL,
Email: platformRelease.Platform.Package.Email,
Boards: boards,
}

latest := platformRelease.Platform.GetLatestRelease()
if latest != nil {
result.Latest = latest.Version.String()
Latest: platformRelease.Version.String(),
}

return result
Expand Down
44 changes: 27 additions & 17 deletions commands/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package core

import (
"context"
"errors"
"regexp"
"strings"
Expand All @@ -28,37 +27,48 @@ import (
rpc "github.com/arduino/arduino-cli/rpc/commands"
)

func match(line, searchArgs string) bool {
return strings.Contains(strings.ToLower(line), searchArgs)
}

// PlatformSearch FIXMEDOC
func PlatformSearch(ctx context.Context, req *rpc.PlatformSearchReq) (*rpc.PlatformSearchResp, error) {
pm := commands.GetPackageManager(req.GetInstance().GetId())
func PlatformSearch(instanceID int32, searchArgs string, allVersions bool) (*rpc.PlatformSearchResp, error) {
pm := commands.GetPackageManager(instanceID)
if pm == nil {
return nil, errors.New("invalid instance")
}

search := req.SearchArgs

res := []*cores.PlatformRelease{}
if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", search); isUsb {
vid, pid := search[:4], search[5:]
if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", searchArgs); isUsb {
vid, pid := searchArgs[:4], searchArgs[5:]
res = pm.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
} else {
match := func(line string) bool {
return strings.Contains(strings.ToLower(line), search)
}
for _, targetPackage := range pm.Packages {
for _, platform := range targetPackage.Platforms {
platformRelease := platform.GetLatestRelease()
if platformRelease == nil {
continue
}
if match(platform.Name) || match(platform.Architecture) {
res = append(res, platformRelease)
continue
}
for _, board := range platformRelease.BoardsManifest {
if match(board.Name) {

// platform has a release, check if it matches the search arguments
if match(platform.Name, searchArgs) || match(platform.Architecture, searchArgs) {
if allVersions {
res = append(res, platform.GetAllReleases()...)
} else {
res = append(res, platformRelease)
break
}
} else {
// if we didn't find a match in the platform data, search for
// a match in the boards manifest
for _, board := range platformRelease.BoardsManifest {
if match(board.Name, searchArgs) {
if allVersions {
res = append(res, platform.GetAllReleases()...)
} else {
res = append(res, platformRelease)
}
break
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion commands/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (s *ArduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeReq, str

// PlatformSearch FIXMEDOC
func (s *ArduinoCoreServerImpl) PlatformSearch(ctx context.Context, req *rpc.PlatformSearchReq) (*rpc.PlatformSearchResp, error) {
return core.PlatformSearch(ctx, req)
return core.PlatformSearch(req.GetInstance().GetId(), req.GetSearchArgs(), req.GetAllVersions())
}

// PlatformList FIXMEDOC
Expand Down
88 changes: 49 additions & 39 deletions rpc/commands/core.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rpc/commands/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ message PlatformUpgradeResp {
message PlatformSearchReq {
Instance instance = 1;
string search_args = 2;
bool all_versions = 3;
}

message PlatformSearchResp {
Expand Down