Skip to content

Commit 83eddd1

Browse files
committed
Fixed progress bar in CLI mode
1 parent 4079544 commit 83eddd1

File tree

11 files changed

+112
-77
lines changed

11 files changed

+112
-77
lines changed

cli/core/install.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/arduino/arduino-cli/cli"
2424
"github.com/arduino/arduino-cli/commands/core"
25+
"github.com/arduino/arduino-cli/output"
2526
"github.com/arduino/arduino-cli/rpc"
2627
"github.com/sirupsen/logrus"
2728
"github.com/spf13/cobra"
@@ -54,7 +55,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
5455
PlatformPackage: platformRef.Package,
5556
Architecture: platformRef.PlatformArchitecture,
5657
Version: platformRef.PlatformVersion.String(),
57-
})
58+
}, output.DownloadProgressBar())
5859
// if err != nil {
5960
// formatter.PrintError(err, "Error during build")
6061
// os.Exit(cli.ErrGeneric)

cli/core/upgrade.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/arduino/arduino-cli/cli"
2424
"github.com/arduino/arduino-cli/commands/core"
25+
"github.com/arduino/arduino-cli/output"
2526
"github.com/arduino/arduino-cli/rpc"
2627
"github.com/sirupsen/logrus"
2728
"github.com/spf13/cobra"
@@ -53,6 +54,6 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
5354
PlatformPackage: platformRef.Package,
5455
Architecture: platformRef.PlatformArchitecture,
5556
Version: platformRef.PlatformVersion.String(),
56-
})
57+
}, output.DownloadProgressBar())
5758
}
5859
}

commands/core/download.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,19 @@ func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.T
9191
return err
9292
} else {
9393
return download(resp, toolRelease.String(), progressCallback)
94-
9594
}
9695
}
9796

