Skip to content

Commit ea57c70

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 5: Create and Destroy)
1 parent 56733ee commit ea57c70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+247
-182
lines changed

Diff for: commands/instances.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
paths "github.com/arduino/go-paths-helper"
4141
"github.com/sirupsen/logrus"
4242
"google.golang.org/grpc/codes"
43+
"google.golang.org/grpc/metadata"
4344
"google.golang.org/grpc/status"
4445
)
4546

@@ -57,8 +58,16 @@ func installTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, dow
5758
return nil
5859
}
5960

60-
// Create a new CoreInstance ready to be initialized, supporting directories are also created.
61-
func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateResponse, error) {
61+
// Create a new Instance ready to be initialized, supporting directories are also created.
62+
func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
63+
var userAgent []string
64+
if md, ok := metadata.FromIncomingContext(ctx); ok {
65+
userAgent = md.Get("user-agent")
66+
}
67+
if len(userAgent) == 0 {
68+
userAgent = []string{"gRPCClientUnknown/0.0.0"}
69+
}
70+
6271
// Setup downloads directory
6372
downloadsDir := configuration.DownloadsDir(configuration.Settings)
6473
if downloadsDir.NotExist() {
@@ -78,7 +87,7 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
7887
}
7988
}
8089

81-
inst, err := instances.Create(dataDir, packagesDir, downloadsDir, extraUserAgent...)
90+
inst, err := instances.Create(dataDir, packagesDir, downloadsDir, userAgent...)
8291
if err != nil {
8392
return nil, err
8493
}
@@ -395,8 +404,8 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
395404
return nil
396405
}
397406

398-
// Destroy FIXMEDOC
399-
func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
407+
// Destroy deletes an instance.
408+
func (s *arduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
400409
if ok := instances.Delete(req.GetInstance()); !ok {
401410
return nil, &cmderrors.InvalidInstanceError{}
402411
}

Diff for: commands/service.go

-18
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/arduino/arduino-cli/commands/cmderrors"
2525
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2626
"github.com/sirupsen/logrus"
27-
"google.golang.org/grpc/metadata"
2827
)
2928

3029
// NewArduinoCoreServer returns an implementation of the ArduinoCoreService gRPC service
@@ -41,11 +40,6 @@ type arduinoCoreServerImpl struct {
4140
versionString string
4241
}
4342

44-
// Destroy FIXMEDOC
45-
func (s *arduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
46-
return Destroy(ctx, req)
47-
}
48-
4943
// UpdateIndex FIXMEDOC
5044
func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream rpc.ArduinoCoreService_UpdateIndexServer) error {
5145
syncSend := NewSynchronizedSend(stream.Send)
@@ -62,18 +56,6 @@ func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesInd
6256
)
6357
}
6458

