Skip to content

Commit 5df31b9

Browse files
committed
Merge branch 'daemon' of github.com:cmaglie/arduino-cli into HEAD
2 parents 89d1780 + 695f23c commit 5df31b9

File tree

6 files changed

+258
-166
lines changed

6 files changed

+258
-166
lines changed

cli/cli.go

+41-31
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package cli
2020
import (
2121
"context"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"os"
2526
"path/filepath"
@@ -84,17 +85,6 @@ var AppName = filepath.Base(os.Args[0])
8485

8586
var Config *configs.Configuration
8687

87-
// InitPackageAndLibraryManagerWithoutBundles initializes the PackageManager
88-
// and the LibraryManager but ignores bundles and user installed cores
89-
func InitPackageAndLibraryManagerWithoutBundles() (*packagemanager.PackageManager, *librariesmanager.LibrariesManager) {
90-
logrus.Info("Package manager will scan only managed hardware folder")
91-
92-
fakeResult := false
93-
Config.IDEBundledCheckResult = &fakeResult
94-
Config.SketchbookDir = nil
95-
return InitPackageAndLibraryManager()
96-
}
97-
9888
func packageManagerInitReq() *rpc.InitReq {
9989
urls := []string{}
10090
for _, URL := range Config.BoardManagerAdditionalUrls {
@@ -112,41 +102,61 @@ func packageManagerInitReq() *rpc.InitReq {
112102
return &rpc.InitReq{Configuration: conf}
113103
}
114104

105+
func InitInstance(libManagerOnly bool) *rpc.InitResp {
106+
if libManagerOnly {
107+
logrus.Info("Initializing library manager")
108+
} else {
109+
logrus.Info("Initializing package manager")
110+
}
111+
req := packageManagerInitReq()
112+
req.LibraryManagerOnly = libManagerOnly
113+
resp, err := commands.Init(context.Background(), req)
114+
if err != nil {
115+
if libManagerOnly {
116+
formatter.PrintError(err, "Error initializing library manager")
117+
} else {
118+
formatter.PrintError(err, "Error initializing package manager")
119+
}
120+
os.Exit(ErrGeneric)
121+
}
122+
return resp
123+
}
124+
115125
// CreateInstance creates and return an instance of the Arduino Core engine
116126
func CreateInstance() *rpc.Instance {
117-
logrus.Info("Initializing package manager")
118-
resp, err := commands.Init(context.Background(), packageManagerInitReq())
119-
if err != nil {
120-
formatter.PrintError(err, "Error initializing package manager")
121-
os.Exit(rpc.ErrGeneric)
127+
resp := InitInstance(false)
128+
if resp.GetPlatformsIndexErrors() != nil {
129+
for _, err := range resp.GetPlatformsIndexErrors() {
130+
formatter.PrintError(errors.New(err), "Error loading index")
131+
}
132+
formatter.PrintErrorMessage("Launch '" + AppName + " core update-index' to fix or download indexes.")
133+
os.Exit(ErrGeneric)
122134
}
123135
return resp.GetInstance()
124136
}
125137

126138
// InitPackageAndLibraryManager initializes the PackageManager and the LibaryManager
127139
// TODO: for the daemon mode, this might be called at startup, but for now only commands needing the PM will call it
128140
func InitPackageAndLibraryManager() (*packagemanager.PackageManager, *librariesmanager.LibrariesManager) {
129-
logrus.Info("Initializing package manager")
130-
resp, err := commands.Init(context.Background(), packageManagerInitReq())
131-
if err != nil {
132-
formatter.PrintError(err, "Error initializing package manager")
133-
os.Exit(rpc.ErrGeneric)
134-
}
141+
resp := InitInstance(false)
135142
return commands.GetPackageManager(resp), commands.GetLibraryManager(resp)
136143
}
137144

145+
// InitPackageAndLibraryManagerWithoutBundles initializes the PackageManager
146+
// and the LibraryManager but ignores bundles and user installed cores
147+
func InitPackageAndLibraryManagerWithoutBundles() (*packagemanager.PackageManager, *librariesmanager.LibrariesManager) {
148+
logrus.Info("Package manager will scan only managed hardware folder")
149+
150+
fakeResult := false
151+
Config.IDEBundledCheckResult = &fakeResult
152+
Config.SketchbookDir = nil
153+
return InitPackageAndLibraryManager()
154+
}
155+
138156
// InitLibraryManager initializes the LibraryManager. If pm is nil, the library manager will not handle core-libraries.
139157
// TODO: for the daemon mode, this might be called at startup, but for now only commands needing the PM will call it
140158
func InitLibraryManager(cfg *configs.Configuration) *librariesmanager.LibrariesManager {
141-
req := packageManagerInitReq()
142-
req.LibraryManagerOnly = true
143-
144-
logrus.Info("Initializing library manager")
145-
resp, err := commands.Init(context.Background(), req)
146-
if err != nil {
147-
formatter.PrintError(err, "Error initializing library manager")
148-
os.Exit(rpc.ErrGeneric)
149-
}
159+
resp := InitInstance(true)
150160
return commands.GetLibraryManager(resp)
151161
}
152162

cli/core/update_index.go

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

4545
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
46-
instance := cli.CreateInstance()
46+
instance := cli.InitInstance(false).GetInstance()
4747
logrus.Info("Executing `arduino core update-index`")
4848

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

commands/instances.go

+29-25
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ func Init(ctx context.Context, req *rpc.InitReq) (*rpc.InitResp, error) {
9595
return nil, fmt.Errorf("parsing url %s: %s", rawurl, err)
9696
}
9797
}
98-
pm, lm, err := createInstance(ctx, config, req.GetLibraryManagerOnly())
98+
pm, lm, reqPltIndex, reqLibIndex, err := createInstance(ctx, config, req.GetLibraryManagerOnly())
9999
if err != nil {
100-
return nil, fmt.Errorf("Impossible create instance")
100+
return nil, fmt.Errorf("cannot initialize package manager: %s", err)
101101
}
102102
instance := &CoreInstance{
103103
config: config,
@@ -109,7 +109,9 @@ func Init(ctx context.Context, req *rpc.InitReq) (*rpc.InitResp, error) {
109109
instances[handle] = instance
110110

111111
return &rpc.InitResp{
112-
Instance: &rpc.Instance{Id: handle},
112+
Instance: &rpc.Instance{Id: handle},
113+
PlatformsIndexErrors: reqPltIndex,
114+
LibrariesIndexError: reqLibIndex,
113115
}, nil
114116
}
115117

@@ -161,35 +163,34 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
161163

162164
tmpFile, err := ioutil.TempFile("", "")
163165
if err != nil {
164-
return nil, fmt.Errorf("Error creating temp file for download", err)
165-
166+
return nil, fmt.Errorf("creating temp file for download: %s", err)
166167
}
167168
if err := tmpFile.Close(); err != nil {
168-
return nil, fmt.Errorf("Error creating temp file for download", err)
169+
return nil, fmt.Errorf("creating temp file for download: %s", err)
169170
}
170171
tmp := paths.New(tmpFile.Name())
171172
defer tmp.Remove()
172173

173174
d, err := downloader.Download(tmp.String(), URL.String())
174175
if err != nil {
175-
return nil, fmt.Errorf("Error downloading index "+URL.String(), err)
176+
return nil, fmt.Errorf("downloading index %s: %s", URL, err)
176177
}
177178
coreIndexPath := indexpath.Join(path.Base(URL.Path))
178179
Download(d, "Updating index: "+coreIndexPath.Base(), downloadCB)
179180
if d.Error() != nil {
180-
return nil, fmt.Errorf("Error downloading index "+URL.String(), d.Error())
181+
return nil, fmt.Errorf("downloading index %s: %s", URL, d.Error())
181182
}
182183

183184
if _, err := packageindex.LoadIndex(tmp); err != nil {
184-
return nil, fmt.Errorf("Invalid package index in "+URL.String(), err)
185+
return nil, fmt.Errorf("invalid package index in %s: %s", URL, err)
185186
}
186187

187188
if err := indexpath.MkdirAll(); err != nil {
188-
return nil, fmt.Errorf("Can't create data directory "+indexpath.String(), err)
189+
return nil, fmt.Errorf("can't create data directory %s: %s", indexpath, err)
189190
}
190191

191192
if err := tmp.CopyTo(coreIndexPath); err != nil {
192-
return nil, fmt.Errorf("Error saving downloaded index "+URL.String(), err)
193+
return nil, fmt.Errorf("saving downloaded index %s: %s", URL, err)
193194
}
194195
}
195196
Rescan(ctx, &rpc.RescanReq{Instance: req.Instance})
@@ -203,17 +204,21 @@ func Rescan(ctx context.Context, req *rpc.RescanReq) (*rpc.RescanResp, error) {
203204
return nil, fmt.Errorf("invalid handle")
204205
}
205206

206-
pm, lm, err := createInstance(ctx, coreInstance.config, coreInstance.getLibOnly)
207+
pm, lm, reqPltIndex, reqLibIndex, err := createInstance(ctx, coreInstance.config, coreInstance.getLibOnly)
207208
if err != nil {
208-
return nil, fmt.Errorf("rescanning: %s", err)
209+
return nil, fmt.Errorf("rescanning filesystem: %s", err)
209210
}
210211
coreInstance.pm = pm
211212
coreInstance.lm = lm
212-
return &rpc.RescanResp{}, nil
213+
return &rpc.RescanResp{
214+
PlatformsIndexErrors: reqPltIndex,
215+
LibrariesIndexError: reqLibIndex,
216+
}, nil
213217
}
214218

215-
func createInstance(ctx context.Context, config *configs.Configuration, getLibOnly bool) (*packagemanager.PackageManager, *librariesmanager.LibrariesManager, error) {
219+
func createInstance(ctx context.Context, config *configs.Configuration, getLibOnly bool) (*packagemanager.PackageManager, *librariesmanager.LibrariesManager, []string, string, error) {
216220
var pm *packagemanager.PackageManager
221+
platformIndexErrors := []string{}
217222
if !getLibOnly {
218223
pm = packagemanager.NewPackageManager(
219224
config.IndexesDir(),
@@ -223,14 +228,17 @@ func createInstance(ctx context.Context, config *configs.Configuration, getLibOn
223228

224229
for _, URL := range config.BoardManagerAdditionalUrls {
225230
if err := pm.LoadPackageIndex(URL); err != nil {
226-
return nil, nil, fmt.Errorf("loading "+URL.String()+" package index: %s", err)
231+
platformIndexErrors = append(platformIndexErrors, err.Error())
227232
}
228233
}
229234

230235
if err := pm.LoadHardware(config); err != nil {
231-
return nil, nil, fmt.Errorf("loading hardware packages: %s", err)
236+
return nil, nil, nil, "", fmt.Errorf("loading hardware packages: %s", err)
232237
}
233238
}
239+
if len(platformIndexErrors) == 0 {
240+
platformIndexErrors = nil
241+
}
234242

235243
// Initialize library manager
236244
// --------------------------
@@ -258,20 +266,16 @@ func createInstance(ctx context.Context, config *configs.Configuration, getLibOn
258266
}
259267

260268
// Load index and auto-update it if needed
269+
librariesIndexError := ""
261270
if err := lm.LoadIndex(); err != nil {
262-
UpdateLibrariesIndex(ctx, lm, func(curr *rpc.DownloadProgress) {
263-
fmt.Printf(">> %+v\n", curr)
264-
})
265-
if err := lm.LoadIndex(); err != nil {
266-
return nil, nil, fmt.Errorf("loading libraries index: %s", err)
267-
}
271+
librariesIndexError = err.Error()
268272
}
269273

270274
// Scan for libraries
271275
if err := lm.RescanLibraries(); err != nil {
272-
return nil, nil, fmt.Errorf("libraries rescan: %s", err)
276+
return nil, nil, nil, "", fmt.Errorf("libraries rescan: %s", err)
273277
}
274-
return pm, lm, nil
278+
return pm, lm, platformIndexErrors, librariesIndexError, nil
275279
}
276280

277281
func Download(d *downloader.Downloader, label string, downloadCB DownloadProgressCB) error {

0 commit comments

Comments
 (0)