Skip to content

Commit

Permalink
Add "--all" option to "core search" to show all available versions (#483
Browse files Browse the repository at this point in the history
)

* add an option to show all the versions

* add all_versions option to the rpc proto

* clean up code + docs

* added integration tests
  • Loading branch information
Massimiliano Pippi committed Nov 19, 2019
1 parent ef6463b commit 5df98ee
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 98 deletions.
11 changes: 11 additions & 0 deletions arduino/cores/cores.go
Expand Up @@ -142,6 +142,17 @@ func (platform *Platform) GetLatestRelease() *PlatformRelease {
return platform.FindReleaseWithVersion(latestVersion)
}

// GetAllReleases returns all the releases of this platform, or an empty
// slice if no releases are available
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
14 changes: 8 additions & 6 deletions cli/core/search.go
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,7 @@ 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(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
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
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
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
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
32 changes: 2 additions & 30 deletions test/test_board.py
Expand Up @@ -19,7 +19,7 @@


@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
def test_core_list(run_command):
def test_board_list(run_command):
result = run_command("core update-index")
assert result.ok
result = run_command("board list --format json")
Expand All @@ -33,37 +33,9 @@ def test_core_list(run_command):


@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
def test_core_listall(run_command):
def test_board_listall(run_command):
assert run_command("core update-index")
result = run_command("board listall")
print(result.stderr, result.stdout)
assert result.ok
assert ["Board", "Name", "FQBN"] == result.stdout.splitlines()[0].strip().split()


def test_core_search(run_command):
url = "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/test_index.json"
assert run_command("core update-index --additional-urls={}".format(url))
# list all
result = run_command("core search")
assert result.ok
assert 3 < len(result.stdout.splitlines())
result = run_command("core search --format json")
assert result.ok
data = json.loads(result.stdout)
assert 1 < len(data)
# search a specific core
result = run_command("core search avr")
assert result.ok
assert 2 < len(result.stdout.splitlines())
result = run_command("core search avr --format json")
assert result.ok
data = json.loads(result.stdout)
assert 0 < len(data)
# additional URL
result = run_command(
"core search test_core --format json --additional-urls={}".format(url)
)
assert result.ok
data = json.loads(result.stdout)
assert 1 == len(data)
53 changes: 53 additions & 0 deletions test/test_core.py
@@ -0,0 +1,53 @@
# This file is part of arduino-cli.
#
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
#
# This software is released under the GNU General Public License version 3,
# which covers the main part of arduino-cli.
# The terms of this license can be found at:
# https://www.gnu.org/licenses/gpl-3.0.en.html
#
# You can be released from the requirements of the above licenses by purchasing
# a commercial license. Buying such a license is mandatory if you want to modify or
# otherwise use the software for commercial activities involving the Arduino
# software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to license@arduino.cc.
import pytest
import simplejson as json

from .common import running_on_ci


def test_core_search(run_command):
url = "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/test_index.json"
assert run_command("core update-index --additional-urls={}".format(url))
# list all
result = run_command("core search")
assert result.ok
assert 3 < len(result.stdout.splitlines())
result = run_command("core search --format json")
assert result.ok
data = json.loads(result.stdout)
assert 1 < len(data)
# search a specific core
result = run_command("core search avr")
assert result.ok
assert 2 < len(result.stdout.splitlines())
result = run_command("core search avr --format json")
assert result.ok
data = json.loads(result.stdout)
assert 0 < len(data)
# additional URL
result = run_command(
"core search test_core --format json --additional-urls={}".format(url)
)
assert result.ok
data = json.loads(result.stdout)
assert 1 == len(data)
# show all versions
result = run_command(
"core search test_core --all --format json --additional-urls={}".format(url)
)
assert result.ok
data = json.loads(result.stdout)
assert 2 == len(data)
19 changes: 19 additions & 0 deletions test/testdata/test_index.json
Expand Up @@ -25,6 +25,25 @@
"name": "Test Board"
}
]
},
{
"category": "Test Category",
"help": {
"online": "https://github.com/Arduino/arduino-cli"
},
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/additional-urls/test/testdata/core.txt",
"checksum": "SHA-256:1ba93f6aea56842dfef065c0f5eb0a34c1f78b72b3f2426c94e47ba3a359c9ff",
"name": "test_core",
"version": "2.0.0",
"architecture": "x86",
"archiveFileName": "core.txt",
"size": "2799",
"toolsDependencies": [],
"boards": [
{
"name": "Test Board"
}
]
}
],
"tools": [],
Expand Down

0 comments on commit 5df98ee

Please sign in to comment.