Skip to content

Commit 890546e

Browse files
committed
porting of list and search core command
1 parent b320404 commit 890546e

File tree

10 files changed

+806
-172
lines changed

10 files changed

+806
-172
lines changed

cli/core/list.go

+10-34
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
package core
1919

2020
import (
21+
"context"
22+
2123
"github.com/arduino/arduino-cli/cli"
22-
"github.com/arduino/arduino-cli/common/formatter"
23-
"github.com/arduino/arduino-cli/common/formatter/output"
24-
"github.com/sirupsen/logrus"
24+
"github.com/arduino/arduino-cli/commands/core"
25+
"github.com/arduino/arduino-cli/output"
26+
"github.com/arduino/arduino-cli/rpc"
2527
"github.com/spf13/cobra"
26-
semver "go.bug.st/relaxed-semver"
2728
)
2829

2930
func initListCommand() *cobra.Command {
@@ -44,34 +45,9 @@ var listFlags struct {
4445
}
4546

4647
func runListCommand(cmd *cobra.Command, args []string) {
47-
logrus.Info("Executing `arduino core list`")
48-
49-
pm, _ := cli.InitPackageAndLibraryManager()
50-
51-
installed := []*output.InstalledPlatform{}
52-
for _, targetPackage := range pm.GetPackages().Packages {
53-
for _, platform := range targetPackage.Platforms {
54-
if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil {
55-
if listFlags.updatableOnly {
56-
if latest := platform.GetLatestRelease(); latest == nil || latest == platformRelease {
57-
continue
58-
}
59-
}
60-
var latestVersion *semver.Version
61-
if latest := platformRelease.Platform.GetLatestRelease(); latest != nil {
62-
latestVersion = latest.Version
63-
}
64-
installed = append(installed, &output.InstalledPlatform{
65-
ID: platformRelease.String(),
66-
Installed: platformRelease.Version,
67-
Latest: latestVersion,
68-
Name: platformRelease.Platform.Name,
69-
})
70-
}
71-
}
72-
}
73-
74-
if len(installed) > 0 {
75-
formatter.Print(output.InstalledPlatforms{Platforms: installed})
76-
}
48+
instance := cli.CreateInstance()
49+
core.PlatformList(context.Background(), &rpc.PlatformListReq{
50+
Instance: instance,
51+
UpdatableOnly: listFlags.updatableOnly,
52+
}, output.NewTaskProgressCB())
7753
}

cli/core/search.go

+13-52
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
package core
1919

2020
import (
21-
"regexp"
22-
"strings"
21+
"context"
2322

24-
"github.com/arduino/arduino-cli/common/formatter/output"
23+
"strings"
2524

26-
"github.com/arduino/arduino-cli/arduino/cores"
2725
"github.com/arduino/arduino-cli/cli"
28-
"github.com/arduino/arduino-cli/common/formatter"
26+
"github.com/arduino/arduino-cli/commands/core"
27+
"github.com/arduino/arduino-cli/output"
28+
"github.com/arduino/arduino-cli/rpc"
29+
"github.com/sirupsen/logrus"
2930
"github.com/spf13/cobra"
3031
)
3132

@@ -42,51 +43,11 @@ func initSearchCommand() *cobra.Command {
4243
}
4344

4445
func runSearchCommand(cmd *cobra.Command, args []string) {
45-
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
46-
47-
search := strings.ToLower(strings.Join(args, " "))
48-
formatter.Print("Searching for platforms matching '" + search + "'")
49-
formatter.Print("")
50-
51-
res := []*cores.PlatformRelease{}
52-
if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", search); isUsb {
53-
vid, pid := search[:4], search[5:]
54-
res = pm.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
55-
} else {
56-
match := func(line string) bool {
57-
return strings.Contains(strings.ToLower(line), search)
58-
}
59-
for _, targetPackage := range pm.GetPackages().Packages {
60-
for _, platform := range targetPackage.Platforms {
61-
platformRelease := platform.GetLatestRelease()
62-
if platformRelease == nil {
63-
continue
64-
}
65-
if match(platform.Name) || match(platform.Architecture) {
66-
res = append(res, platformRelease)
67-
continue
68-
}
69-
for _, board := range platformRelease.BoardsManifest {
70-
if match(board.Name) {
71-
res = append(res, platformRelease)
72-
break
73-
}
74-
}
75-
}
76-
}
77-
}
78-
79-
if len(res) == 0 {
80-
formatter.Print("No platforms matching your search")
81-
} else {
82-
out := []*output.SearchedPlatform{}
83-
for _, platformRelease := range res {
84-
out = append(out, &output.SearchedPlatform{
85-
ID: platformRelease.Platform.String(),
86-
Name: platformRelease.Platform.Name,
87-
Version: platformRelease.Version,
88-
})
89-
}
90-
formatter.Print(output.SearchedPlatforms{Platforms: out})
91-
}
46+
logrus.Info("Executing `arduino core search`")
47+
instance := cli.CreateInstance()
48+
arguments := strings.ToLower(strings.Join(args, " "))
49+
core.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
50+
Instance: instance,
51+
SearchArgs: arguments,
52+
}, output.NewTaskProgressCB())
9253
}

