Skip to content

Commit 61ba685

Browse files
authored
[breaking] Removed redundant and useless commands from gRPC API (#1891)
* Removed redundant and useless commands from gRPC interface * Moved some flags in the command creation This will turn out useful in the following commits * Made some cli procedures available for other packages Unfortunately we cannot limit the availability of the cli.core.* package to the cli.* packages, but they are now available as public API. * Moved 'lib list' flags into command creation * Moved 'core list' flags into command creation * Made 'lib list' procedure public * Made 'core list' procedure public * instance.Init no longer return errors array. They are printed directly inside the function through the feedback package. * Ultra-simplified 'outdated' 'update' and 'upgrade' * Moved 'update' flags in command creation * Fixed integration tests * Updated docs * Fixed integration tests
1 parent 5efa9b5 commit 61ba685

File tree

25 files changed

+705
-1599
lines changed

25 files changed

+705
-1599
lines changed

Diff for: cli/core/install.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ import (
3131
"github.com/spf13/cobra"
3232
)
3333

34-
var (
35-
postInstallFlags arguments.PostInstallFlags
36-
noOverwrite bool
37-
)
38-
3934
func initInstallCommand() *cobra.Command {
35+
var noOverwrite bool
36+
var postInstallFlags arguments.PostInstallFlags
4037
installCommand := &cobra.Command{
4138
Use: fmt.Sprintf("install %s:%s[@%s]...", tr("PACKAGER"), tr("ARCH"), tr("VERSION")),
4239
Short: tr("Installs one or more cores and corresponding tool dependencies."),
@@ -49,7 +46,9 @@ func initInstallCommand() *cobra.Command {
4946
PreRun: func(cmd *cobra.Command, args []string) {
5047
arguments.CheckFlagsConflicts(cmd, "run-post-install", "skip-post-install")
5148
},
52-
Run: runInstallCommand,
49+
Run: func(cmd *cobra.Command, args []string) {
50+
runInstallCommand(args, postInstallFlags, noOverwrite)
51+
},
5352
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
5453
return arguments.GetInstallableCores(), cobra.ShellCompDirectiveDefault
5554
},
@@ -59,7 +58,7 @@ func initInstallCommand() *cobra.Command {
5958
return installCommand
6059
}
6160

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

Diff for: cli/core/list.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,32 @@ import (
2929
"github.com/spf13/cobra"
3030
)
3131

32-
var (
33-
updatableOnly bool
34-
all bool
35-
)
36-
3732
func initListCommand() *cobra.Command {
33+
var updatableOnly bool
34+
var all bool
3835
listCommand := &cobra.Command{
3936
Use: "list",
4037
Short: tr("Shows the list of installed platforms."),
4138
Long: tr("Shows the list of installed platforms."),
4239
Example: " " + os.Args[0] + " core list",
4340
Args: cobra.NoArgs,
44-
Run: runListCommand,
41+
Run: func(cmd *cobra.Command, args []string) {
42+
runListCommand(args, all, updatableOnly)
43+
},
4544
}
4645
listCommand.Flags().BoolVar(&updatableOnly, "updatable", false, tr("List updatable platforms."))
4746
listCommand.Flags().BoolVar(&all, "all", false, tr("If set return all installable and installed cores, including manually installed."))
4847
return listCommand
4948
}
5049

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

56+
// List print a list of installed platforms.
57+
func List(inst *rpc.Instance, all bool, updatableOnly bool) {
5558
platforms, err := core.GetPlatforms(&rpc.PlatformListRequest{
5659
Instance: inst,
5760
UpdatableOnly: updatableOnly,

Diff for: cli/core/search.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
7373
}
7474
}
7575

76-
for _, err := range instance.Init(inst) {
77-
feedback.Errorf(tr("Error initializing instance: %v"), err)
78-
}
76+
instance.Init(inst)
7977

8078
arguments := strings.ToLower(strings.Join(args, " "))
8179
logrus.Infof("Executing `arduino-cli core search` with args: '%s'", arguments)

Diff for: cli/core/update_index.go

+4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func initUpdateIndexCommand() *cobra.Command {
4444
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
4545
inst := instance.CreateInstanceAndRunFirstUpdate()
4646
logrus.Info("Executing `arduino-cli core update-index`")
47+
UpdateIndex(inst)
48+
}
4749

50+
// UpdateIndex updates the index of platforms.
51+
func UpdateIndex(inst *rpc.Instance) {
4852
err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{Instance: inst}, output.ProgressBar())
4953
if err != nil {
5054
feedback.Error(err)

Diff for: cli/core/upgrade.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
)
3535

3636
func initUpgradeCommand() *cobra.Command {
37+
var postInstallFlags arguments.PostInstallFlags
3738
upgradeCommand := &cobra.Command{
3839
Use: fmt.Sprintf("upgrade [%s:%s] ...", tr("PACKAGER"), tr("ARCH")),
3940
Short: tr("Upgrades one or all installed platforms to the latest version."),
@@ -43,16 +44,22 @@ func initUpgradeCommand() *cobra.Command {
4344
" " + os.Args[0] + " core upgrade\n\n" +
4445
" # " + tr("upgrade arduino:samd to the latest version") + "\n" +
4546
" " + os.Args[0] + " core upgrade arduino:samd",
46-
Run: runUpgradeCommand,
47+
Run: func(cmd *cobra.Command, args []string) {
48+
runUpgradeCommand(args, postInstallFlags.DetectSkipPostInstallValue())
49+
},
4750
}
4851
postInstallFlags.AddToCommand(upgradeCommand)
4952
return upgradeCommand
5053
}
5154

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

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

99106
if _, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress()); err != nil {

Diff for: cli/instance/instance.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package instance
1717

1818
import (
1919
"context"
20-
"errors"
2120
"os"
2221

2322
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -50,10 +49,7 @@ func CreateAndInitWithProfile(profileName string, sketchPath *paths.Path) (*rpc.
5049
feedback.Errorf(tr("Error creating instance: %v"), err)
5150
os.Exit(errorcodes.ErrGeneric)
5251
}
53-
profile, errs := InitWithProfile(instance, profileName, sketchPath)
54-
for _, err := range errs {
55-
feedback.Errorf(tr("Error initializing instance: %v"), err)
56-
}
52+
profile := InitWithProfile(instance, profileName, sketchPath)
5753
return instance, profile
5854
}
5955

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

7974
// InitWithProfile initializes instance by loading libraries and platforms specified in the given profile of the given sketch.
8075
// In case of loading failures return a list of errors for each platform or library that we failed to load.
8176
// Required Package and library indexes files are automatically downloaded.
82-
func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *paths.Path) (*rpc.Profile, []error) {
83-
errs := []error{}
84-
77+
func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *paths.Path) *rpc.Profile {
8578
// In case the CLI is executed for the first time
8679
if err := FirstUpdate(instance); err != nil {
87-
return nil, append(errs, err)
80+
feedback.Errorf(tr("Error initializing instance: %v"), err)
81+
return nil
8882
}
8983

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

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

121-
return profile, errs
115+
return profile
122116
}
123117

124118
// FirstUpdate downloads libraries and packages indexes if they don't exist.

Diff for: cli/lib/list.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ import (
3232
"github.com/spf13/cobra"
3333
)
3434

35-
var (
36-
all bool
37-
updatable bool
38-
)
39-
4035
func initListCommand() *cobra.Command {
36+
var all bool
37+
var updatable bool
4138
listCommand := &cobra.Command{
4239
Use: fmt.Sprintf("list [%s]", tr("LIBNAME")),
4340
Short: tr("Shows a list of installed libraries."),
@@ -48,18 +45,24 @@ library. By default the libraries provided as built-in by platforms/core are
4845
not listed, they can be listed by adding the --all flag.`),
4946
Example: " " + os.Args[0] + " lib list",
5047
Args: cobra.MaximumNArgs(1),
51-
Run: runListCommand,
48+
Run: func(cmd *cobra.Command, args []string) {
49+
runListCommand(args, all, updatable)
50+
},
5251
}
5352
listCommand.Flags().BoolVar(&all, "all", false, tr("Include built-in libraries (from platforms and IDE) in listing."))
5453
fqbn.AddToCommand(listCommand)
5554
listCommand.Flags().BoolVar(&updatable, "updatable", false, tr("List updatable libraries."))
5655
return listCommand
5756
}
5857

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

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

97-
feedback.PrintResult(installedResult{libs})
100+
feedback.PrintResult(installedResult{
101+
onlyUpdates: updatable,
102+
installedLibs: libs,
103+
})
98104
logrus.Info("Done")
99105
}
100106

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

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

111118
func (ir installedResult) String() string {
112-
if ir.installedLibs == nil || len(ir.installedLibs) == 0 {
113-
if updatable {
114-
return tr("No updates available.")
119+
if len(ir.installedLibs) == 0 {
120+
if ir.onlyUpdates {
121+
return tr("No libraries update is available.")
115122
}
116123
return tr("No libraries installed.")
117124
}

Diff for: cli/lib/search.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
6969
os.Exit(errorcodes.ErrGeneric)
7070
}
7171

72-
for _, err := range instance.Init(inst) {
73-
feedback.Errorf(tr("Error initializing instance: %v"), err)
74-
}
72+
instance.Init(inst)
7573

7674
searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
7775
Instance: inst,

Diff for: cli/lib/update_index.go

+4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func initUpdateIndexCommand() *cobra.Command {
4444
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
4545
inst := instance.CreateInstanceAndRunFirstUpdate()
4646
logrus.Info("Executing `arduino-cli lib update-index`")
47+
UpdateIndex(inst)
48+
}
4749

50+
// UpdateIndex updates the index of libraries.
51+
func UpdateIndex(inst *rpc.Instance) {
4852
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
4953
Instance: inst,
5054
}, output.ProgressBar())

Diff for: cli/lib/upgrade.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ func initUpgradeCommand() *cobra.Command {
4646
func runUpgradeCommand(cmd *cobra.Command, args []string) {
4747
instance := instance.CreateAndInit()
4848
logrus.Info("Executing `arduino-cli lib upgrade`")
49+
Upgrade(instance, args)
50+
}
4951

52+
// Upgrade upgrades the specified libraries
53+
func Upgrade(instance *rpc.Instance, libraries []string) {
5054
var upgradeErr error
51-
if len(args) == 0 {
55+
if len(libraries) == 0 {
5256
req := &rpc.LibraryUpgradeAllRequest{Instance: instance}
5357
upgradeErr = lib.LibraryUpgradeAll(req, output.ProgressBar(), output.TaskProgress())
5458
} else {
55-
for _, libName := range args {
59+
for _, libName := range libraries {
5660
req := &rpc.LibraryUpgradeRequest{
5761
Instance: instance,
5862
Name: libName,

0 commit comments

Comments
 (0)