@@ -22,51 +22,51 @@ import (
22
22
"fmt"
23
23
"time"
24
24
25
- "go.bug.st/downloader"
26
- semver "go.bug.st/relaxed-semver"
27
-
28
25
"github.com/arduino/arduino-cli/arduino/cores"
29
26
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
30
27
"github.com/arduino/arduino-cli/commands"
31
- "github.com/arduino/arduino-cli/common/formatter"
32
28
"github.com/arduino/arduino-cli/rpc"
29
+ "go.bug.st/downloader"
30
+ semver "go.bug.st/relaxed-semver"
33
31
)
34
32
35
33
func PlatformDownload (ctx context.Context , req * rpc.PlatformDownloadReq , progressCallback func (* rpc.DownloadProgress )) (* rpc.PlatformDownloadResp , error ) {
36
- version , err := semver .Parse (req .Version )
37
- if err != nil {
38
- formatter .PrintError (err , "Error in version parsing" )
39
- return nil , fmt .Errorf ("parse from string error: %s" , err )
34
+ var version * semver.Version
35
+ if v , err := semver .Parse (req .Version ); err == nil {
36
+ version = v
37
+ } else {
38
+ return nil , fmt .Errorf ("invalid version: %s" , err )
40
39
}
41
- ref := & packagemanager.PlatformReference {
40
+
41
+ pm := commands .GetPackageManager (req )
42
+ platform , tools , err := pm .FindPlatformReleaseDependencies (& packagemanager.PlatformReference {
42
43
Package : req .PlatformPackage ,
43
44
PlatformArchitecture : req .Architecture ,
44
- PlatformVersion : version }
45
- pm := commands .GetPackageManager (req )
46
- platform , tools , err := pm .FindPlatformReleaseDependencies (ref )
45
+ PlatformVersion : version ,
46
+ })
47
47
if err != nil {
48
- formatter .PrintError (err , "Could not determine platform dependencies" )
49
- return nil , fmt .Errorf ("find platform dependencies error: %s" , err )
48
+ return nil , fmt .Errorf ("find platform dependencies: %s" , err )
50
49
}
50
+
51
51
err = downloadPlatform (pm , platform , progressCallback )
52
52
if err != nil {
53
53
return nil , err
54
54
}
55
+
55
56
for _ , tool := range tools {
56
57
err := downloadTool (pm , tool , progressCallback )
57
58
if err != nil {
58
- formatter .PrintError (err , "Could not determine platform dependencies" )
59
- return nil , fmt .Errorf ("find platform dependencies error: %s" , err )
59
+ return nil , fmt .Errorf ("downloading tool %s: %s" , tool , err )
60
60
}
61
61
}
62
+
62
63
return & rpc.PlatformDownloadResp {}, nil
63
64
}
64
65
65
66
func downloadPlatform (pm * packagemanager.PackageManager , platformRelease * cores.PlatformRelease , progressCallback func (* rpc.DownloadProgress )) error {
66
67
// Download platform
67
68
resp , err := pm .DownloadPlatformRelease (platformRelease )
68
69
if err != nil {
69
- formatter .PrintError (err , "Error downloading " + platformRelease .String ())
70
70
return err
71
71
}
72
72
return download (resp , platformRelease .String (), progressCallback )
@@ -75,8 +75,7 @@ func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.
75
75
func downloadTool (pm * packagemanager.PackageManager , tool * cores.ToolRelease , progressCallback func (* rpc.DownloadProgress )) error {
76
76
// Check if tool has a flavor available for the current OS
77
77
if tool .GetCompatibleFlavour () == nil {
78
- formatter .PrintErrorMessage ("The tool " + tool .String () + " is not available for the current OS" )
79
- return fmt .Errorf ("The tool " + tool .String () + " is not available" )
78
+ return fmt .Errorf ("tool %s not available for the current OS" , tool )
80
79
}
81
80
82
81
return DownloadToolRelease (pm , tool , progressCallback )
@@ -86,16 +85,14 @@ func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, pr
86
85
func DownloadToolRelease (pm * packagemanager.PackageManager , toolRelease * cores.ToolRelease , progressCallback func (* rpc.DownloadProgress )) error {
87
86
resp , err := pm .DownloadToolRelease (toolRelease )
88
87
if err != nil {
89
- formatter .PrintError (err , "Error downloading " + toolRelease .String ())
90
88
return err
91
89
}
92
90
return download (resp , toolRelease .String (), progressCallback )
93
91
}
94
92
95
- // TODO: Refactor this into output.*?
96
93
func download (d * downloader.Downloader , label string , progressCallback func (* rpc.DownloadProgress )) error {
97
94
if d == nil {
98
- // TODO: Already downloaded
95
+ // This signal means that the file is already downloaded
99
96
progressCallback (& rpc.DownloadProgress {
100
97
File : label ,
101
98
Completed : true ,
@@ -111,7 +108,6 @@ func download(d *downloader.Downloader, label string, progressCallback func(*rpc
111
108
progressCallback (& rpc.DownloadProgress {Downloaded : downloaded })
112
109
}, 250 * time .Millisecond )
113
110
if d .Error () != nil {
114
- formatter .PrintError (d .Error (), "Error downloading " + label )
115
111
return d .Error ()
116
112
}
117
113
progressCallback (& rpc.DownloadProgress {Completed : true })
0 commit comments