Skip to content

Commit ba2c25c

Browse files
committed
refactor libraries API, let upgrade single libraries
1 parent 60709ab commit ba2c25c

File tree

9 files changed

+80
-34
lines changed

9 files changed

+80
-34
lines changed

cli/instance/instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func initInstance() *rpc.InitResp {
4545
if resp.GetLibrariesIndexError() != "" {
4646
commands.UpdateLibrariesIndex(context.Background(),
4747
&rpc.UpdateLibrariesIndexReq{Instance: resp.GetInstance()}, output.ProgressBar())
48-
rescResp, err := commands.Rescan(context.Background(), &rpc.RescanReq{Instance: resp.GetInstance()})
48+
rescResp, err := commands.Rescan(resp.GetInstance().GetId())
4949
if rescResp.GetLibrariesIndexError() != "" {
5050
formatter.PrintErrorMessage("Error loading library index: " + rescResp.GetLibrariesIndexError())
5151
os.Exit(errorcodes.ErrGeneric)

cli/lib/upgrade.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ import (
2626
"github.com/arduino/arduino-cli/cli/output"
2727
"github.com/arduino/arduino-cli/commands/lib"
2828
"github.com/arduino/arduino-cli/common/formatter"
29-
rpc "github.com/arduino/arduino-cli/rpc/commands"
3029
"github.com/sirupsen/logrus"
3130
"github.com/spf13/cobra"
32-
"golang.org/x/net/context"
3331
)
3432

3533
func initUpgradeCommand() *cobra.Command {
@@ -39,7 +37,7 @@ func initUpgradeCommand() *cobra.Command {
3937
Long: "This command ungrades all installed libraries to the latest available version." +
4038
"To upgrade a single library use the 'install' command.",
4139
Example: " " + os.Args[0] + " lib upgrade",
42-
Args: cobra.NoArgs,
40+
Args: cobra.ArbitraryArgs,
4341
Run: runUpgradeCommand,
4442
}
4543
return listCommand
@@ -49,9 +47,13 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
4947
instance := instance.CreateInstaceIgnorePlatformIndexErrors()
5048

5149
if len(args) == 0 {
52-
err := lib.LibraryUpgradeAll(context.Background(), &rpc.LibraryUpgradeAllReq{
53-
Instance: instance,
54-
}, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
50+
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
51+
if err != nil {
52+
formatter.PrintError(err, "Error upgrading libraries")
53+
os.Exit(errorcodes.ErrGeneric)
54+
}
55+
} else {
56+
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
5557
if err != nil {
5658
formatter.PrintError(err, "Error upgrading libraries")
5759
os.Exit(errorcodes.ErrGeneric)

commands/core/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
5757
return nil, err
5858
}
5959

60-
_, err = commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance})
60+
_, err = commands.Rescan(req.GetInstance().GetId())
6161
if err != nil {
6262
return nil, err
6363
}

commands/core/uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallReq, taskC
6969
}
7070
}
7171

72-
_, err = commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance})
72+
_, err = commands.Rescan(req.GetInstance().GetId())
7373
if err != nil {
7474
return nil, err
7575
}

commands/core/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq,
5252
return nil, err
5353
}
5454

55-
if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
55+
if _, err := commands.Rescan(req.GetInstance().GetId()); err != nil {
5656
return nil, err
5757
}
5858

commands/daemon/daemon.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (s *ArduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyReq
7676

7777
// Rescan FIXMEDOC
7878
func (s *ArduinoCoreServerImpl) Rescan(ctx context.Context, req *rpc.RescanReq) (*rpc.RescanResp, error) {
79-
return commands.Rescan(ctx, req)
79+
return commands.Rescan(req.GetInstance().GetId())
8080
}
8181

8282
// UpdateIndex FIXMEDOC
@@ -277,7 +277,7 @@ func (s *ArduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallReq, s
277277

278278
// LibraryUpgradeAll FIXMEDOC
279279
func (s *ArduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllReq, stream rpc.ArduinoCore_LibraryUpgradeAllServer) error {
280-
err := lib.LibraryUpgradeAll(stream.Context(), req,
280+
err := lib.LibraryUpgradeAll(req.GetInstance().GetId(),
281281
func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryUpgradeAllResp{Progress: p}) },
282282
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryUpgradeAllResp{TaskProgress: p}) },
283283
s.DownloaderHeaders,

commands/instances.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexReq,
243243
if d.Error() != nil {
244244
return d.Error()
245245
}
246-
if _, err := Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
246+
if _, err := Rescan(req.GetInstance().GetId()); err != nil {
247247
return fmt.Errorf("rescanning filesystem: %s", err)
248248
}
249249
return nil
@@ -293,21 +293,20 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
293293
return nil, fmt.Errorf("saving downloaded index %s: %s", URL, err)
294294
}
295295
}
296-
if _, err := Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
296+
if _, err := Rescan(id); err != nil {
297297
return nil, fmt.Errorf("rescanning filesystem: %s", err)
298298
}
299299
return &rpc.UpdateIndexResp{}, nil
300300
}
301301

