Skip to content

Commit 36e36cf

Browse files
authored
Update single libraries (#315)
* no need to pass around the whole request object * always show available versions on list * refactor libraries API, let upgrade single libraries * fixed examples * forgot
1 parent d7643ae commit 36e36cf

File tree

13 files changed

+104
-56
lines changed

13 files changed

+104
-56
lines changed

Diff for: 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)

Diff for: cli/lib/upgrade.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,41 @@ 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 {
3634
listCommand := &cobra.Command{
3735
Use: "upgrade",
3836
Short: "Upgrades installed libraries.",
39-
Long: "This command ungrades all installed libraries to the latest available version." +
40-
"To upgrade a single library use the 'install' command.",
41-
Example: " " + os.Args[0] + " lib upgrade",
42-
Args: cobra.NoArgs,
43-
Run: runUpgradeCommand,
37+
Long: "This command upgrades an installed library to the latest available version. " +
38+
"Multiple libraries can be passed separated by a space. If no arguments are provided, " +
39+
"the command will upgrade all the installed libraries where an update is available.",
40+
Example: " " + os.Args[0] + " lib upgrade \n" +
41+
" " + os.Args[0] + " lib upgrade Audio\n" +
42+
" " + os.Args[0] + " lib upgrade Audio ArduinoJson",
43+
Args: cobra.ArbitraryArgs,
44+
Run: runUpgradeCommand,
4445
}
4546
return listCommand
4647
}
4748

4849
func runUpgradeCommand(cmd *cobra.Command, args []string) {
4950
instance := instance.CreateInstaceIgnorePlatformIndexErrors()
5051

51-
err := lib.LibraryUpgradeAll(context.Background(), &rpc.LibraryUpgradeAllReq{
52-
Instance: instance,
53-
}, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
54-
if err != nil {
55-
formatter.PrintError(err, "Error upgrading libraries")
56-
os.Exit(errorcodes.ErrGeneric)
52+
if len(args) == 0 {
53+
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
54+
if err != nil {
55+
formatter.PrintError(err, "Error upgrading libraries")
56+
os.Exit(errorcodes.ErrGeneric)
57+
}
58+
} else {
59+
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
60+
if err != nil {
61+
formatter.PrintError(err, "Error upgrading libraries")
62+
os.Exit(errorcodes.ErrGeneric)
63+
}
5764
}
5865

5966
logrus.Info("Done")

Diff for: 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
}

Diff for: 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
}

Diff for: 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

Diff for: 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,

Diff for: commands/instances.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ func GetPackageManager(req InstanceContainer) *packagemanager.PackageManager {
7575
return i.PackageManager
7676
}
7777

78-
// GetLibraryManager FIXMEDOC
79-
func GetLibraryManager(req InstanceContainer) *librariesmanager.LibrariesManager {
80-
i, ok := instances[req.GetInstance().GetId()]
78+
// GetLibraryManager returns the library manager for the given instance ID
79+
func GetLibraryManager(instanceID int32) *librariesmanager.LibrariesManager {
80+
i, ok := instances[instanceID]
8181
if !ok {
8282
return nil
8383
}
@@ -231,7 +231,7 @@ func Destroy(ctx context.Context, req *rpc.DestroyReq) (*rpc.DestroyResp, error)
231231
// UpdateLibrariesIndex updates the library_index.json
232232
func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexReq, downloadCB func(*rpc.DownloadProgress)) error {
233233
logrus.Info("Updating libraries index")
234-
lm := GetLibraryManager(req)
234+
lm := GetLibraryManager(req.GetInstance().GetId())
235235
if lm == nil {
236236
return fmt.Errorf("invalid handle")
237237
}
@@ -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
}

Diff for: commands/lib/download.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadReq, downloadC
3434
downloaderHeaders http.Header) (*rpc.LibraryDownloadResp, error) {
3535
logrus.Info("Executing `arduino lib download`")
3636

37-
lm := commands.GetLibraryManager(req)
37+
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3838

3939
logrus.Info("Preparing download")
4040

Diff for: commands/lib/install.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq,
3434
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error {
3535

36-
lm := commands.GetLibraryManager(req)
36+
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3737

3838
libRelease, err := findLibraryIndexRelease(lm, req)
3939
if err != nil {
@@ -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

Diff for: commands/lib/list.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type installedLib struct {
3434

3535
// LibraryList FIXMEDOC
3636
func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryListResp, error) {
37-
lm := commands.GetLibraryManager(req)
37+
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3838

3939
instaledLib := []*rpc.InstalledLibrary{}
4040
res := listLibraries(lm, req.GetUpdatable(), req.GetAll())
@@ -64,12 +64,9 @@ func listLibraries(lm *librariesmanager.LibrariesManager, updatable bool, all bo
6464
continue
6565
}
6666
}
67-
var available *librariesindex.Release
68-
if updatable {
69-
available = lm.Index.FindLibraryUpdate(lib)
70-
if available == nil {
71-
continue
72-
}
67+
available := lm.Index.FindLibraryUpdate(lib)
68+
if updatable && available == nil {
69+
continue
7370
}
7471
res = append(res, &installedLib{
7572
Library: lib,

Diff for: commands/lib/search.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929

3030
// LibrarySearch FIXMEDOC
3131
func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.LibrarySearchResp, error) {
32-
lm := commands.GetLibraryManager(req)
32+
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3333
if lm == nil {
3434
return nil, errors.New("invalid instance")
3535
}

Diff for: commands/lib/uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
// LibraryUninstall FIXMEDOC
2929
func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallReq, taskCB commands.TaskProgressCB) error {
30-
lm := commands.GetLibraryManager(req)
30+
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3131
ref, err := createLibIndexReference(lm, req)
3232
if err != nil {
3333
return err

Diff for: 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)
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)