Skip to content

Commit 5df98ee

Browse files
author
Massimiliano Pippi
authored
Add "--all" option to "core search" to show all available versions (arduino#483)
* add an option to show all the versions * add all_versions option to the rpc proto * clean up code + docs * added integration tests
1 parent ef6463b commit 5df98ee

File tree

10 files changed

+172
-98
lines changed

10 files changed

+172
-98
lines changed

Diff for: arduino/cores/cores.go

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

145+
// GetAllReleases returns all the releases of this platform, or an empty
146+
// slice if no releases are available
147+
func (platform *Platform) GetAllReleases() []*PlatformRelease {
148+
retVal := []*PlatformRelease{}
149+
for _, v := range platform.GetAllReleasesVersions() {
150+
retVal = append(retVal, platform.FindReleaseWithVersion(v))
151+
}
152+
153+
return retVal
154+
}
155+
145156
// GetAllReleasesVersions returns all the version numbers in this Platform Package.
146157
func (platform *Platform) GetAllReleasesVersions() []*semver.Version {
147158
versions := []*semver.Version{}

Diff for: cli/core/search.go

+8-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,7 @@ 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(instance.GetId(), arguments, allVersions)
5759
if err != nil {
5860
feedback.Errorf("Error saerching for platforms: %v", err)
5961
os.Exit(errorcodes.ErrGeneric)

Diff for: 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

Diff for: 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
}

Diff for: 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(), req.GetAllVersions())
200200
}
201201

202202
// PlatformList FIXMEDOC

Diff for: rpc/commands/core.pb.go

+49-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: rpc/commands/core.proto

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ message PlatformUpgradeResp {
7070
message PlatformSearchReq {
7171
Instance instance = 1;
7272
string search_args = 2;
73+
bool all_versions = 3;
7374
}
7475

7576
message PlatformSearchResp {

Diff for: test/test_board.py

+2-30
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

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

3434

3535
@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
36-
def test_core_listall(run_command):
36+
def test_board_listall(run_command):
3737
assert run_command("core update-index")
3838
result = run_command("board listall")
3939
print(result.stderr, result.stdout)
4040
assert result.ok
4141
assert ["Board", "Name", "FQBN"] == result.stdout.splitlines()[0].strip().split()
42-
43-
44-
def test_core_search(run_command):
45-
url = "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/test_index.json"
46-
assert run_command("core update-index --additional-urls={}".format(url))
47-
# list all
48-
result = run_command("core search")
49-
assert result.ok
50-
assert 3 < len(result.stdout.splitlines())
51-
result = run_command("core search --format json")
52-
assert result.ok
53-
data = json.loads(result.stdout)
54-
assert 1 < len(data)
55-
# search a specific core
56-
result = run_command("core search avr")
57-
assert result.ok
58-
assert 2 < len(result.stdout.splitlines())
59-
result = run_command("core search avr --format json")
60-
assert result.ok
61-
data = json.loads(result.stdout)
62-
assert 0 < len(data)
63-
# additional URL
64-
result = run_command(
65-
"core search test_core --format json --additional-urls={}".format(url)
66-
)
67-
assert result.ok
68-
data = json.loads(result.stdout)
69-
assert 1 == len(data)

Diff for: test/test_core.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# This file is part of arduino-cli.
2+
#
3+
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
#
5+
# This software is released under the GNU General Public License version 3,
6+
# which covers the main part of arduino-cli.
7+
# The terms of this license can be found at:
8+
# https://www.gnu.org/licenses/gpl-3.0.en.html
9+
#
10+
# You can be released from the requirements of the above licenses by purchasing
11+
# a commercial license. Buying such a license is mandatory if you want to modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
import pytest
16+
import simplejson as json
17+
18+
from .common import running_on_ci
19+
20+
21+
def test_core_search(run_command):
22+
url = "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/test_index.json"
23+
assert run_command("core update-index --additional-urls={}".format(url))
24+
# list all
25+
result = run_command("core search")
26+
assert result.ok
27+
assert 3 < len(result.stdout.splitlines())
28+
result = run_command("core search --format json")
29+
assert result.ok
30+
data = json.loads(result.stdout)
31+
assert 1 < len(data)
32+
# search a specific core
33+
result = run_command("core search avr")
34+
assert result.ok
35+
assert 2 < len(result.stdout.splitlines())
36+
result = run_command("core search avr --format json")
37+
assert result.ok
38+
data = json.loads(result.stdout)
39+
assert 0 < len(data)
40+
# additional URL
41+
result = run_command(
42+
"core search test_core --format json --additional-urls={}".format(url)
43+
)
44+
assert result.ok
45+
data = json.loads(result.stdout)
46+
assert 1 == len(data)
47+
# show all versions
48+
result = run_command(
49+
"core search test_core --all --format json --additional-urls={}".format(url)
50+
)
51+
assert result.ok
52+
data = json.loads(result.stdout)
53+
assert 2 == len(data)

Diff for: test/testdata/test_index.json

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525
"name": "Test Board"
2626
}
2727
]
28+
},
29+
{
30+
"category": "Test Category",
31+
"help": {
32+
"online": "https://github.com/Arduino/arduino-cli"
33+
},
34+
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/additional-urls/test/testdata/core.txt",
35+
"checksum": "SHA-256:1ba93f6aea56842dfef065c0f5eb0a34c1f78b72b3f2426c94e47ba3a359c9ff",
36+
"name": "test_core",
37+
"version": "2.0.0",
38+
"architecture": "x86",
39+
"archiveFileName": "core.txt",
40+
"size": "2799",
41+
"toolsDependencies": [],
42+
"boards": [
43+
{
44+
"name": "Test Board"
45+
}
46+
]
2847
}
2948
],
3049
"tools": [],

0 commit comments

Comments
 (0)