From 11b4de821e6af75628552bafd07da20ccb0ef97c Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Tue, 12 Sep 2023 15:27:10 +0200 Subject: [PATCH] Replace direct access to the package manager with a call to PlatformSearch function --- arduino/cores/packagemanager/download.go | 8 ++++---- arduino/cores/packagemanager/install_uninstall.go | 2 +- arduino/cores/packagemanager/package_manager_test.go | 5 ++++- commands/core/uninstall.go | 2 +- commands/upload/upload.go | 5 ++++- internal/cli/compile/compile.go | 11 +++++------ internal/cli/upload/upload.go | 11 +++++------ 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/arduino/cores/packagemanager/download.go b/arduino/cores/packagemanager/download.go index 5baf34d2b9c..f3a5877c8a6 100644 --- a/arduino/cores/packagemanager/download.go +++ b/arduino/cores/packagemanager/download.go @@ -43,12 +43,12 @@ func (platform *PlatformReference) String() string { // FindPlatform returns the Platform matching the PlatformReference or nil if not found. // The PlatformVersion field of the reference is ignored. -func (pme *Explorer) FindPlatform(platformPackage, platformArchitecture string) *cores.Platform { - targetPackage, ok := pme.packages[platformPackage] +func (pme *Explorer) FindPlatform(ref *PlatformReference) *cores.Platform { + targetPackage, ok := pme.packages[ref.Package] if !ok { return nil } - platform, ok := targetPackage.Platforms[platformArchitecture] + platform, ok := targetPackage.Platforms[ref.PlatformArchitecture] if !ok { return nil } @@ -57,7 +57,7 @@ func (pme *Explorer) FindPlatform(platformPackage, platformArchitecture string) // FindPlatformRelease returns the PlatformRelease matching the PlatformReference or nil if not found func (pme *Explorer) FindPlatformRelease(ref *PlatformReference) *cores.PlatformRelease { - platform := pme.FindPlatform(ref.Package, ref.PlatformArchitecture) + platform := pme.FindPlatform(ref) if platform == nil { return nil } diff --git a/arduino/cores/packagemanager/install_uninstall.go b/arduino/cores/packagemanager/install_uninstall.go index 93ef233322e..73a75c7b00d 100644 --- a/arduino/cores/packagemanager/install_uninstall.go +++ b/arduino/cores/packagemanager/install_uninstall.go @@ -44,7 +44,7 @@ func (pme *Explorer) DownloadAndInstallPlatformUpgrades( } // Search the latest version for all specified platforms - platform := pme.FindPlatform(platformRef.Package, platformRef.PlatformArchitecture) + platform := pme.FindPlatform(platformRef) if platform == nil { return nil, &arduino.PlatformNotFoundError{Platform: platformRef.String()} } diff --git a/arduino/cores/packagemanager/package_manager_test.go b/arduino/cores/packagemanager/package_manager_test.go index e847d37ac31..7a6d8994004 100644 --- a/arduino/cores/packagemanager/package_manager_test.go +++ b/arduino/cores/packagemanager/package_manager_test.go @@ -387,7 +387,10 @@ func TestBoardOrdering(t *testing.T) { pme, release := pm.NewExplorer() defer release() - pl := pme.FindPlatform("arduino", "avr") + pl := pme.FindPlatform(&PlatformReference{ + Package: "arduino", + PlatformArchitecture: "avr", + }) require.NotNil(t, pl) plReleases := pl.GetAllInstalled() require.NotEmpty(t, plReleases) diff --git a/commands/core/uninstall.go b/commands/core/uninstall.go index da8cb786e5e..165b1fc16ea 100644 --- a/commands/core/uninstall.go +++ b/commands/core/uninstall.go @@ -48,7 +48,7 @@ func platformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, t PlatformArchitecture: req.Architecture, } if ref.PlatformVersion == nil { - platform := pme.FindPlatform(ref.Package, ref.PlatformArchitecture) + platform := pme.FindPlatform(ref) if platform == nil { return &arduino.PlatformNotFoundError{Platform: ref.String()} } diff --git a/commands/upload/upload.go b/commands/upload/upload.go index 40cbb0bb018..aa74023b0e3 100644 --- a/commands/upload/upload.go +++ b/commands/upload/upload.go @@ -285,7 +285,10 @@ func runProgramAction(pme *packagemanager.Explorer, Property: fmt.Sprintf("%s.tool.%s", action, port.Protocol), // TODO: Can be done better, maybe inline getToolID(...) Value: uploadToolID} } else if len(split) == 2 { - p := pme.FindPlatform(split[0], boardPlatform.Platform.Architecture) + p := pme.FindPlatform(&packagemanager.PlatformReference{ + Package: split[0], + PlatformArchitecture: boardPlatform.Platform.Architecture, + }) if p == nil { return nil, &arduino.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture} } diff --git a/internal/cli/compile/compile.go b/internal/cli/compile/compile.go index fbb6ef6cb1a..350ba436ba2 100644 --- a/internal/cli/compile/compile.go +++ b/internal/cli/compile/compile.go @@ -25,8 +25,8 @@ import ( "strings" "github.com/arduino/arduino-cli/arduino" - "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands/compile" + "github.com/arduino/arduino-cli/commands/core" "github.com/arduino/arduino-cli/commands/sketch" "github.com/arduino/arduino-cli/commands/upload" "github.com/arduino/arduino-cli/configuration" @@ -362,13 +362,12 @@ func runCompileCommand(cmd *cobra.Command, args []string) { panic(tr("Platform ID is not correct")) } - pme, release := commands.GetPackageManagerExplorer(compileRequest) - platform := pme.FindPlatform(split[0], split[1]) - release() - if profileArg.String() == "" { res.Error += fmt.Sprintln() - if platform != nil { + if platform, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{ + Instance: inst, + SearchArgs: platformErr.Platform, + AllVersions: false}); platform != nil { suggestion := fmt.Sprintf("`%s core install %s`", version.VersionInfo.Application, platformErr.Platform) res.Error += tr("Try running %s", suggestion) } else { diff --git a/internal/cli/upload/upload.go b/internal/cli/upload/upload.go index 2217b0ccc1c..ae5593c6c01 100644 --- a/internal/cli/upload/upload.go +++ b/internal/cli/upload/upload.go @@ -23,7 +23,7 @@ import ( "strings" "github.com/arduino/arduino-cli/arduino" - "github.com/arduino/arduino-cli/commands" + "github.com/arduino/arduino-cli/commands/core" sk "github.com/arduino/arduino-cli/commands/sketch" "github.com/arduino/arduino-cli/commands/upload" "github.com/arduino/arduino-cli/i18n" @@ -129,12 +129,11 @@ func runUploadCommand(command *cobra.Command, args []string) { panic(tr("Platform ID is not correct")) } - pme, release := commands.GetPackageManagerExplorer(&rpc.UploadRequest{Instance: inst}) - platform := pme.FindPlatform(split[0], split[1]) - release() - msg += "\n" - if platform != nil { + if platform, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{ + Instance: inst, + SearchArgs: platformErr.Platform, + AllVersions: false}); platform != nil { msg += tr("Try running %s", fmt.Sprintf("`%s core install %s`", version.VersionInfo.Application, platformErr.Platform)) } else { msg += tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform)