Skip to content

Commit ec68c6d

Browse files
committed
Porting update_index command
1 parent c591d4d commit ec68c6d

File tree

9 files changed

+332
-138
lines changed

9 files changed

+332
-138
lines changed

arduino/cores/packageindex/index.go

-45
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,10 @@ package packageindex
2020
import (
2121
"encoding/json"
2222
"fmt"
23-
"io/ioutil"
24-
"net/url"
25-
"path"
2623

2724
"github.com/arduino/arduino-cli/arduino/cores"
2825
"github.com/arduino/arduino-cli/arduino/resources"
29-
"github.com/arduino/arduino-cli/common/formatter"
3026
"github.com/arduino/go-paths-helper"
31-
"github.com/sirupsen/logrus"
32-
"go.bug.st/downloader"
3327
"go.bug.st/relaxed-semver"
3428
)
3529

@@ -221,42 +215,3 @@ func LoadIndex(jsonIndexFile *paths.Path) (*Index, error) {
221215

222216
return &index, nil
223217
}
224-
225-
// TODO: This should be in packagemanager......
226-
func UpdateIndex(URL *url.URL, clipath *paths.Path) error {
227-
logrus.WithField("url", URL).Print("Updating index")
228-
229-
tmpFile, err := ioutil.TempFile("", "")
230-
if err != nil {
231-
return fmt.Errorf("Error creating temp file for download", err)
232-
233-
}
234-
if err := tmpFile.Close(); err != nil {
235-
return fmt.Errorf("Error creating temp file for download", err)
236-
}
237-
tmp := paths.New(tmpFile.Name())
238-
defer tmp.Remove()
239-
240-
d, err := downloader.Download(tmp.String(), URL.String())
241-
if err != nil {
242-
return fmt.Errorf("Error downloading index "+URL.String(), err)
243-
}
244-
coreIndexPath := clipath.Join(path.Base(URL.Path))
245-
formatter.DownloadProgressBar(d, "Updating index: "+coreIndexPath.Base())
246-
if d.Error() != nil {
247-
return fmt.Errorf("Error downloading index "+URL.String(), d.Error())
248-
}
249-
250-
if _, err := LoadIndex(tmp); err != nil {
251-
return fmt.Errorf("Invalid package index in "+URL.String(), err)
252-
}
253-
254-
if err := clipath.MkdirAll(); err != nil {
255-
return fmt.Errorf("Can't create data directory "+clipath.String(), err)
256-
}
257-
258-
if err := tmp.CopyTo(coreIndexPath); err != nil {
259-
return fmt.Errorf("Error saving downloaded index "+URL.String(), err)
260-
}
261-
return nil
262-
}

cli/core/update_index.go

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

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

23-
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
2423
"github.com/arduino/arduino-cli/cli"
25-
"github.com/arduino/arduino-cli/common/formatter"
24+
"github.com/arduino/arduino-cli/commands"
25+
"github.com/arduino/arduino-cli/output"
26+
"github.com/arduino/arduino-cli/rpc"
2627
"github.com/spf13/cobra"
2728
)
2829

@@ -39,11 +40,9 @@ func initUpdateIndexCommand() *cobra.Command {
3940
}
4041

4142
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
42-
for _, URL := range cli.Config.BoardManagerAdditionalUrls {
43-
err := packageindex.UpdateIndex(URL, cli.Config.IndexesDir())
44-
if err != nil {
45-
formatter.PrintError(err, "Error creating temp file for download")
46-
os.Exit(cli.ErrGeneric)
47-
}
48-
}
43+
instance := cli.CreateInstance()
44+
commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{
45+
Instance: instance,
46+
}, output.DownloadProgressBar())
47+
4948
}

commands/core/download.go

+2-28
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
"time"
2524

2625
"github.com/arduino/arduino-cli/arduino/cores"
2726
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2827
"github.com/arduino/arduino-cli/commands"
2928
"github.com/arduino/arduino-cli/rpc"
30-
"go.bug.st/downloader"
3129
semver "go.bug.st/relaxed-semver"
3230
)
3331

@@ -74,7 +72,7 @@ func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.
7472
if err != nil {
7573
return err
7674
}
77-
return download(resp, platformRelease.String(), downloadCB)
75+
return commands.Download(resp, platformRelease.String(), downloadCB)
7876
}
7977

8078
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB) error {
@@ -92,29 +90,5 @@ func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.T
9290
if err != nil {
9391
return err
9492
}
95-
return download(resp, toolRelease.String(), downloadCB)
96-
}
97-
98-
func download(d *downloader.Downloader, label string, downloadCB commands.DownloadProgressCB) error {
99-
if d == nil {
100-
// This signal means that the file is already downloaded
101-
downloadCB(&rpc.DownloadProgress{
102-
File: label,
103-
Completed: true,
104-
})
105-
return nil
106-
}
107-
downloadCB(&rpc.DownloadProgress{
108-
File: label,
109-
Url: d.URL,
110-
TotalSize: d.Size(),
111-
})
112-
d.RunAndPoll(func(downloaded int64) {
113-
downloadCB(&rpc.DownloadProgress{Downloaded: downloaded})
114-
}, 250*time.Millisecond)
115-
if d.Error() != nil {
116-
return d.Error()
117-
}
118-
downloadCB(&rpc.DownloadProgress{Completed: true})
119-
return nil
93+
return commands.Download(resp, toolRelease.String(), downloadCB)
12094
}

commands/instances.go

+82
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ package commands
55
import (
66
"context"
77
"fmt"
8+
"io/ioutil"
89
"net/url"
10+
"path"
911
"time"
1012

13+
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
14+
1115
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
1216
"github.com/arduino/arduino-cli/arduino/libraries"
1317
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
1418
"github.com/arduino/arduino-cli/configs"
1519
"github.com/arduino/arduino-cli/rpc"
1620
paths "github.com/arduino/go-paths-helper"
1721
"github.com/sirupsen/logrus"
22+
"go.bug.st/downloader"
1823
)
1924