commands/core/list.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package core
19+
20+
import (
21+
"context"
22+
23+
"github.com/arduino/arduino-cli/commands"
24+
"github.com/arduino/arduino-cli/rpc"
25+
"github.com/sirupsen/logrus"
26+
semver "go.bug.st/relaxed-semver"
27+
)
28+
29+
func PlatformList(ctx context.Context, req *rpc.PlatformListReq, taskCB commands.TaskProgressCB) (*rpc.PlatformListResp, error) {
30+
logrus.Info("Executing `arduino core list`")
31+
pm := commands.GetPackageManager(req)
32+
taskCB(&rpc.TaskProgress{Name: "List installed platform", Completed: true})
33+
installed := []*rpc.InstalledPlatform{}
34+
for _, targetPackage := range pm.GetPackages().Packages {
35+
for _, platform := range targetPackage.Platforms {
36+
if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil {
37+
if req.GetUpdatableOnly() {
38+
if latest := platform.GetLatestRelease(); latest == nil || latest == platformRelease {
39+
continue
40+
}
41+
}
42+
var latestVersion *semver.Version
43+
if latest := platformRelease.Platform.GetLatestRelease(); latest != nil {
44+
latestVersion = latest.Version
45+
}
46+
installed = append(installed, &rpc.InstalledPlatform{
47+
ID: platformRelease.String(),
48+
Installed: platformRelease.Version.String(),
49+
Latest: latestVersion.String(),
50+
Name: platformRelease.Platform.Name,
51+
})
52+
}
53+
}
54+
}
55+
56+
// if len(installed) > 0 {
57+
// //formatter.Print(output.InstalledPlatforms{Platforms: installed})
58+
// }
59+
return &rpc.PlatformListResp{InstalledPlatform: installed}, nil
60+
}

commands/core/search.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package core
19+
20+
import (
21+
"context"
22+
"regexp"
23+
"strings"
24+
25+
"github.com/arduino/arduino-cli/commands"
26+
"github.com/arduino/arduino-cli/rpc"
27+
28+
"github.com/arduino/arduino-cli/arduino/cores"
29+
)
30+
31+
func PlatformSearch(ctx context.Context, req *rpc.PlatformSearchReq, taskCB commands.TaskProgressCB) (*rpc.PlatformSearchResp, error) {
32+
pm := commands.GetPackageManager(req)
33+
34+
search := req.SearchArgs
35+
taskCB(&rpc.TaskProgress{Name: "Searching for matching '" + search + "'"})
36+
37+
res := []*cores.PlatformRelease{}
38+
if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", search); isUsb {
39+
vid, pid := search[:4], search[5:]
40+
res = pm.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
41+
} else {
42+
match := func(line string) bool {
43+
return strings.Contains(strings.ToLower(line), search)
44+
}
45+
for _, targetPackage := range pm.GetPackages().Packages {
46+
for _, platform := range targetPackage.Platforms {
47+
platformRelease := platform.GetLatestRelease()
48+
if platformRelease == nil {
49+
continue
50+
}
51+
if match(platform.Name) || match(platform.Architecture) {
52+
res = append(res, platformRelease)
53+
continue
54+
}
55+
for _, board := range platformRelease.BoardsManifest {
56+
if match(board.Name) {
57+
res = append(res, platformRelease)
58+
break
59+
}
60+
}
61+
}
62+
}
63+
}
64+
out := []*rpc.SearchOutput{}
65+
if len(res) == 0 {
66+
taskCB(&rpc.TaskProgress{Message: "No platforms matching your search", Completed: true})
67+
} else {
68+
69+
for _, platformRelease := range res {
70+
out = append(out, &rpc.SearchOutput{
71+
ID: platformRelease.Platform.String(),
72+
Name: platformRelease.Platform.Name,
73+
Version: platformRelease.Version.String(),
74+
})
75+
}
76+
taskCB(&rpc.TaskProgress{Completed: true})
77+
}
78+
79+
return &rpc.PlatformSearchResp{SearchOutput: out}, nil
80+
}

