Skip to content

Commit 276c7b1

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 3)
This patch inlines BoardList and expands the gRPC API to provide the same output given to the cli.
1 parent a71a378 commit 276c7b1

File tree

15 files changed

+129
-124
lines changed

15 files changed

+129
-124
lines changed

Diff for: commands/service.go

-11
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ type arduinoCoreServerImpl struct {
4242
versionString string
4343
}
4444

45-
// BoardList FIXMEDOC
46-
func (s *arduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListRequest) (*rpc.BoardListResponse, error) {
47-
ports, _, err := BoardList(req)
48-
if err != nil {
49-
return nil, err
50-
}
51-
return &rpc.BoardListResponse{
52-
Ports: ports,
53-
}, nil
54-
}
55-
5645
// BoardSearch exposes to the gRPC interface the board search command
5746
func (s *arduinoCoreServerImpl) BoardSearch(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchResponse, error) {
5847
return BoardSearch(ctx, req)

Diff for: commands/service_board_list.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/arduino/arduino-cli/commands/cmderrors"
3131
"github.com/arduino/arduino-cli/commands/internal/instances"
32+
f "github.com/arduino/arduino-cli/internal/algorithms"
3233
"github.com/arduino/arduino-cli/internal/arduino/cores"
3334
"github.com/arduino/arduino-cli/internal/arduino/cores/packagemanager"
3435
"github.com/arduino/arduino-cli/internal/arduino/httpclient"
@@ -204,10 +205,10 @@ func identify(pme *packagemanager.Explorer, port *discovery.Port) ([]*rpc.BoardL
204205
// BoardList returns a list of boards found by the loaded discoveries.
205206
// In case of errors partial results from discoveries that didn't fail
206207
// are returned.
207-
func BoardList(req *rpc.BoardListRequest) (r []*rpc.DetectedPort, discoveryStartErrors []error, e error) {
208+
func (s *arduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListRequest) (*rpc.BoardListResponse, error) {
208209
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
209210
if err != nil {
210-
return nil, nil, err
211+
return nil, err
211212
}
212213
defer release()
213214

@@ -216,19 +217,19 @@ func BoardList(req *rpc.BoardListRequest) (r []*rpc.DetectedPort, discoveryStart
216217
var err error
217218
fqbnFilter, err = cores.ParseFQBN(f)
218219
if err != nil {
219-
return nil, nil, &cmderrors.InvalidFQBNError{Cause: err}
220+
return nil, &cmderrors.InvalidFQBNError{Cause: err}
220221
}
221222
}
222223

223224
dm := pme.DiscoveryManager()
224-
discoveryStartErrors = dm.Start()
225+
warnings := f.Map(dm.Start(), (error).Error)
225226
time.Sleep(time.Duration(req.GetTimeout()) * time.Millisecond)
226227

227-
retVal := []*rpc.DetectedPort{}
228+
ports := []*rpc.DetectedPort{}
228229
for _, port := range dm.List() {
229230
boards, err := identify(pme, port)
230231
if err != nil {
231-
return nil, discoveryStartErrors, err
232+
warnings = append(warnings, err.Error())
232233
}
233234

234235
// boards slice can be empty at this point if neither the cores nor the
@@ -239,10 +240,13 @@ func BoardList(req *rpc.BoardListRequest) (r []*rpc.DetectedPort, discoveryStart
239240
}
240241

241242
if fqbnFilter == nil || hasMatchingBoard(b, fqbnFilter) {
242-
retVal = append(retVal, b)
243+
ports = append(ports, b)
243244
}
244245
}
245-
return retVal, discoveryStartErrors, nil
246+
return &rpc.BoardListResponse{
247+
Ports: ports,
248+
Warnings: warnings,
249+
}, nil
246250
}
247251

248252
func hasMatchingBoard(b *rpc.DetectedPort, fqbnFilter *cores.FQBN) bool {

Diff for: internal/cli/arguments/completion.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020

2121
"github.com/arduino/arduino-cli/commands"
22+
f "github.com/arduino/arduino-cli/internal/algorithms"
2223
"github.com/arduino/arduino-cli/internal/cli/instance"
2324
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2425
)
@@ -164,16 +165,11 @@ func GetInstallableLibs() []string {
164165
// GetAvailablePorts is an helper function useful to autocomplete.
165166
// It returns a list of upload port of the boards which are currently connected.
166167
// It will not suggests network ports because the timeout is not set.
167-
func GetAvailablePorts() []*rpc.Port {
168+
func GetAvailablePorts(srv rpc.ArduinoCoreServiceServer) []*rpc.Port {
169+
// Get the port list
168170
inst := instance.CreateAndInit()
171+
list, _ := srv.BoardList(context.Background(), &rpc.BoardListRequest{Instance: inst})
169172

170-
list, _, _ := commands.BoardList(&rpc.BoardListRequest{
171-
Instance: inst,
172-
})
173-
var res []*rpc.Port
174-
// transform the data structure for the completion
175-
for _, i := range list {
176-
res = append(res, i.GetPort())
177-
}
178-
return res
173+
// Transform the data structure for the completion (DetectedPort -> Port)
174+
return f.Map(list.GetPorts(), (*rpc.DetectedPort).GetPort)
179175
}

Diff for: internal/cli/arguments/fqbn.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (f *Fqbn) Set(fqbn string) {
6969
// - the port is not found, in this case nil is returned
7070
// - the FQBN autodetection fail, in this case the function prints an error and
7171
// terminates the execution
72-
func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance, defaultFQBN, defaultAddress, defaultProtocol string) (string, *rpc.Port) {
72+
func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance, srv rpc.ArduinoCoreServiceServer, defaultFQBN, defaultAddress, defaultProtocol string) (string, *rpc.Port) {
7373
fqbn := fqbnArg.String()
7474
if fqbn == "" {
7575
fqbn = defaultFQBN
@@ -78,7 +78,7 @@ func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance,
7878
if portArgs == nil || portArgs.address == "" {
7979
feedback.FatalError(&cmderrors.MissingFQBNError{}, feedback.ErrGeneric)
8080
}
81-
fqbn, port := portArgs.DetectFQBN(instance)
81+
fqbn, port := portArgs.DetectFQBN(instance, srv)
8282
if fqbn == "" {
8383
feedback.FatalError(&cmderrors.MissingFQBNError{}, feedback.ErrGeneric)
8484
}

Diff for: internal/cli/arguments/port.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ type Port struct {
3939
}
4040

4141
// AddToCommand adds the flags used to set port and protocol to the specified Command
42-
func (p *Port) AddToCommand(cmd *cobra.Command) {
42+
func (p *Port) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
4343
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
4444
cmd.RegisterFlagCompletionFunc("port", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
45-
return f.Map(GetAvailablePorts(), (*rpc.Port).GetAddress), cobra.ShellCompDirectiveDefault
45+
return f.Map(GetAvailablePorts(srv), (*rpc.Port).GetAddress), cobra.ShellCompDirectiveDefault
4646
})
4747
cmd.Flags().StringVarP(&p.protocol, "protocol", "l", "", tr("Upload port protocol, e.g: serial"))
4848
cmd.RegisterFlagCompletionFunc("protocol", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
49-
return f.Map(GetAvailablePorts(), (*rpc.Port).GetProtocol), cobra.ShellCompDirectiveDefault
49+
return f.Map(GetAvailablePorts(srv), (*rpc.Port).GetProtocol), cobra.ShellCompDirectiveDefault
5050
})
5151
p.timeout.AddToCommand(cmd)
5252
}
@@ -129,15 +129,15 @@ func (p *Port) GetSearchTimeout() time.Duration {
129129
// DetectFQBN tries to identify the board connected to the port and returns the
130130
// discovered Port object together with the FQBN. If the port does not match
131131
// exactly 1 board,
132-
func (p *Port) DetectFQBN(inst *rpc.Instance) (string, *rpc.Port) {
133-
detectedPorts, _, err := commands.BoardList(&rpc.BoardListRequest{
132+
func (p *Port) DetectFQBN(inst *rpc.Instance, srv rpc.ArduinoCoreServiceServer) (string, *rpc.Port) {
133+
detectedPorts, err := srv.BoardList(context.Background(), &rpc.BoardListRequest{
134134
Instance: inst,
135135
Timeout: p.timeout.Get().Milliseconds(),
136136
})
137137
if err != nil {
138138
feedback.Fatal(tr("Error during FQBN detection: %v", err), feedback.ErrGeneric)
139139
}
140-
for _, detectedPort := range detectedPorts {
140+
for _, detectedPort := range detectedPorts.GetPorts() {
141141
port := detectedPort.GetPort()
142142
if p.address != port.GetAddress() {
143143
continue

Diff for: internal/cli/board/attach.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func initAttachCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
4949
},
5050
}
5151
fqbn.AddToCommand(attachCommand, srv)
52-
port.AddToCommand(attachCommand)
52+
port.AddToCommand(attachCommand, srv)
5353
programmer.AddToCommand(attachCommand, srv)
5454

5555
return attachCommand

Diff for: internal/cli/board/list.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func initListCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
4646
Example: " " + os.Args[0] + " board list --discovery-timeout 10s",
4747
Args: cobra.NoArgs,
4848
Run: func(cmd *cobra.Command, args []string) {
49-
runListCommand(watch, timeoutArg.Get().Milliseconds(), fqbn.String())
49+
runListCommand(srv, watch, timeoutArg.Get().Milliseconds(), fqbn.String())
5050
},
5151
}
5252

@@ -57,7 +57,7 @@ func initListCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5757
}
5858

5959
// runListCommand detects and lists the connected arduino boards
60-
func runListCommand(watch bool, timeout int64, fqbn string) {
60+
func runListCommand(srv rpc.ArduinoCoreServiceServer, watch bool, timeout int64, fqbn string) {
6161
inst := instance.CreateAndInit()
6262

6363
logrus.Info("Executing `arduino-cli board list`")
@@ -67,11 +67,13 @@ func runListCommand(watch bool, timeout int64, fqbn string) {
6767
return
6868
}
6969

70-
ports, discoveryErrors, err := commands.BoardList(&rpc.BoardListRequest{
70+
list, err := srv.BoardList(context.Background(), &rpc.BoardListRequest{
7171
Instance: inst,
7272
Timeout: timeout,
7373
Fqbn: fqbn,
7474
})
75+
ports := list.GetPorts()
76+
discoveryErrors := list.GetWarnings()
7577
var invalidFQBNErr *cmderrors.InvalidFQBNError
7678
if errors.As(err, &invalidFQBNErr) {
7779
feedback.Fatal(tr(err.Error()), feedback.ErrBadArgument)

Diff for: internal/cli/burnbootloader/burnbootloader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5555
}
5656

5757
fqbn.AddToCommand(burnBootloaderCommand, srv)
58-
port.AddToCommand(burnBootloaderCommand)
58+
port.AddToCommand(burnBootloaderCommand, srv)
5959
programmer.AddToCommand(burnBootloaderCommand, srv)
6060
burnBootloaderCommand.Flags().BoolVarP(&verify, "verify", "t", false, tr("Verify uploaded binary after the upload."))
6161
burnBootloaderCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, tr("Turns on verbose mode."))

Diff for: internal/cli/compile/compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
116116
compileCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, tr("Optional, turns on verbose mode."))
117117
compileCommand.Flags().BoolVar(&quiet, "quiet", false, tr("Optional, suppresses almost every output."))
118118
compileCommand.Flags().BoolVarP(&uploadAfterCompile, "upload", "u", false, tr("Upload the binary after the compilation."))
119-
portArgs.AddToCommand(compileCommand)
119+
portArgs.AddToCommand(compileCommand, srv)
120120
compileCommand.Flags().BoolVarP(&verify, "verify", "t", false, tr("Verify uploaded binary after the upload."))
121121
compileCommand.Flags().StringSliceVar(&library, "library", []string{},
122122
tr("Path to a single library’s root folder. Can be used multiple times or entries can be comma separated."))
@@ -179,7 +179,7 @@ func runCompileCommand(cmd *cobra.Command, args []string, srv rpc.ArduinoCoreSer
179179
fqbnArg.Set(profile.GetFqbn())
180180
}
181181

182-
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, sk.GetDefaultFqbn(), sk.GetDefaultPort(), sk.GetDefaultProtocol())
182+
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, srv, sk.GetDefaultFqbn(), sk.GetDefaultPort(), sk.GetDefaultProtocol())
183183

184184
if keysKeychain != "" || signKey != "" || encryptKey != "" {
185185
arguments.CheckFlagsMandatory(cmd, "keys-keychain", "sign-key", "encrypt-key")

Diff for: internal/cli/debug/debug.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
6262

6363
debugCommand.AddCommand(newDebugCheckCommand(srv))
6464
fqbnArg.AddToCommand(debugCommand, srv)
65-
portArgs.AddToCommand(debugCommand)
65+
portArgs.AddToCommand(debugCommand, srv)
6666
programmer.AddToCommand(debugCommand, srv)
6767
profileArg.AddToCommand(debugCommand)
6868
debugCommand.Flags().StringVar(&interpreter, "interpreter", "console", tr("Debug interpreter e.g.: %s", "console, mi, mi1, mi2, mi3"))
@@ -101,7 +101,7 @@ func runDebugCommand(srv rpc.ArduinoCoreServiceServer, args []string, portArgs *
101101
fqbnArg.Set(profile.GetFqbn())
102102
}
103103

104-
fqbn, port := arguments.CalculateFQBNAndPort(portArgs, fqbnArg, inst, sk.GetDefaultFqbn(), sk.GetDefaultPort(), sk.GetDefaultProtocol())
104+
fqbn, port := arguments.CalculateFQBNAndPort(portArgs, fqbnArg, inst, srv, sk.GetDefaultFqbn(), sk.GetDefaultPort(), sk.GetDefaultProtocol())
105105

106106
prog := profile.GetProgrammer()
107107
if prog == "" || programmer.GetProgrammer() != "" {

Diff for: internal/cli/debug/debug_check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func newDebugCheckCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
4545
},
4646
}
4747
fqbnArg.AddToCommand(debugCheckCommand, srv)
48-
portArgs.AddToCommand(debugCheckCommand)
48+
portArgs.AddToCommand(debugCheckCommand, srv)
4949
programmer.AddToCommand(debugCheckCommand, srv)
5050
debugCheckCommand.Flags().StringVar(&interpreter, "interpreter", "console", tr("Debug interpreter e.g.: %s", "console, mi, mi1, mi2, mi3"))
5151
return debugCheckCommand

Diff for: internal/cli/monitor/monitor.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
6666
if len(args) > 0 {
6767
sketchPath = args[0]
6868
}
69-
runMonitorCmd(&portArgs, &fqbnArg, &profileArg, sketchPath, configs, describe, timestamp, quiet, raw)
69+
runMonitorCmd(srv, &portArgs, &fqbnArg, &profileArg, sketchPath, configs, describe, timestamp, quiet, raw)
7070
},
7171
}
72-
portArgs.AddToCommand(monitorCommand)
72+
portArgs.AddToCommand(monitorCommand, srv)
7373
profileArg.AddToCommand(monitorCommand)
7474
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
7575
monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
@@ -81,6 +81,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
8181
}
8282

8383
func runMonitorCmd(
84+
srv rpc.ArduinoCoreServiceServer,
8485
portArgs *arguments.Port, fqbnArg *arguments.Fqbn, profileArg *arguments.Profile, sketchPathArg string,
8586
configs []string, describe, timestamp, quiet, raw bool,
8687
) {
@@ -136,7 +137,7 @@ func runMonitorCmd(
136137
case sketch.GetDefaultFqbn() != "":
137138
fqbn = sketch.GetDefaultFqbn()
138139
default:
139-
fqbn, _ = portArgs.DetectFQBN(inst)
140+
fqbn, _ = portArgs.DetectFQBN(inst, srv)
140141
}
141142

142143
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(inst, defaultPort, defaultProtocol)

Diff for: internal/cli/upload/upload.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
6868
}
6969

7070
fqbnArg.AddToCommand(uploadCommand, srv)
71-
portArgs.AddToCommand(uploadCommand)
71+
portArgs.AddToCommand(uploadCommand, srv)
7272
profileArg.AddToCommand(uploadCommand)
7373
uploadCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries to upload."))
7474
uploadCommand.Flags().StringVarP(&importFile, "input-file", "i", "", tr("Binary file to upload."))
@@ -113,7 +113,7 @@ func runUploadCommand(srv rpc.ArduinoCoreServiceServer, args []string, uploadFie
113113
defaultFQBN := sketch.GetDefaultFqbn()
114114
defaultAddress := sketch.GetDefaultPort()
115115
defaultProtocol := sketch.GetDefaultProtocol()
116-
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, defaultFQBN, defaultAddress, defaultProtocol)
116+
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, srv, defaultFQBN, defaultAddress, defaultProtocol)
117117

118118
userFieldRes, err := commands.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
119119
Instance: inst,

0 commit comments

Comments
 (0)