Skip to content

[breaking] Removed redundant and useless commands from gRPC API #1891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 24, 2022
Merged
13 changes: 6 additions & 7 deletions cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ import (
"github.com/spf13/cobra"
)

var (
postInstallFlags arguments.PostInstallFlags
noOverwrite bool
)

func initInstallCommand() *cobra.Command {
var noOverwrite bool
var postInstallFlags arguments.PostInstallFlags
installCommand := &cobra.Command{
Use: fmt.Sprintf("install %s:%s[@%s]...", tr("PACKAGER"), tr("ARCH"), tr("VERSION")),
Short: tr("Installs one or more cores and corresponding tool dependencies."),
Expand All @@ -49,7 +46,9 @@ func initInstallCommand() *cobra.Command {
PreRun: func(cmd *cobra.Command, args []string) {
arguments.CheckFlagsConflicts(cmd, "run-post-install", "skip-post-install")
},
Run: runInstallCommand,
Run: func(cmd *cobra.Command, args []string) {
runInstallCommand(args, postInstallFlags, noOverwrite)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstallableCores(), cobra.ShellCompDirectiveDefault
},
Expand All @@ -59,7 +58,7 @@ func initInstallCommand() *cobra.Command {
return installCommand
}

func runInstallCommand(cmd *cobra.Command, args []string) {
func runInstallCommand(args []string, postInstallFlags arguments.PostInstallFlags, noOverwrite bool) {
inst := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli core install`")

Expand Down
17 changes: 10 additions & 7 deletions cli/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,32 @@ import (
"github.com/spf13/cobra"
)

var (
updatableOnly bool
all bool
)

func initListCommand() *cobra.Command {
var updatableOnly bool
var all bool
listCommand := &cobra.Command{
Use: "list",
Short: tr("Shows the list of installed platforms."),
Long: tr("Shows the list of installed platforms."),
Example: " " + os.Args[0] + " core list",
Args: cobra.NoArgs,
Run: runListCommand,
Run: func(cmd *cobra.Command, args []string) {
runListCommand(args, all, updatableOnly)
},
}
listCommand.Flags().BoolVar(&updatableOnly, "updatable", false, tr("List updatable platforms."))
listCommand.Flags().BoolVar(&all, "all", false, tr("If set return all installable and installed cores, including manually installed."))
return listCommand
}

func runListCommand(cmd *cobra.Command, args []string) {
func runListCommand(args []string, all bool, updatableOnly bool) {
inst := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli core list`")
List(inst, all, updatableOnly)
}

// List print a list of installed platforms.
func List(inst *rpc.Instance, all bool, updatableOnly bool) {
platforms, err := core.GetPlatforms(&rpc.PlatformListRequest{
Instance: inst,
UpdatableOnly: updatableOnly,
Expand Down
4 changes: 1 addition & 3 deletions cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
}
}

for _, err := range instance.Init(inst) {
feedback.Errorf(tr("Error initializing instance: %v"), err)
}
instance.Init(inst)

arguments := strings.ToLower(strings.Join(args, " "))
logrus.Infof("Executing `arduino-cli core search` with args: '%s'", arguments)
Expand Down
4 changes: 4 additions & 0 deletions cli/core/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func initUpdateIndexCommand() *cobra.Command {
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
inst := instance.CreateInstanceAndRunFirstUpdate()
logrus.Info("Executing `arduino-cli core update-index`")
UpdateIndex(inst)
}

// UpdateIndex updates the index of platforms.
func UpdateIndex(inst *rpc.Instance) {
err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{Instance: inst}, output.ProgressBar())
if err != nil {
feedback.Error(err)
Expand Down
13 changes: 10 additions & 3 deletions cli/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
)

func initUpgradeCommand() *cobra.Command {
var postInstallFlags arguments.PostInstallFlags
upgradeCommand := &cobra.Command{
Use: fmt.Sprintf("upgrade [%s:%s] ...", tr("PACKAGER"), tr("ARCH")),
Short: tr("Upgrades one or all installed platforms to the latest version."),
Expand All @@ -43,16 +44,22 @@ func initUpgradeCommand() *cobra.Command {
" " + os.Args[0] + " core upgrade\n\n" +
" # " + tr("upgrade arduino:samd to the latest version") + "\n" +
" " + os.Args[0] + " core upgrade arduino:samd",
Run: runUpgradeCommand,
Run: func(cmd *cobra.Command, args []string) {
runUpgradeCommand(args, postInstallFlags.DetectSkipPostInstallValue())
},
}
postInstallFlags.AddToCommand(upgradeCommand)
return upgradeCommand
}

func runUpgradeCommand(cmd *cobra.Command, args []string) {
func runUpgradeCommand(args []string, skipPostInstall bool) {
inst := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli core upgrade`")
Upgrade(inst, args, skipPostInstall)
}

// Upgrade upgrades one or all installed platforms to the latest version.
func Upgrade(inst *rpc.Instance, args []string, skipPostInstall bool) {
// if no platform was passed, upgrade allthethings
if len(args) == 0 {
targets, err := core.GetPlatforms(&rpc.PlatformListRequest{
Expand Down Expand Up @@ -93,7 +100,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
Instance: inst,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
SkipPostInstall: postInstallFlags.DetectSkipPostInstallValue(),
SkipPostInstall: skipPostInstall,
}

if _, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress()); err != nil {
Expand Down
24 changes: 9 additions & 15 deletions cli/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package instance

import (
"context"
"errors"
"os"

"github.com/arduino/arduino-cli/cli/errorcodes"
Expand Down Expand Up @@ -50,10 +49,7 @@ func CreateAndInitWithProfile(profileName string, sketchPath *paths.Path) (*rpc.
feedback.Errorf(tr("Error creating instance: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
profile, errs := InitWithProfile(instance, profileName, sketchPath)
for _, err := range errs {
feedback.Errorf(tr("Error initializing instance: %v"), err)
}
profile := InitWithProfile(instance, profileName, sketchPath)
return instance, profile
}

Expand All @@ -71,20 +67,18 @@ func Create() (*rpc.Instance, error) {
// platform or library that we failed to load.
// Package and library indexes files are automatically updated if the
// CLI is run for the first time.
func Init(instance *rpc.Instance) []error {
_, errs := InitWithProfile(instance, "", nil)
return errs
func Init(instance *rpc.Instance) {
InitWithProfile(instance, "", nil)
}

// InitWithProfile initializes instance by loading libraries and platforms specified in the given profile of the given sketch.
// In case of loading failures return a list of errors for each platform or library that we failed to load.
// Required Package and library indexes files are automatically downloaded.
func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *paths.Path) (*rpc.Profile, []error) {
errs := []error{}

func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *paths.Path) *rpc.Profile {
// In case the CLI is executed for the first time
if err := FirstUpdate(instance); err != nil {
return nil, append(errs, err)
feedback.Errorf(tr("Error initializing instance: %v"), err)
return nil
}

downloadCallback := output.ProgressBar()
Expand All @@ -98,7 +92,7 @@ func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *pat
var profile *rpc.Profile
err := commands.Init(initReq, func(res *rpc.InitResponse) {
if st := res.GetError(); st != nil {
errs = append(errs, errors.New(st.Message))
feedback.Errorf(tr("Error initializing instance: %v"), st.Message)
}

if progress := res.GetInitProgress(); progress != nil {
Expand All @@ -115,10 +109,10 @@ func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *pat
}
})
if err != nil {
errs = append(errs, err)
feedback.Errorf(tr("Error initializing instance: %v"), err)
}

return profile, errs
return profile
}

// FirstUpdate downloads libraries and packages indexes if they don't exist.
Expand Down
29 changes: 18 additions & 11 deletions cli/lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ import (
"github.com/spf13/cobra"
)

var (
all bool
updatable bool
)

func initListCommand() *cobra.Command {
var all bool
var updatable bool
listCommand := &cobra.Command{
Use: fmt.Sprintf("list [%s]", tr("LIBNAME")),
Short: tr("Shows a list of installed libraries."),
Expand All @@ -48,18 +45,24 @@ library. By default the libraries provided as built-in by platforms/core are
not listed, they can be listed by adding the --all flag.`),
Example: " " + os.Args[0] + " lib list",
Args: cobra.MaximumNArgs(1),
Run: runListCommand,
Run: func(cmd *cobra.Command, args []string) {
runListCommand(args, all, updatable)
},
}
listCommand.Flags().BoolVar(&all, "all", false, tr("Include built-in libraries (from platforms and IDE) in listing."))
fqbn.AddToCommand(listCommand)
listCommand.Flags().BoolVar(&updatable, "updatable", false, tr("List updatable libraries."))
return listCommand
}

func runListCommand(cmd *cobra.Command, args []string) {
func runListCommand(args []string, all bool, updatable bool) {
instance := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli lib list`")
List(instance, args, all, updatable)
}

// List lists all the installed libraries.
func List(instance *rpc.Instance, args []string, all bool, updatable bool) {
name := ""
if len(args) > 0 {
name = args[0]
Expand Down Expand Up @@ -94,13 +97,17 @@ func runListCommand(cmd *cobra.Command, args []string) {
libs = []*rpc.InstalledLibrary{}
}

feedback.PrintResult(installedResult{libs})
feedback.PrintResult(installedResult{
onlyUpdates: updatable,
installedLibs: libs,
})
logrus.Info("Done")
}

// output from this command requires special formatting, let's create a dedicated
// feedback.Result implementation
type installedResult struct {
onlyUpdates bool
installedLibs []*rpc.InstalledLibrary
}

Expand All @@ -109,9 +116,9 @@ func (ir installedResult) Data() interface{} {
}

func (ir installedResult) String() string {
if ir.installedLibs == nil || len(ir.installedLibs) == 0 {
if updatable {
return tr("No updates available.")
if len(ir.installedLibs) == 0 {
if ir.onlyUpdates {
return tr("No libraries update is available.")
}
return tr("No libraries installed.")
}
Expand Down
4 changes: 1 addition & 3 deletions cli/lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
os.Exit(errorcodes.ErrGeneric)
}

for _, err := range instance.Init(inst) {
feedback.Errorf(tr("Error initializing instance: %v"), err)
}
instance.Init(inst)

searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
Instance: inst,
Expand Down
4 changes: 4 additions & 0 deletions cli/lib/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func initUpdateIndexCommand() *cobra.Command {
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
inst := instance.CreateInstanceAndRunFirstUpdate()
logrus.Info("Executing `arduino-cli lib update-index`")
UpdateIndex(inst)
}

// UpdateIndex updates the index of libraries.
func UpdateIndex(inst *rpc.Instance) {
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
Instance: inst,
}, output.ProgressBar())
Expand Down
8 changes: 6 additions & 2 deletions cli/lib/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ func initUpgradeCommand() *cobra.Command {
func runUpgradeCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli lib upgrade`")
Upgrade(instance, args)
}

// Upgrade upgrades the specified libraries
func Upgrade(instance *rpc.Instance, libraries []string) {
var upgradeErr error
if len(args) == 0 {
if len(libraries) == 0 {
req := &rpc.LibraryUpgradeAllRequest{Instance: instance}
upgradeErr = lib.LibraryUpgradeAll(req, output.ProgressBar(), output.TaskProgress())
} else {
for _, libName := range args {
for _, libName := range libraries {
req := &rpc.LibraryUpgradeRequest{
Instance: instance,
Name: libName,
Expand Down
Loading