Skip to content

Commit 75b755a

Browse files
committed
Porting of command/core/download
1 parent 8e7db44 commit 75b755a

File tree

2 files changed

+24
-85
lines changed

2 files changed

+24
-85
lines changed

cli/core/download.go

+10-59
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818
package core
1919

2020
import (
21-
"os"
21+
"context"
2222

23-
"go.bug.st/downloader"
24-
25-
"github.com/arduino/arduino-cli/arduino/cores"
26-
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2723
"github.com/arduino/arduino-cli/cli"
28-
"github.com/arduino/arduino-cli/common/formatter"
24+
"github.com/arduino/arduino-cli/commands/core"
25+
"github.com/arduino/arduino-cli/rpc"
2926
"github.com/sirupsen/logrus"
3027
"github.com/spf13/cobra"
3128
)
@@ -45,62 +42,16 @@ func initDownloadCommand() *cobra.Command {
4542
}
4643

4744
func runDownloadCommand(cmd *cobra.Command, args []string) {
45+
instance := cli.CreateInstance()
4846
logrus.Info("Executing `arduino core download`")
4947

5048
platformsRefs := parsePlatformReferenceArgs(args)
51-
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
5249
for _, platformRef := range platformsRefs {
53-
downloadPlatformByRef(pm, platformRef)
54-
}
55-
}
56-
57-
func downloadPlatformByRef(pm *packagemanager.PackageManager, platformsRef *packagemanager.PlatformReference) {
58-
platform, tools, err := pm.FindPlatformReleaseDependencies(platformsRef)
59-
if err != nil {
60-
formatter.PrintError(err, "Could not determine platform dependencies")
61-
os.Exit(cli.ErrBadCall)
62-
}
63-
downloadPlatform(pm, platform)
64-
for _, tool := range tools {
65-
downloadTool(pm, tool)
66-
}
67-
}
68-
69-
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {
70-
// Download platform
71-
resp, err := pm.DownloadPlatformRelease(platformRelease)
72-
download(resp, err, platformRelease.String())
73-
}
74-
75-
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease) {
76-
// Check if tool has a flavor available for the current OS
77-
if tool.GetCompatibleFlavour() == nil {
78-
formatter.PrintErrorMessage("The tool " + tool.String() + " is not available for the current OS")
79-
os.Exit(cli.ErrGeneric)
80-
}
81-
82-
DownloadToolRelease(pm, tool)
83-
}
84-
85-
// DownloadToolRelease downloads a ToolRelease
86-
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
87-
resp, err := pm.DownloadToolRelease(toolRelease)
88-
download(resp, err, toolRelease.String())
89-
}
90-
91-
func download(d *downloader.Downloader, err error, label string) {
92-
if err != nil {
93-
formatter.PrintError(err, "Error downloading "+label)
94-
os.Exit(cli.ErrNetwork)
95-
}
96-
if d == nil {
97-
formatter.Print(label + " already downloaded")
98-
return
99-
}
100-
formatter.Print("Downloading " + label + "...")
101-
formatter.DownloadProgressBar(d, label)
102-
if d.Error() != nil {
103-
formatter.PrintError(d.Error(), "Error downloading "+label)
104-
os.Exit(cli.ErrNetwork)
50+
core.PlatformDownload(context.Background(), &rpc.PlatformDownloadReq{
51+
Instance: instance,
52+
PlatformPackage: platformRef.Package,
53+
Architecture: platformRef.PlatformArchitecture,
54+
Version: platformRef.PlatformVersion.String(),
55+
})
10556
}
10657
}

commands/core/download.go

+14-26
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,31 @@
1818
package core
1919

2020
import (
21+
"context"
2122
"os"
2223

2324
"go.bug.st/downloader"
25+
semver "go.bug.st/relaxed-semver"
2426

2527
"github.com/arduino/arduino-cli/arduino/cores"
2628
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2729
"github.com/arduino/arduino-cli/cli"
2830
"github.com/arduino/arduino-cli/common/formatter"
29-
"github.com/sirupsen/logrus"
30-
"github.com/spf13/cobra"
31+
"github.com/arduino/arduino-cli/rpc"
3132
)
3233

33-
func initDownloadCommand() *cobra.Command {
34-
downloadCommand := &cobra.Command{
35-
Use: "download [PACKAGER:ARCH[=VERSION]](S)",
36-
Short: "Downloads one or more cores and corresponding tool dependencies.",
37-
Long: "Downloads one or more cores and corresponding tool dependencies.",
38-
Example: "" +
39-
" " + cli.AppName + " core download arduino:samd # to download the latest version of arduino SAMD core.\n" +
40-
" " + cli.AppName + " core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
41-
Args: cobra.MinimumNArgs(1),
42-
Run: runDownloadCommand,
34+
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq) (*rpc.PlatformDownloadResp, error) {
35+
version, err := semver.Parse(req.Version)
36+
if err != nil {
37+
formatter.PrintError(err, "version not readable")
38+
os.Exit(cli.ErrBadCall)
4339
}
44-
return downloadCommand
45-
}
46-
47-
func runDownloadCommand(cmd *cobra.Command, args []string) {
48-
logrus.Info("Executing `arduino core download`")
49-
50-
platformsRefs := parsePlatformReferenceArgs(args)
40+
ref := &packagemanager.PlatformReference{
41+
Package: req.PlatformPackage,
42+
PlatformArchitecture: req.Architecture,
43+
PlatformVersion: version}
5144
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
52-
for _, platformRef := range platformsRefs {
53-
downloadPlatformByRef(pm, platformRef)
54-
}
55-
}
56-
57-
func downloadPlatformByRef(pm *packagemanager.PackageManager, platformsRef *packagemanager.PlatformReference) {
58-
platform, tools, err := pm.FindPlatformReleaseDependencies(platformsRef)
45+
platform, tools, err := pm.FindPlatformReleaseDependencies(ref)
5946
if err != nil {
6047
formatter.PrintError(err, "Could not determine platform dependencies")
6148
os.Exit(cli.ErrBadCall)
@@ -64,6 +51,7 @@ func downloadPlatformByRef(pm *packagemanager.PackageManager, platformsRef *pack
6451
for _, tool := range tools {
6552
downloadTool(pm, tool)
6653
}
54+
return nil, nil
6755
}
6856

6957
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {

0 commit comments

Comments
 (0)