Skip to content

Commit 92d7489

Browse files
cmaglielorenzo-biava
authored andcommitted
Factored out output package import from release package (WIP 1/2)
There is still need to factor a function to convert a result.DownloadResult to output.ProcessResult.
1 parent 508113e commit 92d7489

File tree

5 files changed

+98
-29
lines changed

5 files changed

+98
-29
lines changed

commands/core/download.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,25 @@ func downloadToolsArchives(tools []*cores.ToolRelease, results *output.CoreProce
9393
downloads[tool.Tool.Name+"@"+tool.Version] = tool.GetCompatibleFlavour()
9494
}
9595
logrus.Info("Downloading tools")
96-
releases.ParallelDownload(downloads, false, "Downloaded", &results.Tools, commands.GenerateDownloadProgressFormatter())
96+
downloadRes := releases.ParallelDownload(downloads, false, commands.GenerateDownloadProgressFormatter())
97+
98+
for name, res := range downloadRes {
99+
path, err := downloads[name].ArchivePath()
100+
if err != nil {
101+
// FIXME: do something!!
102+
logrus.Error("Could not determine library archive path:", err)
103+
}
104+
status := ""
105+
if res.Error == nil {
106+
status = "Downloaded"
107+
}
108+
results.Tools = append(results.Tools, output.ProcessResult{
109+
ItemName: name,
110+
Path: path,
111+
Error: res.Error.Error(),
112+
Status: status,
113+
})
114+
}
97115
}
98116

99117
func downloadPlatformArchives(platforms []*cores.PlatformRelease, results *output.CoreProcessResults) {
@@ -102,5 +120,23 @@ func downloadPlatformArchives(platforms []*cores.PlatformRelease, results *outpu
102120
downloads[platform.Platform.Package.Name+":"+platform.Platform.Name+"@"+platform.Version] = platform.Resource
103121
}
104122
logrus.Info("Downloading cores")
105-
releases.ParallelDownload(downloads, false, "Downloaded", &results.Cores, commands.GenerateDownloadProgressFormatter())
123+
downloadRes := releases.ParallelDownload(downloads, false, commands.GenerateDownloadProgressFormatter())
124+
125+
for name, res := range downloadRes {
126+
path, err := downloads[name].ArchivePath()
127+
if err != nil {
128+
// FIXME: do something!!
129+
logrus.Error("Could not determine library archive path:", err)
130+
}
131+
status := ""
132+
if res.Error == nil {
133+
status = "Downloaded"
134+
}
135+
results.Cores = append(results.Cores, output.ProcessResult{
136+
ItemName: name,
137+
Path: path,
138+
Error: res.Error.Error(),
139+
Status: status,
140+
})
141+
}
106142
}

commands/lib/download.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,25 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
7474
}
7575

7676
logrus.Info("Downloading")
77-
releases.ParallelDownload(libsToDownload, false, "Downloaded", &outputResults.Libraries, commands.GenerateDownloadProgressFormatter())
77+
downloadRes := releases.ParallelDownload(libsToDownload, false, commands.GenerateDownloadProgressFormatter())
78+
// "Downloaded", &outputResults.Libraries
79+
for name, res := range downloadRes {
80+
path, err := libsToDownload[name].ArchivePath()
81+
if err != nil {
82+
// FIXME: do something!!
83+
logrus.Error("Could not determine library archive path:", err)
84+
}
85+
status := ""
86+
if res.Error == nil {
87+
status = "Installed"
88+
}
89+
outputResults.Libraries = append(outputResults.Libraries, output.ProcessResult{
90+
ItemName: name,
91+
Path: path,
92+
Error: res.Error.Error(),
93+
Status: status,
94+
})
95+
}
7896
logrus.Info("Download finished")
7997
formatter.Print(outputResults)
8098
logrus.Info("Done")

commands/lib/install.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,25 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
7373
}
7474

7575
logrus.Info("Downloading")
76-
releases.ParallelDownload(libsToDownload, false, "Installed", &outputResults.Libraries, commands.GenerateDownloadProgressFormatter())
76+
downloadRes := releases.ParallelDownload(libsToDownload, false, commands.GenerateDownloadProgressFormatter())
77+
// "Installed", &outputResults.Libraries,
78+
for name, res := range downloadRes {
79+
path, err := libsToDownload[name].ArchivePath()
80+
if err != nil {
81+
// FIXME: do something!!
82+
logrus.Error("Could not determine library archive path:", err)
83+
}
84+
status := ""
85+
if res.Error == nil {
86+
status = "Installed"
87+
}
88+
outputResults.Libraries = append(outputResults.Libraries, output.ProcessResult{
89+
ItemName: name,
90+
Path: path,
91+
Error: res.Error.Error(),
92+
Status: status,
93+
})
94+
}
7795
logrus.Info("Download finished")
7896

