Skip to content

Commit

Permalink
Inlining methods in ArduinoCoreServiceImpl (part 5: Create and Destroy)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Mar 14, 2024
1 parent 56733ee commit ea57c70
Show file tree
Hide file tree
Showing 42 changed files with 247 additions and 182 deletions.
19 changes: 14 additions & 5 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
paths "github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

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

// Create a new CoreInstance ready to be initialized, supporting directories are also created.
func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateResponse, error) {
// Create a new Instance ready to be initialized, supporting directories are also created.
func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
var userAgent []string
if md, ok := metadata.FromIncomingContext(ctx); ok {
userAgent = md.Get("user-agent")
}
if len(userAgent) == 0 {
userAgent = []string{"gRPCClientUnknown/0.0.0"}
}

// Setup downloads directory
downloadsDir := configuration.DownloadsDir(configuration.Settings)
if downloadsDir.NotExist() {
Expand All @@ -78,7 +87,7 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
}
}

inst, err := instances.Create(dataDir, packagesDir, downloadsDir, extraUserAgent...)
inst, err := instances.Create(dataDir, packagesDir, downloadsDir, userAgent...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -395,8 +404,8 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
return nil
}

// Destroy FIXMEDOC
func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
// Destroy deletes an instance.
func (s *arduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
if ok := instances.Delete(req.GetInstance()); !ok {
return nil, &cmderrors.InvalidInstanceError{}
}
Expand Down
18 changes: 0 additions & 18 deletions commands/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/arduino/arduino-cli/commands/cmderrors"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/metadata"
)

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

// Destroy FIXMEDOC
func (s *arduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
return Destroy(ctx, req)
}

// UpdateIndex FIXMEDOC
func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream rpc.ArduinoCoreService_UpdateIndexServer) error {
syncSend := NewSynchronizedSend(stream.Send)
Expand All @@ -62,18 +56,6 @@ func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesInd
)
}

// Create FIXMEDOC
func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
var userAgent []string
if md, ok := metadata.FromIncomingContext(ctx); ok {
userAgent = md.Get("user-agent")
}
if len(userAgent) == 0 {
userAgent = []string{"gRPCClientUnknown/0.0.0"}
}
return Create(req, userAgent...)
}

