Skip to content

Commit f7cdf72

Browse files
authored
Fix update indexes running when not necessary (#1063)
* Libs and cores update is now correctly run only on first CLI execution * Enhance error messages when updating indexes or local index files are missing
1 parent 1855b53 commit f7cdf72

File tree

8 files changed

+123
-35
lines changed

8 files changed

+123
-35
lines changed

cli/instance/instance.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ package instance
1717

1818
import (
1919
"context"
20+
"fmt"
21+
"strings"
2022

2123
"github.com/arduino/arduino-cli/cli/output"
2224
"github.com/arduino/arduino-cli/commands"
25+
"github.com/arduino/arduino-cli/configuration"
2326
rpc "github.com/arduino/arduino-cli/rpc/commands"
27+
"github.com/arduino/go-paths-helper"
2428
"github.com/pkg/errors"
2529
"github.com/sirupsen/logrus"
2630
)
@@ -51,9 +55,13 @@ func getInitResponse() (*rpc.InitResp, error) {
5155
return nil, errors.Wrap(err, "creating instance")
5256
}
5357

54-
// Init() succeeded but there were errors loading library indexes,
55-
// let's rescan and try again
56-
if resp.GetLibrariesIndexError() != "" {
58+
// Gets the data directory to verify if library_index.json and package_index.json exist
59+
dataDir := paths.New(configuration.Settings.GetString("directories.data"))
60+
61+
// The library_index.json file doesn't exists, that means the CLI is run for the first time
62+
// so we proceed with the first update that downloads the file
63+
libraryIndex := dataDir.Join("library_index.json")
64+
if libraryIndex.NotExist() {
5765
logrus.Warnf("There were errors loading the library index, trying again...")
5866

5967
// update all indexes
@@ -80,15 +88,11 @@ func getInitResponse() (*rpc.InitResp, error) {
8088
resp.PlatformsIndexErrors = rescanResp.PlatformsIndexErrors
8189
}
8290

83-
// Init() succeeded but there were errors loading platform indexes,
84-
// let's rescan and try again
85-
if resp.GetPlatformsIndexErrors() != nil {
86-
87-
// log each error
88-
for _, err := range resp.GetPlatformsIndexErrors() {
89-
logrus.Errorf("Error loading platform index: %v", err)
90-
}
91-
91+
// The package_index.json file doesn't exists, that means the CLI is run for the first time,
92+
// similarly to the library update we download that file and all the other package indexes
93+
// from additional_urls
94+
packageIndex := dataDir.Join("package_index.json")
95+
if packageIndex.NotExist() {
9296
// update platform index
9397
_, err := commands.UpdateIndex(context.Background(),
9498
&rpc.UpdateIndexReq{Instance: resp.GetInstance()}, output.ProgressBar())
@@ -124,8 +128,7 @@ func checkPlatformErrors(resp *rpc.InitResp) error {
124128
for _, err := range resp.GetPlatformsIndexErrors() {
125129
logrus.Errorf("Error loading platform index: %v", err)
126130
}
127-
// return
128-
return errors.New("There were errors loading platform indexes")
131+
return fmt.Errorf("error loading platform index: \n%v", strings.Join(resp.GetPlatformsIndexErrors(), "\n"))
129132
}
130133

131134
return nil

commands/download.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package commands
1717

1818
import (
19+
"errors"
1920
"time"
2021

2122
"github.com/arduino/arduino-cli/httpclient"
@@ -60,6 +61,10 @@ func Download(d *downloader.Downloader, label string, downloadCB DownloadProgres
6061
if d.Error() != nil {
6162
return d.Error()
6263
}
64+
// The URL is not reachable for some reason
65+
if d.Resp.StatusCode >= 400 && d.Resp.StatusCode <= 599 {
66+
return errors.New(d.Resp.Status)
67+
}
6368
downloadCB(&rpc.DownloadProgress{Completed: true})
6469
return nil
6570
}

commands/instances.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
230230
return nil, fmt.Errorf("downloading index %s: %s", URL, err)
231231
}
232232
coreIndexPath := indexpath.Join(path.Base(URL.Path))
233-
Download(d, "Updating index: "+coreIndexPath.Base(), downloadCB)
234-
if d.Error() != nil {
235-
return nil, fmt.Errorf("downloading index %s: %s", URL, d.Error())
233+
err = Download(d, "Updating index: "+coreIndexPath.Base(), downloadCB)
234+
if err != nil {
235+
return nil, fmt.Errorf("downloading index %s: %s", URL, err)
236236
}
237237

238238
// Check for signature

go.mod

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ require (
2121
github.com/h2non/filetype v1.0.8 // indirect
2222
github.com/imjasonmiller/godice v0.1.2
2323
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
24-
github.com/kr/pretty v0.2.0 // indirect
2524
github.com/kr/text v0.2.0 // indirect
2625
github.com/leonelquinteros/gotext v1.4.0
27-
github.com/marcinbor85/gohex v0.0.0-20200531163658-baab2527a9a2
2826
github.com/mattn/go-colorable v0.1.2
2927
github.com/mattn/go-isatty v0.0.8
3028
github.com/mattn/go-runewidth v0.0.9 // indirect
3129
github.com/miekg/dns v1.0.5 // indirect
32-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
3330
github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228 // indirect
3431
github.com/pkg/errors v0.9.1
3532
github.com/pmylund/sortutil v0.0.0-20120526081524-abeda66eb583
@@ -40,11 +37,10 @@ require (
4037
github.com/sirupsen/logrus v1.4.2
4138
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c
4239
github.com/spf13/jwalterweatherman v1.0.0
43-
github.com/spf13/pflag v1.0.3
4440
github.com/spf13/viper v1.6.2
4541
github.com/stretchr/testify v1.6.1
4642
go.bug.st/cleanup v1.0.0
47-
go.bug.st/downloader/v2 v2.0.1
43+
go.bug.st/downloader/v2 v2.1.0
4844
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18
4945
go.bug.st/serial v1.1.1
5046
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 // indirect
@@ -53,7 +49,6 @@ require (
5349
golang.org/x/text v0.3.2
5450
google.golang.org/grpc v1.27.0
5551
google.golang.org/protobuf v1.25.0
56-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
5752
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
5853
gopkg.in/src-d/go-git.v4 v4.13.1
5954
gopkg.in/yaml.v2 v2.3.0

0 commit comments

Comments
 (0)