Skip to content

Commit 444217d

Browse files
author
Massimiliano Pippi
committed
Fix update index on fresh installs (#530)
* remove error checking on unused proto field, added deprecation comment * fix typo in function name * do not os.exit in library code * create data dir tree if it doesnt exist
1 parent bf01165 commit 444217d

24 files changed

+231
-142
lines changed

Diff for: cli/board/attach.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ var attachFlags struct {
5353
}
5454

5555
func runAttachCommand(cmd *cobra.Command, args []string) {
56-
instance := instance.CreateInstance()
56+
instance, err := instance.CreateInstance()
57+
if err != nil {
58+
feedback.Errorf("Attach board error: %v", err)
59+
os.Exit(errorcodes.ErrGeneric)
60+
}
5761

5862
var path *paths.Path
5963
if len(args) > 1 {
@@ -62,13 +66,12 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
6266
path = initSketchPath(path)
6367
}
6468

65-
_, err := board.Attach(context.Background(), &rpc.BoardAttachReq{
69+
if _, err = board.Attach(context.Background(), &rpc.BoardAttachReq{
6670
Instance: instance,
6771
BoardUri: args[0],
6872
SketchPath: path.String(),
6973
SearchTimeout: attachFlags.searchTimeout,
70-
}, output.TaskProgress())
71-
if err != nil {
74+
}, output.TaskProgress()); err != nil {
7275
feedback.Errorf("Attach board error: %v", err)
7376
os.Exit(errorcodes.ErrGeneric)
7477
}

Diff for: cli/board/details.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ var detailsCommand = &cobra.Command{
4141
}
4242

4343
func runDetailsCommand(cmd *cobra.Command, args []string) {
44+
inst, err := instance.CreateInstance()
45+
if err != nil {
46+
feedback.Errorf("Error getting board details: %v", err)
47+
os.Exit(errorcodes.ErrGeneric)
48+
}
49+
4450
res, err := board.Details(context.Background(), &rpc.BoardDetailsReq{
45-
Instance: instance.CreateInstance(),
51+
Instance: inst,
4652
Fqbn: args[0],
4753
})
4854

Diff for: cli/board/list.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ func runListCommand(cmd *cobra.Command, args []string) {
6161
time.Sleep(timeout)
6262
}
6363

64-
ports, err := board.List(instance.CreateInstance().GetId())
64+
inst, err := instance.CreateInstance()
65+
if err != nil {
66+
feedback.Errorf("Error detecting boards: %v", err)
67+
os.Exit(errorcodes.ErrGeneric)
68+
}
69+
70+
ports, err := board.List(inst.GetId())
6571
if err != nil {
6672
feedback.Errorf("Error detecting boards: %v", err)
6773
os.Exit(errorcodes.ErrNetwork)

Diff for: cli/board/listall.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ var listAllCommand = &cobra.Command{
4646

4747
// runListAllCommand list all installed boards
4848
func runListAllCommand(cmd *cobra.Command, args []string) {
49-
instance := instance.CreateInstance()
49+
inst, err := instance.CreateInstance()
50+
if err != nil {
51+
feedback.Errorf("Error listing boards: %v", err)
52+
os.Exit(errorcodes.ErrGeneric)
53+
}
5054

5155
list, err := board.ListAll(context.Background(), &rpc.BoardListAllReq{
52-
Instance: instance,
56+
Instance: inst,
5357
SearchArgs: args,
5458
})
5559
if err != nil {

Diff for: cli/compile/compile.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ func NewCommand() *cobra.Command {
8484
}
8585

8686
func run(cmd *cobra.Command, args []string) {
87-
instance := instance.CreateInstance()
87+
inst, err := instance.CreateInstance()
88+
if err != nil {
89+
feedback.Errorf("Error during build: %v", err)
90+
os.Exit(errorcodes.ErrGeneric)
91+
}
8892

8993
var path *paths.Path
9094
if len(args) > 0 {
@@ -93,8 +97,8 @@ func run(cmd *cobra.Command, args []string) {
9397

9498
sketchPath := initSketchPath(path)
9599

96-
_, err := compile.Compile(context.Background(), &rpc.CompileReq{
97-
Instance: instance,
100+
_, err = compile.Compile(context.Background(), &rpc.CompileReq{
101+
Instance: inst,
98102
Fqbn: fqbn,
99103
SketchPath: sketchPath.String(),
100104
ShowProperties: showProperties,
@@ -116,7 +120,7 @@ func run(cmd *cobra.Command, args []string) {
116120

117121
if uploadAfterCompile {
118122
_, err := upload.Upload(context.Background(), &rpc.UploadReq{
119-
Instance: instance,
123+
Instance: inst,
120124
Fqbn: fqbn,
121125
SketchPath: sketchPath.String(),
122126
Port: port,

Diff for: cli/core/download.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ func initDownloadCommand() *cobra.Command {
4747
}
4848

4949
func runDownloadCommand(cmd *cobra.Command, args []string) {
50-
instance := instance.CreateInstance()
50+
inst, err := instance.CreateInstance()
51+
if err != nil {
52+
feedback.Errorf("Error downloading: %v", err)
53+
os.Exit(errorcodes.ErrGeneric)
54+
}
55+
5156
logrus.Info("Executing `arduino core download`")
5257

5358
platformsRefs, err := globals.ParseReferenceArgs(args, true)
@@ -58,7 +63,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
5863

5964
for i, platformRef := range platformsRefs {
6065
platformDownloadreq := &rpc.PlatformDownloadReq{
61-
Instance: instance,
66+
Instance: inst,
6267
PlatformPackage: platformRef.PackageName,
6368
Architecture: platformRef.Architecture,
6469
Version: platformRef.Version,

Diff for: cli/core/install.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ func initInstallCommand() *cobra.Command {
4848
}
4949

5050
func runInstallCommand(cmd *cobra.Command, args []string) {
51-
instance := instance.CreateInstance()
51+
inst, err := instance.CreateInstance()
52+
if err != nil {
53+
feedback.Errorf("Error installing: %v", err)
54+
os.Exit(errorcodes.ErrGeneric)
55+
}
56+
5257
logrus.Info("Executing `arduino core install`")
5358

5459
platformsRefs, err := globals.ParseReferenceArgs(args, true)
@@ -59,7 +64,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
5964

6065
for _, platformRef := range platformsRefs {
6166
plattformInstallReq := &rpc.PlatformInstallReq{
62-
Instance: instance,
67+
Instance: inst,
6368
PlatformPackage: platformRef.PackageName,
6469
Architecture: platformRef.Architecture,
6570
Version: platformRef.Version,

Diff for: cli/core/list.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ var listFlags struct {
4949
}
5050

5151
func runListCommand(cmd *cobra.Command, args []string) {
52-
instance := instance.CreateInstance()
52+
inst, err := instance.CreateInstance()
53+
if err != nil {
54+
feedback.Errorf("Error listing platforms: %v", err)
55+
os.Exit(errorcodes.ErrGeneric)
56+
}
57+
5358
logrus.Info("Executing `arduino core list`")
5459

55-
platforms, err := core.GetPlatforms(instance.Id, listFlags.updatableOnly)
60+
platforms, err := core.GetPlatforms(inst.Id, listFlags.updatableOnly)
5661
if err != nil {
5762
feedback.Errorf("Error listing platforms: %v", err)
5863
os.Exit(errorcodes.ErrGeneric)

Diff for: cli/core/search.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ func initSearchCommand() *cobra.Command {
5151
}
5252

5353
func runSearchCommand(cmd *cobra.Command, args []string) {
54-
instance := instance.CreateInstance()
54+
inst, err := instance.CreateInstance()
55+
if err != nil {
56+
feedback.Errorf("Error saerching for platforms: %v", err)
57+
os.Exit(errorcodes.ErrGeneric)
58+
}
59+
5560
logrus.Info("Executing `arduino core search`")
5661

5762
arguments := strings.ToLower(strings.Join(args, " "))
58-
resp, err := core.PlatformSearch(instance.GetId(), arguments, allVersions)
63+
resp, err := core.PlatformSearch(inst.GetId(), arguments, allVersions)
5964
if err != nil {
6065
feedback.Errorf("Error saerching for platforms: %v", err)
6166
os.Exit(errorcodes.ErrGeneric)

Diff for: cli/core/uninstall.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ func initUninstallCommand() *cobra.Command {
4444
}
4545

4646
func runUninstallCommand(cmd *cobra.Command, args []string) {
47-
instance := instance.CreateInstance()
47+
inst, err := instance.CreateInstance()
48+
if err != nil {
49+
feedback.Errorf("Error uninstalling: %v", err)
50+
os.Exit(errorcodes.ErrGeneric)
51+
}
52+
4853
logrus.Info("Executing `arduino core uninstall`")
4954

5055
platformsRefs, err := globals.ParseReferenceArgs(args, true)
@@ -61,7 +66,7 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
6166
}
6267
for _, platformRef := range platformsRefs {
6368
_, err := core.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
64-
Instance: instance,
69+
Instance: inst,
6570
PlatformPackage: platformRef.PackageName,
6671
Architecture: platformRef.Architecture,
6772
}, output.NewTaskProgressCB())

Diff for: cli/core/update_index.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func initUpdateIndexCommand() *cobra.Command {
4444
}
4545

4646
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
47-
instance := instance.CreateInstaceIgnorePlatformIndexErrors()
47+
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
4848
logrus.Info("Executing `arduino core update-index`")
4949

5050
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{

Diff for: cli/core/upgrade.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,17 @@ func initUpgradeCommand() *cobra.Command {
4848
}
4949

5050
func runUpgradeCommand(cmd *cobra.Command, args []string) {
51-
instance := instance.CreateInstance()
51+
inst, err := instance.CreateInstance()
52+
if err != nil {
53+
feedback.Errorf("Error upgrading: %v", err)
54+
os.Exit(errorcodes.ErrGeneric)
55+
}
56+
5257
logrus.Info("Executing `arduino core upgrade`")
5358

5459
// if no platform was passed, upgrade allthethings
5560
if len(args) == 0 {
56-
targets, err := core.GetPlatforms(instance.Id, true)
61+
targets, err := core.GetPlatforms(inst.Id, true)
5762
if err != nil {
5863
feedback.Errorf("Error retrieving core list: %v", err)
5964
os.Exit(errorcodes.ErrGeneric)
@@ -85,7 +90,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
8590
}
8691

8792
r := &rpc.PlatformUpgradeReq{
88-
Instance: instance,
93+
Instance: inst,
8994
PlatformPackage: platformRef.PackageName,
9095
Architecture: platformRef.Architecture,
9196
}

Diff for: cli/instance/instance.go

+67-44
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,100 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
116
package instance
217

318
import (
419
"context"
5-
"os"
620

7-
"github.com/arduino/arduino-cli/cli/errorcodes"
8-
"github.com/arduino/arduino-cli/cli/feedback"
921
"github.com/arduino/arduino-cli/cli/globals"
1022
"github.com/arduino/arduino-cli/cli/output"
1123
"github.com/arduino/arduino-cli/commands"
1224
rpc "github.com/arduino/arduino-cli/rpc/commands"
25+
"github.com/pkg/errors"
1326
"github.com/sirupsen/logrus"
14-
"github.com/spf13/viper"
1527
)
1628

17-
// CreateInstaceIgnorePlatformIndexErrors creates and return an instance of the
29+
// CreateInstanceIgnorePlatformIndexErrors creates and return an instance of the
1830
// Arduino Core Engine, but won't stop on platforms index loading errors.
19-
func CreateInstaceIgnorePlatformIndexErrors() *rpc.Instance {
20-
return initInstance().GetInstance()
31+
func CreateInstanceIgnorePlatformIndexErrors() *rpc.Instance {
32+
i, _ := getInitResponse()
33+
return i.GetInstance()
2134
}
2235

2336
// CreateInstance creates and return an instance of the Arduino Core engine
24-
func CreateInstance() *rpc.Instance {
25-
resp := initInstance()
26-
if resp.GetPlatformsIndexErrors() != nil {
27-
for _, err := range resp.GetPlatformsIndexErrors() {
28-
feedback.Errorf("Error loading index: %v", err)
29-
}
30-
feedback.Errorf("Launch '%s core update-index' to fix or download indexes.", os.Args[0])
31-
os.Exit(errorcodes.ErrGeneric)
37+
func CreateInstance() (*rpc.Instance, error) {
38+
resp, err := getInitResponse()
39+
if err != nil {
40+
return nil, err
3241
}
33-
return resp.GetInstance()
42+
43+
return resp.GetInstance(), checkPlatformErrors(resp)
3444
}
3545

36-
func initInstance() *rpc.InitResp {
37-
logrus.Info("Initializing package manager")
38-
req := packageManagerInitReq()
46+
func getInitResponse() (*rpc.InitResp, error) {
47+
// invoke Init()
48+
resp, err := commands.Init(context.Background(), &rpc.InitReq{},
49+
output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
3950

40-
resp, err := commands.Init(context.Background(), req, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
51+
// Init() failed
4152
if err != nil {
42-
feedback.Errorf("Error initializing package manager: %v", err)
43-
os.Exit(errorcodes.ErrGeneric)
53+
return nil, errors.Wrap(err, "creating instance")
4454
}
55+
56+
// Init() succeeded but there were errors loading library indexes,
57+
// let's rescan and try again
4558
if resp.GetLibrariesIndexError() != "" {
46-
commands.UpdateLibrariesIndex(context.Background(),
59+
logrus.Warnf("There were errors loading the library index, trying again...")
60+
61+
// update all indexes
62+
err := commands.UpdateLibrariesIndex(context.Background(),
4763
&rpc.UpdateLibrariesIndexReq{Instance: resp.GetInstance()}, output.ProgressBar())
48-
rescResp, err := commands.Rescan(resp.GetInstance().GetId())
49-
if rescResp.GetLibrariesIndexError() != "" {
50-
feedback.Errorf("Error loading library index: %v", rescResp.GetLibrariesIndexError())
51-
os.Exit(errorcodes.ErrGeneric)
64+
if err != nil {
65+
return nil, errors.Wrap(err, "updating the library index")
5266
}
67+
68+
// rescan libraries
69+
rescanResp, err := commands.Rescan(resp.GetInstance().GetId())
5370
if err != nil {
54-
feedback.Errorf("Error loading library index: %v", err)
55-
os.Exit(errorcodes.ErrGeneric)
71+
return nil, errors.Wrap(err, "during rescan")
5672
}
57-
resp.LibrariesIndexError = rescResp.LibrariesIndexError
58-
resp.PlatformsIndexErrors = rescResp.PlatformsIndexErrors
59-
}
60-
return resp
61-
}
6273

63-
func packageManagerInitReq() *rpc.InitReq {
64-
urls := []string{globals.DefaultIndexURL}
74+
// errors persist
75+
if rescanResp.GetLibrariesIndexError() != "" {
76+
return nil, errors.New("still errors after rescan: " + rescanResp.GetLibrariesIndexError())
77+
}
6578

66-
for _, URL := range viper.GetStringSlice("board_manager.additional_urls") {
67-
urls = append(urls, URL)
79+
// succeeded, copy over PlatformsIndexErrors in case errors occurred
80+
// during rescan
81+
resp.LibrariesIndexError = ""
82+
resp.PlatformsIndexErrors = rescanResp.PlatformsIndexErrors
6883
}
6984

70-
conf := &rpc.Configuration{}
71-
conf.DataDir = viper.GetString("directories.Data")
72-
conf.DownloadsDir = viper.GetString("directories.Downloads")
73-
conf.BoardManagerAdditionalUrls = urls
74-
conf.SketchbookDir = viper.GetString("directories.User")
85+
return resp, nil
86+
}
87+
88+
func checkPlatformErrors(resp *rpc.InitResp) error {
89+
// Init() and/or rescan succeeded, but there were errors loading platform indexes
90+
if resp.GetPlatformsIndexErrors() != nil {
91+
// log each error
92+
for _, err := range resp.GetPlatformsIndexErrors() {
93+
logrus.Errorf("Error loading platform index: %v", err)
94+
}
95+
// return
96+
return errors.New("There were errors loading platform indexes")
97+
}
7598

76-
return &rpc.InitReq{Configuration: conf}
99+
return nil
77100
}

0 commit comments

Comments
 (0)