daemon/client/client.go

+57
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,63 @@ func main() {
187187
}
188188
}
189189

190+
searchRespStream, err := client.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
191+
Instance: instance,
192+
SearchArgs: "uno",
193+
})
194+
if err != nil {
195+
fmt.Printf("Search error: %s\n", err)
196+
os.Exit(1)
197+
}
198+
for {
199+
searchResp, err := searchRespStream.Recv()
200+
if err == io.EOF {
201+
break
202+
}
203+
if err != nil {
204+
fmt.Printf("Compile error: %s\n", err)
205+
os.Exit(1)
206+
}
207+
if searchResp.GetTaskProgress() != nil {
208+
fmt.Printf(">> TASK: %s\n", searchResp.GetTaskProgress())
209+
}
210+
if searchResp.GetSearchOutput() != nil {
211+
serchedOutput := searchResp.GetSearchOutput()
212+
for _, outsearch := range serchedOutput {
213+
fmt.Printf(">> OUTPUT: ID: %s\t Version: %s\t name: %s\n", outsearch.GetID(), outsearch.GetVersion(), outsearch.GetName())
214+
}
215+
216+
}
217+
}
218+
219+
listRespStream, err := client.PlatformList(context.Background(), &rpc.PlatformListReq{
220+
Instance: instance,
221+
})
222+
if err != nil {
223+
fmt.Printf("Search error: %s\n", err)
224+
os.Exit(1)
225+
}
226+
for {
227+
listResp, err := listRespStream.Recv()
228+
if err == io.EOF {
229+
break
230+
}
231+
if err != nil {
232+
fmt.Printf("Compile error: %s\n", err)
233+
os.Exit(1)
234+
}
235+
if listResp.GetTaskProgress() != nil {
236+
fmt.Printf(">> TASK: %s\n", listResp.GetTaskProgress())
237+
}
238+
if listResp.GetInstalledPlatform() != nil {
239+
Installedplatforms := listResp.GetInstalledPlatform()
240+
for _, listpfm := range Installedplatforms {
241+
fmt.Printf(">> OUTPUT: ID: %s\t Installed Version: %s\t Latest Version: %s\t name: %s\n", listpfm.GetID(), listpfm.GetInstalled(), listpfm.GetLatest(), listpfm.GetName())
242+
}
243+
244+
}
245+
}
246+
190247
uninstallRespStream, err := client.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
191248
Instance: instance,
192249
PlatformPackage: "arduino",

daemon/daemon.go

+22
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ func (s *ArduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeReq, str
122122
return stream.Send(resp)
123123
}
124124

125+
func (s *ArduinoCoreServerImpl) PlatformSearch(req *rpc.PlatformSearchReq, stream rpc.ArduinoCore_PlatformSearchServer) error {
126+
127+
resp, err := core.PlatformSearch(stream.Context(), req,
128+
func(p *rpc.TaskProgress) { stream.Send(&rpc.PlatformSearchResp{TaskProgress: p}) },
129+
)
130+
if err != nil {
131+
return err
132+
}
133+
return stream.Send(resp)
134+
}
135+
136+
func (s *ArduinoCoreServerImpl) PlatformList(req *rpc.PlatformListReq, stream rpc.ArduinoCore_PlatformListServer) error {
137+
138+
resp, err := core.PlatformList(stream.Context(), req,
139+
func(p *rpc.TaskProgress) { stream.Send(&rpc.PlatformListResp{TaskProgress: p}) },
140+
)
141+
if err != nil {
142+
return err
143+
}
144+
return stream.Send(resp)
145+
}
146+
125147
func (s *ArduinoCoreServerImpl) Upload(req *rpc.UploadReq, stream rpc.ArduinoCore_UploadServer) error {
126148
resp, err := upload.Upload(
127149
stream.Context(), req,

0 commit comments

Comments
 (0)