// Init FIXMEDOC
func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCoreService_InitServer) error {
syncSend := NewSynchronizedSend(stream.Send)
Expand Down
10 changes: 8 additions & 2 deletions commands/service_platform_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package commands

import (
"context"
"testing"

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

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

createResp, err := Create(&rpc.CreateRequest{})
srv := NewArduinoCoreServer("")
ctx := context.Background()
createResp, err := srv.Create(ctx, &rpc.CreateRequest{})
require.NoError(t, err)

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

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

createResp, err := Create(&rpc.CreateRequest{})
srv := NewArduinoCoreServer("")
ctx := context.Background()

createResp, err := srv.Create(ctx, &rpc.CreateRequest{})
require.NoError(t, err)
inst := createResp.GetInstance()
require.NotNil(t, inst)
Expand Down
38 changes: 19 additions & 19 deletions internal/cli/arguments/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
// GetInstalledBoards is an helper function useful to autocomplete.
// It returns a list of fqbn
// it's taken from cli/board/listall.go
func GetInstalledBoards(srv rpc.ArduinoCoreServiceServer) []string {
inst := instance.CreateAndInit()
func GetInstalledBoards(srv rpc.ArduinoCoreServiceServer, ctx context.Context) []string {
inst := instance.CreateAndInit(srv, ctx)

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

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

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

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

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

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

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

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

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

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

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

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

// Transform the data structure for the completion (DetectedPort -> Port)
return f.Map(list.GetPorts(), (*rpc.DetectedPort).GetPort)
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/arguments/fqbn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package arguments

import (
"context"
"strings"

"github.com/arduino/arduino-cli/commands/cmderrors"
Expand All @@ -36,7 +37,7 @@ type Fqbn struct {
func (f *Fqbn) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
cmd.Flags().StringVarP(&f.fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
cmd.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetInstalledBoards(srv), cobra.ShellCompDirectiveDefault
return GetInstalledBoards(srv, context.Background()), cobra.ShellCompDirectiveDefault
})
cmd.Flags().StringSliceVar(&f.boardOptions, "board-options", []string{},
tr("List of board options separated by commas. Or can be used multiple times for multiple options."))
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/arguments/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ type Port struct {
func (p *Port) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
cmd.RegisterFlagCompletionFunc("port", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return f.Map(GetAvailablePorts(srv), (*rpc.Port).GetAddress), cobra.ShellCompDirectiveDefault
return f.Map(GetAvailablePorts(srv, context.Background()), (*rpc.Port).GetAddress), cobra.ShellCompDirectiveDefault
})
cmd.Flags().StringVarP(&p.protocol, "protocol", "l", "", tr("Upload port protocol, e.g: serial"))
cmd.RegisterFlagCompletionFunc("protocol", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return f.Map(GetAvailablePorts(srv), (*rpc.Port).GetProtocol), cobra.ShellCompDirectiveDefault
return f.Map(GetAvailablePorts(srv, context.Background()), (*rpc.Port).GetProtocol), cobra.ShellCompDirectiveDefault
})
p.timeout.AddToCommand(cmd)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/arguments/programmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Programmer struct {
func (p *Programmer) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
cmd.Flags().StringVarP(&p.programmer, "programmer", "P", "", tr("Programmer to use, e.g: atmel_ice"))
cmd.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetInstalledProgrammers(srv), cobra.ShellCompDirectiveDefault
return GetInstalledProgrammers(srv, context.Background()), cobra.ShellCompDirectiveDefault
})
}

Expand Down
10 changes: 6 additions & 4 deletions internal/cli/arguments/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package arguments

import (
"context"
"fmt"
"strings"

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

// ParseReferences is a convenient wrapper that operates on a slice of strings and
// calls ParseReference for each of them. It returns at the first invalid argument.
func ParseReferences(args []string) ([]*Reference, error) {
func ParseReferences(srv rpc.ArduinoCoreServiceServer, ctx context.Context, args []string) ([]*Reference, error) {
ret := []*Reference{}
for _, arg := range args {
reference, err := ParseReference(arg)
// TODO: This is quite resource consuming (since it creates a new instance for each call)
reference, err := ParseReference(srv, ctx, arg)
if err != nil {
return nil, err
}
Expand All @@ -60,7 +62,7 @@ func ParseReferences(args []string) ([]*Reference, error) {
// To achieve that, it tries to use github.com/arduino/arduino-cli/commands/core.GetPlatform
// Note that the Reference is returned rightaway if the arg inserted by the user matches perfectly one in the response of core.GetPlatform
// A MultiplePlatformsError is returned if the platform searched by the user matches multiple platforms
func ParseReference(arg string) (*Reference, error) {
func ParseReference(srv rpc.ArduinoCoreServiceServer, ctx context.Context, arg string) (*Reference, error) {
logrus.Infof("Parsing reference %s", arg)
ret := &Reference{}
if arg == "" {
Expand Down Expand Up @@ -96,7 +98,7 @@ func ParseReference(arg string) (*Reference, error) {
// try to use core.PlatformList to optimize what the user typed
// (by replacing the PackageName and Architecture in ret with the content of core.GetPlatform())
platforms, _ := commands.PlatformSearch(&rpc.PlatformSearchRequest{
Instance: instance.CreateAndInit(),
Instance: instance.CreateAndInit(srv, ctx),
})
foundPlatforms := []string{}
for _, platform := range platforms.GetSearchOutput() {
Expand Down
11 changes: 8 additions & 3 deletions internal/cli/arguments/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package arguments_test

import (
"context"
"testing"

"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/internal/cli/arguments"
"github.com/arduino/arduino-cli/internal/cli/configuration"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -57,14 +59,16 @@ func TestArgsStringify(t *testing.T) {
}

func TestParseReferenceCores(t *testing.T) {
srv := commands.NewArduinoCoreServer("")
ctx := context.Background()
for _, tt := range goodCores {
actual, err := arguments.ParseReference(tt.in)
actual, err := arguments.ParseReference(srv, ctx, tt.in)
assert.Nil(t, err)
assert.Equal(t, tt.expected, actual)
}

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

refs, err := arguments.ParseReferences(input)
srv := commands.NewArduinoCoreServer("")
refs, err := arguments.ParseReferences(srv, context.Background(), input)
assert.Nil(t, err)
assert.Equal(t, len(goodCores), len(refs))

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/board/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func initDetailsCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
}

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

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

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func initListCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {

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

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

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ for a specific board if you specify the board name`),

// runListAllCommand list all installed boards
func runListAllCommand(args []string, srv rpc.ArduinoCoreServiceServer) {
inst := instance.CreateAndInit()
ctx := context.Background()
inst := instance.CreateAndInit(srv, ctx)

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

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/board/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func initSearchCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
}

func runSearchCommand(srv rpc.ArduinoCoreServiceServer, args []string) {
inst := instance.CreateAndInit()
ctx := context.Background()
inst := instance.CreateAndInit(srv, ctx)

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

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/burnbootloader/burnbootloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
}

func runBootloaderCommand(srv rpc.ArduinoCoreServiceServer) {
instance := instance.CreateAndInit()
ctx := context.Background()
instance := instance.CreateAndInit(srv, ctx)

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

Expand Down
Loading

0 comments on commit ea57c70

Please sign in to comment.