65-
// Create FIXMEDOC
66-
func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
67-
var userAgent []string
68-
if md, ok := metadata.FromIncomingContext(ctx); ok {
69-
userAgent = md.Get("user-agent")
70-
}
71-
if len(userAgent) == 0 {
72-
userAgent = []string{"gRPCClientUnknown/0.0.0"}
73-
}
74-
return Create(req, userAgent...)
75-
}
76-
7759
// Init FIXMEDOC
7860
func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCoreService_InitServer) error {
7961
syncSend := NewSynchronizedSend(stream.Send)

Diff for: commands/service_platform_search_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package commands
1717

1818
import (
19+
"context"
1920
"testing"
2021

2122
"github.com/arduino/arduino-cli/internal/cli/configuration"
@@ -37,7 +38,9 @@ func TestPlatformSearch(t *testing.T) {
3738

3839
configuration.Settings = configuration.Init(paths.TempDir().Join("test", "arduino-cli.yaml").String())
3940

40-
createResp, err := Create(&rpc.CreateRequest{})
41+
srv := NewArduinoCoreServer("")
42+
ctx := context.Background()
43+
createResp, err := srv.Create(ctx, &rpc.CreateRequest{})
4144
require.NoError(t, err)
4245

4346
inst := createResp.GetInstance()
@@ -337,7 +340,10 @@ func TestPlatformSearchSorting(t *testing.T) {
337340

338341
configuration.Settings = configuration.Init(paths.TempDir().Join("test", "arduino-cli.yaml").String())
339342

340-
createResp, err := Create(&rpc.CreateRequest{})
343+
srv := NewArduinoCoreServer("")
344+
ctx := context.Background()
345+
346+
createResp, err := srv.Create(ctx, &rpc.CreateRequest{})
341347
require.NoError(t, err)
342348
inst := createResp.GetInstance()
343349
require.NotNil(t, inst)

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

+19-19
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
// GetInstalledBoards is an helper function useful to autocomplete.
2828
// It returns a list of fqbn
2929
// it's taken from cli/board/listall.go
30-
func GetInstalledBoards(srv rpc.ArduinoCoreServiceServer) []string {
31-
inst := instance.CreateAndInit()
30+
func GetInstalledBoards(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []string {
31+
inst := instance.CreateAndInit(srv, ctx)
3232

3333
list, _ := srv.BoardListAll(context.Background(), &rpc.BoardListAllRequest{
3434
Instance: inst,
@@ -45,8 +45,8 @@ func GetInstalledBoards(srv rpc.ArduinoCoreServiceServer) []string {
4545

4646
// GetInstalledProgrammers is an helper function useful to autocomplete.
4747
// It returns a list of programmers available based on the installed boards
48-
func GetInstalledProgrammers(srv rpc.ArduinoCoreServiceServer) []string {
49-
inst := instance.CreateAndInit()
48+
func GetInstalledProgrammers(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []string {
49+
inst := instance.CreateAndInit(srv, ctx)
5050

5151
// we need the list of the available fqbn in order to get the list of the programmers
5252
listAllReq := &rpc.BoardListAllRequest{
@@ -78,8 +78,8 @@ func GetInstalledProgrammers(srv rpc.ArduinoCoreServiceServer) []string {
7878

7979
// GetUninstallableCores is an helper function useful to autocomplete.
8080
// It returns a list of cores which can be uninstalled
81-
func GetUninstallableCores() []string {
82-
inst := instance.CreateAndInit()
81+
func GetUninstallableCores(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []string {
82+
inst := instance.CreateAndInit(srv, ctx)
8383

8484
platforms, _ := commands.PlatformSearch(&rpc.PlatformSearchRequest{
8585
Instance: inst,
@@ -99,8 +99,8 @@ func GetUninstallableCores() []string {
9999

100100
// GetInstallableCores is an helper function useful to autocomplete.
101101
// It returns a list of cores which can be installed/downloaded
102-
func GetInstallableCores() []string {
103-
inst := instance.CreateAndInit()
102+
func GetInstallableCores(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []string {
103+
inst := instance.CreateAndInit(srv, ctx)
104104

105105
platforms, _ := commands.PlatformSearch(&rpc.PlatformSearchRequest{
106106
Instance: inst,
@@ -118,18 +118,18 @@ func GetInstallableCores() []string {
118118

119119
// GetInstalledLibraries is an helper function useful to autocomplete.
120120
// It returns a list of libs which are currently installed, including the builtin ones
121-
func GetInstalledLibraries() []string {
122-
return getLibraries(true)
121+
func GetInstalledLibraries(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []string {
122+
return getLibraries(srv, ctx, true)
123123
}
124124

125125
// GetUninstallableLibraries is an helper function useful to autocomplete.
126126
// It returns a list of libs which can be uninstalled
127-
func GetUninstallableLibraries() []string {
128-
return getLibraries(false)
127+
func GetUninstallableLibraries(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []string {
128+
return getLibraries(srv, ctx, false)
129129
}
130130

131-
func getLibraries(all bool) []string {
132-
inst := instance.CreateAndInit()
131+
func getLibraries(srv rpc.ArduinoCoreServiceServer, ctx context.Context, all bool) []string {
132+
inst := instance.CreateAndInit(srv, ctx)
133133
libs, _ := commands.LibraryList(context.Background(), &rpc.LibraryListRequest{
134134
Instance: inst,
135135
All: all,
@@ -147,8 +147,8 @@ func getLibraries(all bool) []string {
147147

148148
// GetInstallableLibs is an helper function useful to autocomplete.
149149
// It returns a list of libs which can be installed/downloaded
150-
func GetInstallableLibs() []string {
151-
inst := instance.CreateAndInit()
150+
func GetInstallableLibs(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []string {
151+
inst := instance.CreateAndInit(srv, ctx)
152152

153153
libs, _ := commands.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
154154
Instance: inst,
@@ -165,10 +165,10 @@ func GetInstallableLibs() []string {
165165
// GetAvailablePorts is an helper function useful to autocomplete.
166166
// It returns a list of upload port of the boards which are currently connected.
167167
// It will not suggests network ports because the timeout is not set.
168-
func GetAvailablePorts(srv rpc.ArduinoCoreServiceServer) []*rpc.Port {
168+
func GetAvailablePorts(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []*rpc.Port {
169169
// Get the port list
170-
inst := instance.CreateAndInit()
171-
list, _ := srv.BoardList(context.Background(), &rpc.BoardListRequest{Instance: inst})
170+
inst := instance.CreateAndInit(srv, ctx)
171+
list, _ := srv.BoardList(ctx, &rpc.BoardListRequest{Instance: inst})
172172

173173
// Transform the data structure for the completion (DetectedPort -> Port)
174174
return f.Map(list.GetPorts(), (*rpc.DetectedPort).GetPort)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package arguments
1717

1818
import (
19+
"context"
1920
"strings"
2021

2122
"github.com/arduino/arduino-cli/commands/cmderrors"
@@ -36,7 +37,7 @@ type Fqbn struct {
3637
func (f *Fqbn) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
3738
cmd.Flags().StringVarP(&f.fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
3839
cmd.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
39-
return GetInstalledBoards(srv), cobra.ShellCompDirectiveDefault
40+
return GetInstalledBoards(srv, context.Background()), cobra.ShellCompDirectiveDefault
4041
})
4142
cmd.Flags().StringSliceVar(&f.boardOptions, "board-options", []string{},
4243
tr("List of board options separated by commas. Or can be used multiple times for multiple options."))

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ type Port struct {
4242
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(srv), (*rpc.Port).GetAddress), cobra.ShellCompDirectiveDefault
45+
return f.Map(GetAvailablePorts(srv, context.Background()), (*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(srv), (*rpc.Port).GetProtocol), cobra.ShellCompDirectiveDefault
49+
return f.Map(GetAvailablePorts(srv, context.Background()), (*rpc.Port).GetProtocol), cobra.ShellCompDirectiveDefault
5050
})
5151
p.timeout.AddToCommand(cmd)
5252
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Programmer struct {
3333
func (p *Programmer) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
3434
cmd.Flags().StringVarP(&p.programmer, "programmer", "P", "", tr("Programmer to use, e.g: atmel_ice"))
3535
cmd.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
36-
return GetInstalledProgrammers(srv), cobra.ShellCompDirectiveDefault
36+
return GetInstalledProgrammers(srv, context.Background()), cobra.ShellCompDirectiveDefault
3737
})
3838
}
3939

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package arguments
1717

1818
import (
19+
"context"
1920
"fmt"
2021
"strings"
2122

@@ -43,10 +44,11 @@ func (r *Reference) String() string {
4344

4445
// ParseReferences is a convenient wrapper that operates on a slice of strings and
4546
// calls ParseReference for each of them. It returns at the first invalid argument.
46-
func ParseReferences(args []string) ([]*Reference, error) {
47+
func ParseReferences(srv rpc.ArduinoCoreServiceServer, ctx context.Context, args []string) ([]*Reference, error) {
4748
ret := []*Reference{}
4849
for _, arg := range args {
49-
reference, err := ParseReference(arg)
50+
// TODO: This is quite resource consuming (since it creates a new instance for each call)
51+
reference, err := ParseReference(srv, ctx, arg)
5052
if err != nil {
5153
return nil, err
5254
}
@@ -60,7 +62,7 @@ func ParseReferences(args []string) ([]*Reference, error) {
6062
// To achieve that, it tries to use github.com/arduino/arduino-cli/commands/core.GetPlatform
6163
// Note that the Reference is returned rightaway if the arg inserted by the user matches perfectly one in the response of core.GetPlatform
6264
// A MultiplePlatformsError is returned if the platform searched by the user matches multiple platforms
63-
func ParseReference(arg string) (*Reference, error) {
65+
func ParseReference(srv rpc.ArduinoCoreServiceServer, ctx context.Context, arg string) (*Reference, error) {
6466
logrus.Infof("Parsing reference %s", arg)
6567
ret := &Reference{}
6668
if arg == "" {
@@ -96,7 +98,7 @@ func ParseReference(arg string) (*Reference, error) {
9698
// try to use core.PlatformList to optimize what the user typed
9799
// (by replacing the PackageName and Architecture in ret with the content of core.GetPlatform())
98100
platforms, _ := commands.PlatformSearch(&rpc.PlatformSearchRequest{
99-
Instance: instance.CreateAndInit(),
101+
Instance: instance.CreateAndInit(srv, ctx),
100102
})
101103
foundPlatforms := []string{}
102104
for _, platform := range platforms.GetSearchOutput() {

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
package arguments_test
1717

1818
import (
19+
"context"
1920
"testing"
2021

22+
"github.com/arduino/arduino-cli/commands"
2123
"github.com/arduino/arduino-cli/internal/cli/arguments"
2224
"github.com/arduino/arduino-cli/internal/cli/configuration"
2325
"github.com/stretchr/testify/assert"
@@ -57,14 +59,16 @@ func TestArgsStringify(t *testing.T) {
5759
}
5860

5961
func TestParseReferenceCores(t *testing.T) {
62+
srv := commands.NewArduinoCoreServer("")
63+
ctx := context.Background()
6064
for _, tt := range goodCores {
61-
actual, err := arguments.ParseReference(tt.in)
65+
actual, err := arguments.ParseReference(srv, ctx, tt.in)
6266
assert.Nil(t, err)
6367
assert.Equal(t, tt.expected, actual)
6468
}
6569

6670
for _, tt := range badCores {
67-
actual, err := arguments.ParseReference(tt.in)
71+
actual, err := arguments.ParseReference(srv, ctx, tt.in)
6872
require.NotNil(t, err, "Testing bad core '%s'", tt.in)
6973
require.Equal(t, tt.expected, actual, "Testing bad core '%s'", tt.in)
7074
}
@@ -76,7 +80,8 @@ func TestParseArgs(t *testing.T) {
7680
input = append(input, tt.in)
7781
}
7882

79-
refs, err := arguments.ParseReferences(input)
83+
srv := commands.NewArduinoCoreServer("")
84+
refs, err := arguments.ParseReferences(srv, context.Background(), input)
8085
assert.Nil(t, err)
8186
assert.Equal(t, len(goodCores), len(refs))
8287

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func initDetailsCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5656
}
5757

5858
func runDetailsCommand(srv rpc.ArduinoCoreServiceServer, fqbn string, showFullDetails, listProgrammers bool, showProperties arguments.ShowProperties) {
59-
inst := instance.CreateAndInit()
59+
ctx := context.Background()
60+
inst := instance.CreateAndInit(srv, ctx)
6061

6162
logrus.Info("Executing `arduino-cli board details`")
6263

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func initListCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5858

5959
// runListCommand detects and lists the connected arduino boards
6060
func runListCommand(srv rpc.ArduinoCoreServiceServer, watch bool, timeout int64, fqbn string) {
61-
inst := instance.CreateAndInit()
61+
ctx := context.Background()
62+
inst := instance.CreateAndInit(srv, ctx)
6263

6364
logrus.Info("Executing `arduino-cli board list`")
6465

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ for a specific board if you specify the board name`),
5252

5353
// runListAllCommand list all installed boards
5454
func runListAllCommand(args []string, srv rpc.ArduinoCoreServiceServer) {
55-
inst := instance.CreateAndInit()
55+
ctx := context.Background()
56+
inst := instance.CreateAndInit(srv, ctx)
5657

5758
logrus.Info("Executing `arduino-cli board listall`")
5859

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func initSearchCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
4949
}
5050

5151
func runSearchCommand(srv rpc.ArduinoCoreServiceServer, args []string) {
52-
inst := instance.CreateAndInit()
52+
ctx := context.Background()
53+
inst := instance.CreateAndInit(srv, ctx)
5354

5455
logrus.Info("Executing `arduino-cli board search`")
5556

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
6666
}
6767

6868
func runBootloaderCommand(srv rpc.ArduinoCoreServiceServer) {
69-
instance := instance.CreateAndInit()
69+
ctx := context.Background()
70+
instance := instance.CreateAndInit(srv, ctx)
7071

7172
logrus.Info("Executing `arduino-cli burn-bootloader`")
7273

0 commit comments

Comments
 (0)