7997
logrus.Info("Installing")

common/releases/helpers.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"os"
3636

3737
"github.com/bcmi-labs/arduino-cli/common"
38-
"github.com/bcmi-labs/arduino-cli/common/formatter/output"
3938
"github.com/bcmi-labs/arduino-cli/task"
4039
"github.com/sirupsen/logrus"
4140
)
@@ -118,28 +117,25 @@ type ParallelDownloadProgressHandler interface {
118117
// OkStatus is used to tell the overlying process result ("Downloaded", "Installed", etc...)
119118
// An optional progressHandler can be passed in order to be notified of the status of the download.
120119
// DOES NOT RETURN since will append results to the provided refResults; use refResults.Results() to get them.
121-
func ParallelDownload(items map[string]*DownloadResource, forced bool, OkStatus string, refResults *[]output.ProcessResult,
122-
progressHandler ParallelDownloadProgressHandler) {
120+
func ParallelDownload(items map[string]*DownloadResource, forced bool, progressHandler ParallelDownloadProgressHandler) map[string]*DownloadResult {
123121

124122
// TODO (l.biava): Future improvements envision this utility as an object (say a Builder)
125123
// to simplify the passing of all those parameters, the progress handling closure, the outputResults
126124
// internally populated, etc.
127125

128-
itemC := len(items)
129-
tasks := make(map[string]task.Wrapper, itemC)
130-
paths := make(map[string]string, itemC)
126+
tasks := map[string]task.Wrapper{}
127+
res := map[string]*DownloadResult{}
131128

132-
logrus.Info(fmt.Sprintf("Initiating parallel download of %d tasks", itemC))
129+
logrus.Info(fmt.Sprintf("Initiating parallel download of %d resources", len(items)))
133130

134131
for itemName, item := range items {
135132
cached := IsCached(item)
136133
releaseNotNil := item != nil
137134
if forced || releaseNotNil && (!cached || checkLocalArchive(item) != nil) {
138135
// Notify the progress handler of the new task
139136
if progressHandler != nil {
140-
progressHandler.OnNewDownloadTask(itemName, int64(item.Size))
137+
progressHandler.OnNewDownloadTask(itemName, item.Size)
141138
}
142-
paths[itemName], _ = item.ArchivePath() // if the release exists the archivepath always exists
143139

144140
// Forward the per-file progress handler, if available
145141
// WARNING: This is using a closure on itemName!
@@ -154,13 +150,8 @@ func ParallelDownload(items map[string]*DownloadResource, forced bool, OkStatus
154150

155151
tasks[itemName] = downloadTask(item, getProgressHandler(itemName))
156152
} else if !forced && releaseNotNil && cached {
157-
// Consider OK
158-
path, _ := item.ArchivePath()
159-
*refResults = append(*refResults, output.ProcessResult{
160-
ItemName: itemName,
161-
Status: OkStatus,
162-
Path: path,
163-
})
153+
// Item already downloaded
154+
res[itemName] = &DownloadResult{AlreadyDownloaded: true}
164155
}
165156
}
166157

@@ -177,19 +168,14 @@ func ParallelDownload(items map[string]*DownloadResource, forced bool, OkStatus
177168

178169
for name, result := range results {
179170
if result.Error != nil {
180-
*refResults = append(*refResults, output.ProcessResult{
181-
ItemName: name,
182-
Error: result.Error.Error(),
183-
})
171+
res[name] = &DownloadResult{Error: result.Error}
184172
} else {
185-
*refResults = append(*refResults, output.ProcessResult{
186-
ItemName: name,
187-
Status: OkStatus,
188-
Path: paths[name],
189-
})
173+
res[name] = &DownloadResult{}
190174
}
191175
}
192176
}
177+
178+
return res
193179
}
194180

195181
// OpenLocalArchiveForDownload open local archive if present

common/releases/structs.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,14 @@ func (r *DownloadResource) ArchivePath() (string, error) {
5252
}
5353
return filepath.Join(staging, r.ArchiveFileName), nil
5454
}
55+
56+
// DownloadResult contains the result of a download
57+
type DownloadResult struct {
58+
// Error is nil if the download is successful otherwise
59+
// it contains the reason for the download failure
60+
Error error
61+
62+
// AlreadyDownloaded is true if a cache file is found
63+
// and, consequently, the download has not been executed
64+
AlreadyDownloaded bool
65+
}

0 commit comments

Comments
 (0)