97+
// TODO: Refactor this into output.*?
9898
func download(d *downloader.Downloader, label string, progressCallback func(*rpc.DownloadProgress)) error {
9999
if d == nil {
100100
// TODO: Already downloaded
101+
progressCallback(&rpc.DownloadProgress{
102+
File: label,
103+
Completed: true,
104+
})
101105
return nil
102106
}
103-
formatter.Print("Downloading " + label + "...")
104-
formatter.DownloadProgressBar(d, label)
105107
progressCallback(&rpc.DownloadProgress{
106108
File: label,
107109
Url: d.URL,
@@ -114,5 +116,6 @@ func download(d *downloader.Downloader, label string, progressCallback func(*rpc
114116
formatter.PrintError(d.Error(), "Error downloading "+label)
115117
return d.Error()
116118
}
119+
progressCallback(&rpc.DownloadProgress{Completed: true})
117120
return nil
118121
}

commands/core/install.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
semver "go.bug.st/relaxed-semver"
1313
)
1414

15-
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq) (*rpc.PlatformInstallResp, error) {
15+
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, progress commands.ProgressCB) (*rpc.PlatformInstallResp, error) {
1616
var version *semver.Version
1717
if req.Version != "" {
1818
if v, err := semver.Parse(req.Version); err == nil {
@@ -32,15 +32,17 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq) (*rpc.Pla
3232
return nil, fmt.Errorf("Could not determine platform dependencies", err)
3333
}
3434

35-
err = installPlatform(pm, platform, tools)
35+
err = installPlatform(pm, platform, tools, progress)
3636
if err != nil {
3737
return nil, err
3838
}
3939

4040
return &rpc.PlatformInstallResp{}, nil
4141
}
4242

43-
func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease) error {
43+
func installPlatform(pm *packagemanager.PackageManager,
44+
platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease,
45+
progress commands.ProgressCB) error {
4446
log := pm.Log.WithField("platform", platformRelease)
4547

4648
// Prerequisite checks before install
@@ -60,13 +62,10 @@ func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.P
6062
}
6163

6264
// Package download
63-
print := func(curr *rpc.DownloadProgress) {
64-
fmt.Printf(">> %v\n", curr)
65-
}
6665
for _, tool := range toolsToInstall {
67-
downloadTool(pm, tool, print)
66+
downloadTool(pm, tool, progress)
6867
}
69-
downloadPlatform(pm, platformRelease, print)
68+
downloadPlatform(pm, platformRelease, progress)
7069

7170
for _, tool := range toolsToInstall {
7271
InstallToolRelease(pm, tool)

commands/core/upgrade.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
semver "go.bug.st/relaxed-semver"
2929
)
3030

31-
func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq) (*rpc.PlatformUpgradeResp, error) {
31+
func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, progress commands.ProgressCB) (*rpc.PlatformUpgradeResp, error) {
3232
// Extract all PlatformReference to platforms that have updates
3333
var version *semver.Version
3434
if req.Version != "" {
@@ -44,14 +44,14 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq) (*rpc.Pla
4444
PlatformVersion: version}
4545
pm := commands.GetPackageManager(req)
4646

47-
err := UpgradePlatform(pm, ref)
47+
err := UpgradePlatform(pm, ref, progress)
4848
if err != nil {
4949
return nil, err
5050
}
5151
return &rpc.PlatformUpgradeResp{}, nil
5252
}
5353

54-
func UpgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference) error {
54+
func UpgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference, progress commands.ProgressCB) error {
5555

5656
if platformRef.PlatformVersion != nil {
5757
formatter.PrintErrorMessage("Invalid item " + platformRef.String() + ", upgrade doesn't accept parameters with version")
@@ -84,7 +84,7 @@ func UpgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemana
8484
if err != nil {
8585
return fmt.Errorf("Platform " + platformRef.String() + " is not installed")
8686
}
87-
err = installPlatform(pm, platform, tools)
87+
err = installPlatform(pm, platform, tools, progress)
8888
if err != nil {
8989
return err
9090
}

commands/lib/update_index.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ package lib
1919

2020
import (
2121
"context"
22-
"fmt"
2322

2423
"github.com/arduino/arduino-cli/cli"
2524
"github.com/arduino/arduino-cli/commands"
26-
"github.com/arduino/arduino-cli/rpc"
25+
"github.com/arduino/arduino-cli/output"
2726
"github.com/spf13/cobra"
2827
)
2928

@@ -36,9 +35,7 @@ func initUpdateIndexCommand() *cobra.Command {
3635
Args: cobra.NoArgs,
3736
Run: func(cmd *cobra.Command, args []string) {
3837
lm := cli.InitLibraryManager(cli.Config)
39-
commands.UpdateLibrariesIndex(context.Background(), lm, func(curr *rpc.DownloadProgress) {
40-
fmt.Printf(">> %+v\n", curr)
41-
})
38+
commands.UpdateLibrariesIndex(context.Background(), lm, output.DownloadProgressBar())
4239
},
4340
}
4441
}

commands/progress.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package commands
2+
3+
import "github.com/arduino/arduino-cli/rpc"
4+
5+
// ProgressCB is a callback to get updates on download progress
6+
type ProgressCB func(curr *rpc.DownloadProgress)

daemon/daemon.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (s *ArduinoCoreServerImpl) Compile(req *rpc.CompileReq, stream rpc.ArduinoC
6969
}
7070

7171
func (s *ArduinoCoreServerImpl) PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq) (*rpc.PlatformInstallResp, error) {
72-
return core.PlatformInstall(ctx, req)
72+
return core.PlatformInstall(ctx, req, func(*rpc.DownloadProgress) {})
7373
}
7474

7575
func (s *ArduinoCoreServerImpl) PlatformDownload(req *rpc.PlatformDownloadReq, stream rpc.ArduinoCore_PlatformDownloadServer) error {
@@ -87,5 +87,5 @@ func (s *ArduinoCoreServerImpl) PlatformUninstall(ctx context.Context, req *rpc.
8787
}
8888

8989
func (s *ArduinoCoreServerImpl) PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq) (*rpc.PlatformUpgradeResp, error) {
90-
return core.PlatformUpgrade(ctx, req)
90+
return core.PlatformUpgrade(ctx, req, func(*rpc.DownloadProgress) {})
9191
}

output/progressbar.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
3+
package output
4+
5+
import (
6+
"github.com/arduino/arduino-cli/rpc"
7+
pb "gopkg.in/cheggaaa/pb.v1"
8+
)
9+
10+
// DownloadProgressBar returns a progress bar callback
11+
func DownloadProgressBar() func(*rpc.DownloadProgress) {
12+
var bar *pb.ProgressBar
13+
var prefix string
14+
return func(curr *rpc.DownloadProgress) {
15+
if filename := curr.GetFile(); filename != "" {
16+
prefix = filename
17+
bar = pb.StartNew(int(curr.GetTotalSize()))
18+
bar.Prefix(prefix)
19+
bar.SetUnits(pb.U_BYTES)
20+
}
21+
if curr.GetDownloaded() != 0 {
22+
bar.Set(int(curr.GetDownloaded()))
23+
}
24+
if curr.GetCompleted() {
25+
bar.FinishPrintOver(prefix + " downloaded")
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)