|
| 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 | + |
1 | 16 | package instance
|
2 | 17 |
|
3 | 18 | import (
|
4 | 19 | "context"
|
5 |
| - "os" |
6 | 20 |
|
7 |
| - "github.com/arduino/arduino-cli/cli/errorcodes" |
8 |
| - "github.com/arduino/arduino-cli/cli/feedback" |
9 | 21 | "github.com/arduino/arduino-cli/cli/globals"
|
10 | 22 | "github.com/arduino/arduino-cli/cli/output"
|
11 | 23 | "github.com/arduino/arduino-cli/commands"
|
12 | 24 | rpc "github.com/arduino/arduino-cli/rpc/commands"
|
| 25 | + "github.com/pkg/errors" |
13 | 26 | "github.com/sirupsen/logrus"
|
14 |
| - "github.com/spf13/viper" |
15 | 27 | )
|
16 | 28 |
|
17 |
| -// CreateInstaceIgnorePlatformIndexErrors creates and return an instance of the |
| 29 | +// CreateInstanceIgnorePlatformIndexErrors creates and return an instance of the |
18 | 30 | // 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() |
21 | 34 | }
|
22 | 35 |
|
23 | 36 | // 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 |
32 | 41 | }
|
33 |
| - return resp.GetInstance() |
| 42 | + |
| 43 | + return resp.GetInstance(), checkPlatformErrors(resp) |
34 | 44 | }
|
35 | 45 |
|
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()) |
39 | 50 |
|
40 |
| - resp, err := commands.Init(context.Background(), req, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader()) |
| 51 | + // Init() failed |
41 | 52 | 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") |
44 | 54 | }
|
| 55 | + |
| 56 | + // Init() succeeded but there were errors loading library indexes, |
| 57 | + // let's rescan and try again |
45 | 58 | 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(), |
47 | 63 | &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") |
52 | 66 | }
|
| 67 | + |
| 68 | + // rescan libraries |
| 69 | + rescanResp, err := commands.Rescan(resp.GetInstance().GetId()) |
53 | 70 | 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") |
56 | 72 | }
|
57 |
| - resp.LibrariesIndexError = rescResp.LibrariesIndexError |
58 |
| - resp.PlatformsIndexErrors = rescResp.PlatformsIndexErrors |
59 |
| - } |
60 |
| - return resp |
61 |
| -} |
62 | 73 |
|
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 | + } |
65 | 78 |
|
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 |
68 | 83 | }
|
69 | 84 |
|
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 | + } |
75 | 98 |
|
76 |
| - return &rpc.InitReq{Configuration: conf} |
| 99 | + return nil |
77 | 100 | }
|
0 commit comments