2025
// this map contains all the running Arduino Core Services instances
@@ -128,6 +133,59 @@ func UpdateLibrariesIndex(ctx context.Context, lm *librariesmanager.LibrariesMan
128133
}
129134
}
130135

136+
func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB DownloadProgressCB) (*rpc.UpdateIndexResp, error) {
137+
id := req.Instance.Id
138+
coreInstance, ok := instances[id]
139+
140+
if !ok {
141+
return nil, fmt.Errorf("invalid handle")
142+
}
143+
indexpath := coreInstance.config.IndexesDir()
144+
for _, URL := range coreInstance.config.BoardManagerAdditionalUrls {
145+
logrus.WithField("url", URL).Print("Updating index")
146+
147+
tmpFile, err := ioutil.TempFile("", "")
148+
if err != nil {
149+
return nil, fmt.Errorf("Error creating temp file for download", err)
150+
151+
}
152+
if err := tmpFile.Close(); err != nil {
153+
return nil, fmt.Errorf("Error creating temp file for download", err)
154+
}
155+
tmp := paths.New(tmpFile.Name())
156+
defer tmp.Remove()
157+
158+
d, err := downloader.Download(tmp.String(), URL.String())
159+
if err != nil {
160+
return nil, fmt.Errorf("Error downloading index "+URL.String(), err)
161+
}
162+
coreIndexPath := indexpath.Join(path.Base(URL.Path))
163+
Download(d, "Updating index: "+coreIndexPath.Base(), downloadCB)
164+
if d.Error() != nil {
165+
return nil, fmt.Errorf("Error downloading index "+URL.String(), d.Error())
166+
}
167+
168+
if _, err := packageindex.LoadIndex(tmp); err != nil {
169+
return nil, fmt.Errorf("Invalid package index in "+URL.String(), err)
170+
}
171+
172+
if err := indexpath.MkdirAll(); err != nil {
173+
return nil, fmt.Errorf("Can't create data directory "+indexpath.String(), err)
174+
}
175+
176+
if err := tmp.CopyTo(coreIndexPath); err != nil {
177+
return nil, fmt.Errorf("Error saving downloaded index "+URL.String(), err)
178+
}
179+
180+
// err := packageindex.UpdateIndex(URL, coreInstance.config.IndexesDir())
181+
// if err != nil {
182+
// return nil, fmt.Errorf("cannot create file: %s", err)
183+
// }
184+
}
185+
Rescan(ctx, &rpc.RescanReq{Instance: req.Instance})
186+
return &rpc.UpdateIndexResp{}, nil
187+
}
188+
131189
func Rescan(ctx context.Context, req *rpc.RescanReq) (*rpc.RescanResp, error) {
132190
id := req.Instance.Id
133191
coreInstance, ok := instances[id]
@@ -205,3 +263,27 @@ func createInstance(ctx context.Context, config *configs.Configuration, getLibOn
205263
}
206264
return pm, lm, nil
207265
}
266+
267+
func Download(d *downloader.Downloader, label string, downloadCB DownloadProgressCB) error {
268+
if d == nil {
269+
// This signal means that the file is already downloaded
270+
downloadCB(&rpc.DownloadProgress{
271+
File: label,
272+
Completed: true,
273+
})
274+
return nil
275+
}
276+
downloadCB(&rpc.DownloadProgress{
277+
File: label,
278+
Url: d.URL,
279+
TotalSize: d.Size(),
280+
})
281+
d.RunAndPoll(func(downloaded int64) {
282+
downloadCB(&rpc.DownloadProgress{Downloaded: downloaded})
283+
}, 250*time.Millisecond)
284+
if d.Error() != nil {
285+
return d.Error()
286+
}
287+
downloadCB(&rpc.DownloadProgress{Completed: true})
288+
return nil
289+
}

daemon/client/client.go

+21
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ func main() {
166166
}
167167
}
168168

169+
uiRespStream, err := client.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{
170+
Instance: instance,
171+
})
172+
if err != nil {
173+
fmt.Printf("Error Upgrade platform: %s\n", err)
174+
os.Exit(1)
175+
}
176+
for {
177+
uiResp, err := uiRespStream.Recv()
178+
if err == io.EOF {
179+
break
180+
}
181+
if err != nil {
182+
fmt.Printf("Compile error: %s\n", err)
183+
os.Exit(1)
184+
}
185+
if uiResp.GetDownloadProgress() != nil {
186+
fmt.Printf(">> DOWNLOADc: %s\n", uiResp.GetDownloadProgress())
187+
}
188+
}
189+
169190
uninstallRespStream, err := client.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
170191
Instance: instance,
171192
PlatformPackage: "arduino",

daemon/daemon.go

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func (s *ArduinoCoreServerImpl) Rescan(ctx context.Context, req *rpc.RescanReq)
5252
return commands.Rescan(ctx, req)
5353
}
5454

55+
func (s *ArduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexReq, stream rpc.ArduinoCore_UpdateIndexServer) error {
56+
resp, err := commands.UpdateIndex(stream.Context(), req,
57+
func(p *rpc.DownloadProgress) { stream.Send(&rpc.UpdateIndexResp{DownloadProgress: p}) },
58+
)
59+
stream.Send(resp)
60+
return err
61+
}
62+
5563
func (s *ArduinoCoreServerImpl) Init(ctx context.Context, req *rpc.InitReq) (*rpc.InitResp, error) {
5664
return commands.Init(ctx, req)
5765
}

0 commit comments

Comments
 (0)