Skip to content

Commit dc9e5e0

Browse files
committed
Implemented GRPC LibraryUpgradeAll command
1 parent c375d33 commit dc9e5e0

File tree

10 files changed

+334
-123
lines changed

10 files changed

+334
-123
lines changed

cli/lib/upgrade.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
package lib
1919

2020
import (
21-
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
21+
"os"
22+
2223
"github.com/arduino/arduino-cli/cli"
2324
"github.com/arduino/arduino-cli/commands/lib"
25+
"github.com/arduino/arduino-cli/common/formatter"
26+
"github.com/arduino/arduino-cli/rpc"
2427
"github.com/sirupsen/logrus"
2528
"github.com/spf13/cobra"
29+
"golang.org/x/net/context"
2630
)
2731

2832
func initUpgradeCommand() *cobra.Command {
@@ -39,14 +43,15 @@ func initUpgradeCommand() *cobra.Command {
3943
}
4044

4145
func runUpgradeCommand(cmd *cobra.Command, args []string) {
42-
lm := cli.InitLibraryManager(cli.Config)
43-
list := lib.ListLibraries(lm, true)
44-
libReleases := []*librariesindex.Release{}
45-
for _, upgradeDesc := range list.Libraries {
46-
libReleases = append(libReleases, upgradeDesc.Available)
46+
instance := cli.CreateInstance()
47+
48+
err := lib.LibraryUpgradeAll(context.Background(), &rpc.LibraryUpgradeAllReq{
49+
Instance: instance,
50+
}, cli.OutputProgressBar(), cli.OutputTaskProgress())
51+
if err != nil {
52+
formatter.PrintError(err, "Error upgrading libraries")
53+
os.Exit(cli.ErrGeneric)
4754
}
4855

49-
//downloadLibraries(lm, libReleases)
50-
//installLibraries(lm, libReleases)
5156
logrus.Info("Done")
5257
}

commands/lib/download.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,23 @@ func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadReq, downloadC
4040
return nil, fmt.Errorf("looking for library: %s", err)
4141
}
4242

43-
if err := downloadLibrary(lm, lib, downloadCB); err != nil {
43+
if err := downloadLibrary(lm, lib, downloadCB, func(*rpc.TaskProgress) {}); err != nil {
4444
return nil, err
4545
}
4646

4747
return &rpc.LibraryDownloadResp{}, nil
4848
}
4949

50-
func downloadLibrary(lm *librariesmanager.LibrariesManager, libRelease *librariesindex.Release, downloadCB commands.DownloadProgressCB) error {
51-
d, err := libRelease.Resource.Download(lm.DownloadsDir)
52-
if err != nil {
53-
return fmt.Errorf("download error: %s", err)
50+
func downloadLibrary(lm *librariesmanager.LibrariesManager, libRelease *librariesindex.Release,
51+
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
52+
53+
taskCB(&rpc.TaskProgress{Name: "Downloading " + libRelease.String()})
54+
if d, err := libRelease.Resource.Download(lm.DownloadsDir); err != nil {
55+
return err
56+
} else if err := commands.Download(d, libRelease.String(), downloadCB); err != nil {
57+
return err
5458
}
55-
return commands.Download(d, libRelease.String(), downloadCB)
59+
taskCB(&rpc.TaskProgress{Completed: true})
60+
61+
return nil
5662
}

commands/lib/install.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"context"
2222
"fmt"
2323

24+
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
25+
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2426
"github.com/arduino/arduino-cli/commands"
2527
"github.com/arduino/arduino-cli/rpc"
2628
"github.com/sirupsen/logrus"
@@ -36,12 +38,21 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq,
3638
return fmt.Errorf("looking for library: %s", err)
3739
}
3840

39-
taskCB(&rpc.TaskProgress{Name: "Downloading " + libRelease.String()})
40-
if err := downloadLibrary(lm, libRelease, downloadCB); err != nil {
41+
if err := downloadLibrary(lm, libRelease, downloadCB, taskCB); err != nil {
42+
return fmt.Errorf("downloading library: %s", err)
43+
}
44+
45+
if err := installLibrary(lm, libRelease, taskCB); err != nil {
4146
return err
4247
}
43-
taskCB(&rpc.TaskProgress{Completed: true})
4448

49+
if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
50+
return fmt.Errorf("rescanning libraries: %s", err)
51+
}
52+
return nil
53+
}
54+
55+
func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *librariesindex.Release, taskCB commands.TaskProgressCB) error {
4556
taskCB(&rpc.TaskProgress{Name: "Installing " + libRelease.String()})
4657
logrus.WithField("library", libRelease).Info("Installing library")
4758
libPath, libReplaced, err := lm.InstallPrerequisiteCheck(libRelease)
@@ -56,8 +67,5 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq,
5667
}
5768
taskCB(&rpc.TaskProgress{Completed: true})
5869

59-
if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
60-
return fmt.Errorf("rescanning libraries: %s", err)
61-
}
6270
return nil
6371
}

commands/lib/upgrade.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,30 @@
1818
package lib
1919