302-
// Rescan FIXMEDOC
303-
func Rescan(ctx context.Context, req *rpc.RescanReq) (*rpc.RescanResp, error) {
304-
id := req.GetInstance().GetId()
305-
coreInstance, ok := instances[id]
302+
// Rescan restart discoveries for the given instance
303+
func Rescan(instanceID int32) (*rpc.RescanResp, error) {
304+
coreInstance, ok := instances[instanceID]
306305
if !ok {
307306
return nil, fmt.Errorf("invalid handle")
308307
}
309308

310-
pm, lm, reqPltIndex, reqLibIndex, err := createInstance(ctx, coreInstance.config, coreInstance.getLibOnly)
309+
pm, lm, reqPltIndex, reqLibIndex, err := createInstance(context.Background(), coreInstance.config, coreInstance.getLibOnly)
311310
if err != nil {
312311
return nil, fmt.Errorf("rescanning filesystem: %s", err)
313312
}

commands/lib/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq,
4848
return err
4949
}
5050

51-
if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
51+
if _, err := commands.Rescan(req.GetInstance().GetId()); err != nil {
5252
return fmt.Errorf("rescanning libraries: %s", err)
5353
}
5454
return nil

commands/lib/upgrade.go

+59-14
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,78 @@
1818
package lib
1919

2020
import (
21-
"context"
2221
"fmt"
2322
"net/http"
2423

24+
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2525
"github.com/arduino/arduino-cli/commands"
26-
rpc "github.com/arduino/arduino-cli/rpc/commands"
2726
)
2827

29-
// LibraryUpgradeAll FIXMEDOC
30-
func LibraryUpgradeAll(ctx context.Context, req *rpc.LibraryUpgradeAllReq, downloadCB commands.DownloadProgressCB,
28+
// LibraryUpgradeAll upgrades all the available libraries
29+
func LibraryUpgradeAll(instanceID int32, downloadCB commands.DownloadProgressCB,
30+
taskCB commands.TaskProgressCB, headers http.Header) error {
31+
// get the library manager
32+
lm := commands.GetLibraryManager(instanceID)
33+
34+
if err := upgrade(lm, listLibraries(lm, true, true), downloadCB, taskCB, headers); err != nil {
35+
return err
36+
}
37+
38+
if _, err := commands.Rescan(instanceID); err != nil {
39+
return fmt.Errorf("rescanning libraries: %s", err)
40+
}
41+
42+
return nil
43+
}
44+
45+
// LibraryUpgrade upgrades only the given libraries
46+
func LibraryUpgrade(instanceID int32, libraryNames []string, downloadCB commands.DownloadProgressCB,
47+
taskCB commands.TaskProgressCB, headers http.Header) error {
48+
// get the library manager
49+
lm := commands.GetLibraryManager(instanceID)
50+
51+
// get the libs to upgrade
52+
libs := filterByName(listLibraries(lm, true, true), libraryNames)
53+
54+
// do it
55+
return upgrade(lm, libs, downloadCB, taskCB, headers)
56+
}
57+
58+
func upgrade(lm *librariesmanager.LibrariesManager, libs []*installedLib, downloadCB commands.DownloadProgressCB,
3159
taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error {
32-
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3360

34-
// Obtain the list of upgradable libraries
35-
list := listLibraries(lm, true, true)
61+
// Go through the list and download them
3662

37-
for _, upgradeDesc := range list {
38-
if err := downloadLibrary(lm, upgradeDesc.Available, downloadCB, taskCB, downloaderHeaders); err != nil {
63+
for _, lib := range libs {
64+
if err := downloadLibrary(lm, lib.Available, downloadCB, taskCB, downloaderHeaders); err != nil {
3965
return err
4066
}
4167
}
42-
for _, upgradeDesc := range list {
43-
installLibrary(lm, upgradeDesc.Available, taskCB)
44-
}
4568

46-
if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
47-
return fmt.Errorf("rescanning libraries: %s", err)
69+
// Go through the list and install them
70+
for _, lib := range libs {
71+
if err := installLibrary(lm, lib.Available, taskCB); err != nil {
72+
return err
73+
}
4874
}
75+
4976
return nil
5077
}
78+
79+
func filterByName(libs []*installedLib, names []string) []*installedLib {
80+
// put the names in a map to ease lookup
81+
queryMap := make(map[string]struct{})
82+
for _, name := range names {
83+
queryMap[name] = struct{}{}
84+
}
85+
86+
ret := []*installedLib{}
87+
for _, lib := range libs {
88+
// skip if library name wasn't in the query
89+
if _, found := queryMap[lib.Library.Name]; found {
90+
ret = append(ret, lib)
91+
}
92+
}
93+
94+
return ret
95+
}

0 commit comments

Comments
 (0)