2020
import (
21-
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
22-
"github.com/arduino/arduino-cli/cli"
23-
"github.com/sirupsen/logrus"
24-
"github.com/spf13/cobra"
21+
"context"
22+
"fmt"
23+
24+
"github.com/arduino/arduino-cli/commands"
25+
"github.com/arduino/arduino-cli/rpc"
2526
)
2627

27-
func initUpgradeCommand() *cobra.Command {
28-
listCommand := &cobra.Command{
29-
Use: "upgrade",
30-
Short: "Upgrades installed libraries.",
31-
Long: "This command ungrades all installed libraries to the latest available version." +
32-
"To upgrade a single library use the 'install' command.",
33-
Example: " " + cli.AppName + " lib upgrade",
34-
Args: cobra.NoArgs,
35-
Run: runUpgradeCommand,
36-
}
37-
return listCommand
38-
}
28+
func LibraryUpgradeAll(ctx context.Context, req *rpc.LibraryUpgradeAllReq, downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
29+
lm := commands.GetLibraryManager(req)
3930

40-
func runUpgradeCommand(cmd *cobra.Command, args []string) {
41-
lm := cli.InitLibraryManager(cli.Config)
31+
// Obtain the list of upgradable libraries
4232
list := ListLibraries(lm, true)
43-
libReleases := []*librariesindex.Release{}
44-
for _, upgradeDesc := range list.Libraries {
45-
libReleases = append(libReleases, upgradeDesc.Available)
46-
//downloadLibrary(lm, upgradeDesc.Available)
4733

34+
for _, upgradeDesc := range list.Libraries {
35+
if err := downloadLibrary(lm, upgradeDesc.Available, downloadCB, taskCB); err != nil {
36+
return err
37+
}
38+
}
39+
for _, upgradeDesc := range list.Libraries {
40+
installLibrary(lm, upgradeDesc.Available, taskCB)
4841
}
49-
//installLibraries(lm, libReleases)
5042

51-
logrus.Info("Done")
43+
if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
44+
return fmt.Errorf("rescanning libraries: %s", err)
45+
}
46+
return nil
5247
}

daemon/client/client.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ func main() {
252252
}
253253
if err != nil {
254254
fmt.Printf("Upload error: %s\n", err)
255-
os.Exit(1)
255+
// os.Exit(1)
256+
break
256257
}
257258
if resp := uplResp.GetOutStream(); resp != nil {
258259
fmt.Printf(">> STDOUT: %s", resp)
@@ -349,15 +350,42 @@ func main() {
349350
}
350351
}
351352

352-
libInstall("0.15.2") // Install
353-
libInstall("0.15.3") // Replace
353+
libInstall("0.15.1") // Install
354+
libInstall("0.15.2") // Replace
355+
356+
// LIB UPGRADE
357+
fmt.Println("=== calling LibraryUpgradeAll()")
358+
libUpgradeAllRespStream, err := client.LibraryUpgradeAll(context.Background(), &rpc.LibraryUpgradeAllReq{
359+
Instance: instance,
360+
})
361+
if err != nil {
362+
fmt.Printf("Error upgrading all: %s\n", err)
363+
os.Exit(1)
364+
}
365+
for {
366+
resp, err := libUpgradeAllRespStream.Recv()
367+
if err == io.EOF {
368+
fmt.Printf("---> %+v\n", resp)
369+
fmt.Println()
370+
break
371+
}
372+
if err != nil {
373+
fmt.Printf("upgrading error: %s\n", err)
374+
os.Exit(1)
375+
}
376+
if resp.GetProgress() != nil {
377+
fmt.Printf(">> DOWNLOAD: %s\n", resp.GetProgress())
378+
}
379+
if resp.GetTaskProgress() != nil {
380+
fmt.Printf(">> TASK: %s\n", resp.GetTaskProgress())
381+
}
382+
}
354383

355384
// LIB UNINSTALL
356-
fmt.Println("=== calling LibraryUninstall(WiFi101@0.15.3)")
385+
fmt.Println("=== calling LibraryUninstall(WiFi101)")
357386
libUninstallRespStream, err := client.LibraryUninstall(context.Background(), &rpc.LibraryUninstallReq{
358387
Instance: instance,
359388
Name: "WiFi101",
360-
Version: "0.15.3",
361389
})
362390
if err != nil {
363391
fmt.Printf("Error uninstalling: %s\n", err)

daemon/daemon.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,14 @@ func (s *ArduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallReq, s
205205
}
206206
return stream.Send(&rpc.LibraryUninstallResp{})
207207
}
208+
209+
func (s *ArduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllReq, stream rpc.ArduinoCore_LibraryUpgradeAllServer) error {
210+
err := lib.LibraryUpgradeAll(stream.Context(), req,
211+
func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryUpgradeAllResp{Progress: p}) },
212+
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryUpgradeAllResp{TaskProgress: p}) },
213+
)
214+
if err != nil {
215+
return err
216+
}
217+
return stream.Send(&rpc.LibraryUpgradeAllResp{})
218+
}

0 commit comments

Comments
 (0)