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
parent bf01165e
...@@ -53,7 +53,11 @@ var attachFlags struct { ...@@ -53,7 +53,11 @@ var attachFlags struct {
} }
func runAttachCommand(cmd *cobra.Command, args []string) { func runAttachCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() instance, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Attach board error: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
var path *paths.Path var path *paths.Path
if len(args) > 1 { if len(args) > 1 {
...@@ -62,13 +66,12 @@ func runAttachCommand(cmd *cobra.Command, args []string) { ...@@ -62,13 +66,12 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
path = initSketchPath(path) path = initSketchPath(path)
} }
_, err := board.Attach(context.Background(), &rpc.BoardAttachReq{ if _, err = board.Attach(context.Background(), &rpc.BoardAttachReq{
Instance: instance, Instance: instance,
BoardUri: args[0], BoardUri: args[0],
SketchPath: path.String(), SketchPath: path.String(),
SearchTimeout: attachFlags.searchTimeout, SearchTimeout: attachFlags.searchTimeout,
}, output.TaskProgress()) }, output.TaskProgress()); err != nil {
if err != nil {
feedback.Errorf("Attach board error: %v", err) feedback.Errorf("Attach board error: %v", err)
os.Exit(errorcodes.ErrGeneric) os.Exit(errorcodes.ErrGeneric)
} }
......
...@@ -41,8 +41,14 @@ var detailsCommand = &cobra.Command{ ...@@ -41,8 +41,14 @@ var detailsCommand = &cobra.Command{
} }
func runDetailsCommand(cmd *cobra.Command, args []string) { func runDetailsCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error getting board details: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
res, err := board.Details(context.Background(), &rpc.BoardDetailsReq{ res, err := board.Details(context.Background(), &rpc.BoardDetailsReq{
Instance: instance.CreateInstance(), Instance: inst,
Fqbn: args[0], Fqbn: args[0],
}) })
......
...@@ -61,7 +61,13 @@ func runListCommand(cmd *cobra.Command, args []string) { ...@@ -61,7 +61,13 @@ func runListCommand(cmd *cobra.Command, args []string) {
time.Sleep(timeout) time.Sleep(timeout)
} }
ports, err := board.List(instance.CreateInstance().GetId()) inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error detecting boards: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
ports, err := board.List(inst.GetId())
if err != nil { if err != nil {
feedback.Errorf("Error detecting boards: %v", err) feedback.Errorf("Error detecting boards: %v", err)
os.Exit(errorcodes.ErrNetwork) os.Exit(errorcodes.ErrNetwork)
......
...@@ -46,10 +46,14 @@ var listAllCommand = &cobra.Command{ ...@@ -46,10 +46,14 @@ var listAllCommand = &cobra.Command{
// runListAllCommand list all installed boards // runListAllCommand list all installed boards
func runListAllCommand(cmd *cobra.Command, args []string) { func runListAllCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error listing boards: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
list, err := board.ListAll(context.Background(), &rpc.BoardListAllReq{ list, err := board.ListAll(context.Background(), &rpc.BoardListAllReq{
Instance: instance, Instance: inst,
SearchArgs: args, SearchArgs: args,
}) })
if err != nil { if err != nil {
......
...@@ -84,7 +84,11 @@ func NewCommand() *cobra.Command { ...@@ -84,7 +84,11 @@ func NewCommand() *cobra.Command {
} }
func run(cmd *cobra.Command, args []string) { func run(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error during build: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
var path *paths.Path var path *paths.Path
if len(args) > 0 { if len(args) > 0 {
...@@ -93,8 +97,8 @@ func run(cmd *cobra.Command, args []string) { ...@@ -93,8 +97,8 @@ func run(cmd *cobra.Command, args []string) {
sketchPath := initSketchPath(path) sketchPath := initSketchPath(path)
_, err := compile.Compile(context.Background(), &rpc.CompileReq{ _, err = compile.Compile(context.Background(), &rpc.CompileReq{
Instance: instance, Instance: inst,
Fqbn: fqbn, Fqbn: fqbn,
SketchPath: sketchPath.String(), SketchPath: sketchPath.String(),
ShowProperties: showProperties, ShowProperties: showProperties,
...@@ -116,7 +120,7 @@ func run(cmd *cobra.Command, args []string) { ...@@ -116,7 +120,7 @@ func run(cmd *cobra.Command, args []string) {
if uploadAfterCompile { if uploadAfterCompile {
_, err := upload.Upload(context.Background(), &rpc.UploadReq{ _, err := upload.Upload(context.Background(), &rpc.UploadReq{
Instance: instance, Instance: inst,
Fqbn: fqbn, Fqbn: fqbn,
SketchPath: sketchPath.String(), SketchPath: sketchPath.String(),
Port: port, Port: port,
......
...@@ -47,7 +47,12 @@ func initDownloadCommand() *cobra.Command { ...@@ -47,7 +47,12 @@ func initDownloadCommand() *cobra.Command {
} }
func runDownloadCommand(cmd *cobra.Command, args []string) { func runDownloadCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error downloading: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Info("Executing `arduino core download`") logrus.Info("Executing `arduino core download`")
platformsRefs, err := globals.ParseReferenceArgs(args, true) platformsRefs, err := globals.ParseReferenceArgs(args, true)
...@@ -58,7 +63,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) { ...@@ -58,7 +63,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
for i, platformRef := range platformsRefs { for i, platformRef := range platformsRefs {
platformDownloadreq := &rpc.PlatformDownloadReq{ platformDownloadreq := &rpc.PlatformDownloadReq{
Instance: instance, Instance: inst,
PlatformPackage: platformRef.PackageName, PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture, Architecture: platformRef.Architecture,
Version: platformRef.Version, Version: platformRef.Version,
......
...@@ -48,7 +48,12 @@ func initInstallCommand() *cobra.Command { ...@@ -48,7 +48,12 @@ func initInstallCommand() *cobra.Command {
} }
func runInstallCommand(cmd *cobra.Command, args []string) { func runInstallCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error installing: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Info("Executing `arduino core install`") logrus.Info("Executing `arduino core install`")
platformsRefs, err := globals.ParseReferenceArgs(args, true) platformsRefs, err := globals.ParseReferenceArgs(args, true)
...@@ -59,7 +64,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) { ...@@ -59,7 +64,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
for _, platformRef := range platformsRefs { for _, platformRef := range platformsRefs {
plattformInstallReq := &rpc.PlatformInstallReq{ plattformInstallReq := &rpc.PlatformInstallReq{
Instance: instance, Instance: inst,
PlatformPackage: platformRef.PackageName, PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture, Architecture: platformRef.Architecture,
Version: platformRef.Version, Version: platformRef.Version,
......
...@@ -49,10 +49,15 @@ var listFlags struct { ...@@ -49,10 +49,15 @@ var listFlags struct {
} }
func runListCommand(cmd *cobra.Command, args []string) { func runListCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error listing platforms: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Info("Executing `arduino core list`") logrus.Info("Executing `arduino core list`")
platforms, err := core.GetPlatforms(instance.Id, listFlags.updatableOnly) platforms, err := core.GetPlatforms(inst.Id, listFlags.updatableOnly)
if err != nil { if err != nil {
feedback.Errorf("Error listing platforms: %v", err) feedback.Errorf("Error listing platforms: %v", err)
os.Exit(errorcodes.ErrGeneric) os.Exit(errorcodes.ErrGeneric)
......
...@@ -51,11 +51,16 @@ func initSearchCommand() *cobra.Command { ...@@ -51,11 +51,16 @@ func initSearchCommand() *cobra.Command {
} }
func runSearchCommand(cmd *cobra.Command, args []string) { func runSearchCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error saerching for platforms: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Info("Executing `arduino core search`") logrus.Info("Executing `arduino core search`")
arguments := strings.ToLower(strings.Join(args, " ")) arguments := strings.ToLower(strings.Join(args, " "))
resp, err := core.PlatformSearch(instance.GetId(), arguments, allVersions) resp, err := core.PlatformSearch(inst.GetId(), arguments, allVersions)
if err != nil { if err != nil {
feedback.Errorf("Error saerching for platforms: %v", err) feedback.Errorf("Error saerching for platforms: %v", err)
os.Exit(errorcodes.ErrGeneric) os.Exit(errorcodes.ErrGeneric)
......
...@@ -44,7 +44,12 @@ func initUninstallCommand() *cobra.Command { ...@@ -44,7 +44,12 @@ func initUninstallCommand() *cobra.Command {
} }
func runUninstallCommand(cmd *cobra.Command, args []string) { func runUninstallCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error uninstalling: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Info("Executing `arduino core uninstall`") logrus.Info("Executing `arduino core uninstall`")
platformsRefs, err := globals.ParseReferenceArgs(args, true) platformsRefs, err := globals.ParseReferenceArgs(args, true)
...@@ -61,7 +66,7 @@ func runUninstallCommand(cmd *cobra.Command, args []string) { ...@@ -61,7 +66,7 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
} }
for _, platformRef := range platformsRefs { for _, platformRef := range platformsRefs {
_, err := core.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{ _, err := core.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
Instance: instance, Instance: inst,
PlatformPackage: platformRef.PackageName, PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture, Architecture: platformRef.Architecture,
}, output.NewTaskProgressCB()) }, output.NewTaskProgressCB())
......
...@@ -44,7 +44,7 @@ func initUpdateIndexCommand() *cobra.Command { ...@@ -44,7 +44,7 @@ func initUpdateIndexCommand() *cobra.Command {
} }
func runUpdateIndexCommand(cmd *cobra.Command, args []string) { func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
logrus.Info("Executing `arduino core update-index`") logrus.Info("Executing `arduino core update-index`")
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{ _, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{
......
...@@ -48,12 +48,17 @@ func initUpgradeCommand() *cobra.Command { ...@@ -48,12 +48,17 @@ func initUpgradeCommand() *cobra.Command {
} }
func runUpgradeCommand(cmd *cobra.Command, args []string) { func runUpgradeCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance() inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error upgrading: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Info("Executing `arduino core upgrade`") logrus.Info("Executing `arduino core upgrade`")
// if no platform was passed, upgrade allthethings // if no platform was passed, upgrade allthethings
if len(args) == 0 { if len(args) == 0 {
targets, err := core.GetPlatforms(instance.Id, true) targets, err := core.GetPlatforms(inst.Id, true)
if err != nil { if err != nil {
feedback.Errorf("Error retrieving core list: %v", err) feedback.Errorf("Error retrieving core list: %v", err)
os.Exit(errorcodes.ErrGeneric) os.Exit(errorcodes.ErrGeneric)
...@@ -85,7 +90,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) { ...@@ -85,7 +90,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
} }
r := &rpc.PlatformUpgradeReq{ r := &rpc.PlatformUpgradeReq{
Instance: instance, Instance: inst,
PlatformPackage: platformRef.PackageName, PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture, Architecture: platformRef.Architecture,
} }
......
// This file is part of arduino-cli.
//
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package instance package instance
import ( import (
"context" "context"
"os"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/globals" "github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/cli/output" "github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands" rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/viper"
) )
// CreateInstaceIgnorePlatformIndexErrors creates and return an instance of the // CreateInstanceIgnorePlatformIndexErrors creates and return an instance of the
// Arduino Core Engine, but won't stop on platforms index loading errors. // Arduino Core Engine, but won't stop on platforms index loading errors.
func CreateInstaceIgnorePlatformIndexErrors() *rpc.Instance { func CreateInstanceIgnorePlatformIndexErrors() *rpc.Instance {
return initInstance().GetInstance() i, _ := getInitResponse()
return i.GetInstance()
} }
// CreateInstance creates and return an instance of the Arduino Core engine // CreateInstance creates and return an instance of the Arduino Core engine
func CreateInstance() *rpc.Instance { func CreateInstance() (*rpc.Instance, error) {
resp := initInstance() resp, err := getInitResponse()
if resp.GetPlatformsIndexErrors() != nil { if err != nil {
for _, err := range resp.GetPlatformsIndexErrors() { return nil, err
feedback.Errorf("Error loading index: %v", err)
}
feedback.Errorf("Launch '%s core update-index' to fix or download indexes.", os.Args[0])
os.Exit(errorcodes.ErrGeneric)
} }
return resp.GetInstance()
return resp.GetInstance(), checkPlatformErrors(resp)
} }
func initInstance() *rpc.InitResp { func getInitResponse() (*rpc.InitResp, error) {
logrus.Info("Initializing package manager") // invoke Init()
req := packageManagerInitReq() resp, err := commands.Init(context.Background(), &rpc.InitReq{},
output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
resp, err := commands.Init(context.Background(), req, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader()) // Init() failed
if err != nil { if err != nil {
feedback.Errorf("Error initializing package manager: %v", err) return nil, errors.Wrap(err, "creating instance")
os.Exit(errorcodes.ErrGeneric)
} }
// Init() succeeded but there were errors loading library indexes,
// let's rescan and try again
if resp.GetLibrariesIndexError() != "" { if resp.GetLibrariesIndexError() != "" {
commands.UpdateLibrariesIndex(context.Background(), logrus.Warnf("There were errors loading the library index, trying again...")
// update all indexes
err := commands.UpdateLibrariesIndex(context.Background(),
&rpc.UpdateLibrariesIndexReq{Instance: resp.GetInstance()}, output.ProgressBar()) &rpc.UpdateLibrariesIndexReq{Instance: resp.GetInstance()}, output.ProgressBar())
rescResp, err := commands.Rescan(resp.GetInstance().GetId()) if err != nil {
if rescResp.GetLibrariesIndexError() != "" { return nil, errors.Wrap(err, "updating the library index")
feedback.Errorf("Error loading library index: %v", rescResp.GetLibrariesIndexError())
os.Exit(errorcodes.ErrGeneric)
} }
// rescan libraries
rescanResp, err := commands.Rescan(resp.GetInstance().GetId())
if err != nil { if err != nil {
feedback.Errorf("Error loading library index: %v", err) return nil, errors.Wrap(err, "during rescan")
os.Exit(errorcodes.ErrGeneric)
} }
resp.LibrariesIndexError = rescResp.LibrariesIndexError
resp.PlatformsIndexErrors = rescResp.PlatformsIndexErrors
}
return resp
}
func packageManagerInitReq() *rpc.InitReq { // errors persist
urls := []string{globals.DefaultIndexURL} if rescanResp.GetLibrariesIndexError() != "" {
return nil, errors.New("still errors after rescan: " + rescanResp.GetLibrariesIndexError())
}
for _, URL := range viper.GetStringSlice("board_manager.additional_urls") { // succeeded, copy over PlatformsIndexErrors in case errors occurred
urls = append(urls, URL) // during rescan
resp.LibrariesIndexError = ""
resp.PlatformsIndexErrors = rescanResp.PlatformsIndexErrors
} }
conf := &rpc.Configuration{} return resp, nil
conf.DataDir = viper.GetString("directories.Data") }
conf.DownloadsDir = viper.GetString("directories.Downloads")
conf.BoardManagerAdditionalUrls = urls func checkPlatformErrors(resp *rpc.InitResp) error {
conf.SketchbookDir = viper.GetString("directories.User") // Init() and/or rescan succeeded, but there were errors loading platform indexes
if resp.GetPlatformsIndexErrors() != nil {
// log each error
for _, err := range resp.GetPlatformsIndexErrors() {
logrus.Errorf("Error loading platform index: %v", err)
}
// return
return errors.New("There were errors loading platform indexes")
}
return &rpc.InitReq{Configuration: conf} return nil
} }
...@@ -53,7 +53,7 @@ func runDepsCommand(cmd *cobra.Command, args []string) { ...@@ -53,7 +53,7 @@ func runDepsCommand(cmd *cobra.Command, args []string) {
os.Exit(errorcodes.ErrBadArgument) os.Exit(errorcodes.ErrBadArgument)
} }
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
deps, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesReq{ deps, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesReq{
Instance: instance, Instance: instance,
Name: libRef.Name, Name: libRef.Name,
......
...@@ -46,7 +46,7 @@ func initDownloadCommand() *cobra.Command { ...@@ -46,7 +46,7 @@ func initDownloadCommand() *cobra.Command {
} }
func runDownloadCommand(cmd *cobra.Command, args []string) { func runDownloadCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
refs, err := globals.ParseLibraryReferenceArgs(args) refs, err := globals.ParseLibraryReferenceArgs(args)
if err != nil { if err != nil {
feedback.Errorf("Invalid argument passed: %v", err) feedback.Errorf("Invalid argument passed: %v", err)
......
...@@ -51,7 +51,7 @@ var installFlags struct { ...@@ -51,7 +51,7 @@ var installFlags struct {
} }
func runInstallCommand(cmd *cobra.Command, args []string) { func runInstallCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
libRefs, err := globals.ParseLibraryReferenceArgs(args) libRefs, err := globals.ParseLibraryReferenceArgs(args)
if err != nil { if err != nil {
feedback.Errorf("Arguments error: %v", err) feedback.Errorf("Arguments error: %v", err)
......
...@@ -51,7 +51,7 @@ var listFlags struct { ...@@ -51,7 +51,7 @@ var listFlags struct {
} }
func runListCommand(cmd *cobra.Command, args []string) { func runListCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
logrus.Info("Listing") logrus.Info("Listing")
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{ res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{
......
...@@ -52,7 +52,7 @@ var searchFlags struct { ...@@ -52,7 +52,7 @@ var searchFlags struct {
} }
func runSearchCommand(cmd *cobra.Command, args []string) { func runSearchCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
logrus.Info("Executing `arduino lib search`") logrus.Info("Executing `arduino lib search`")
searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchReq{ searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchReq{
Instance: instance, Instance: instance,
......
...@@ -47,7 +47,7 @@ func initUninstallCommand() *cobra.Command { ...@@ -47,7 +47,7 @@ func initUninstallCommand() *cobra.Command {
func runUninstallCommand(cmd *cobra.Command, args []string) { func runUninstallCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino lib uninstall`") logrus.Info("Executing `arduino lib uninstall`")
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
refs, err := globals.ParseLibraryReferenceArgs(args) refs, err := globals.ParseLibraryReferenceArgs(args)
if err != nil { if err != nil {
feedback.Errorf("Invalid argument passed: %v", err) feedback.Errorf("Invalid argument passed: %v", err)
......
...@@ -38,7 +38,7 @@ func initUpdateIndexCommand() *cobra.Command { ...@@ -38,7 +38,7 @@ func initUpdateIndexCommand() *cobra.Command {
Example: " " + os.Args[0] + " lib update-index", Example: " " + os.Args[0] + " lib update-index",
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexReq{ err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexReq{
Instance: instance, Instance: instance,
}, output.ProgressBar()) }, output.ProgressBar())
......
...@@ -47,7 +47,7 @@ func initUpgradeCommand() *cobra.Command { ...@@ -47,7 +47,7 @@ func initUpgradeCommand() *cobra.Command {
} }
func runUpgradeCommand(cmd *cobra.Command, args []string) { func runUpgradeCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstaceIgnorePlatformIndexErrors() instance := instance.CreateInstanceIgnorePlatformIndexErrors()
if len(args) == 0 { if len(args) == 0 {
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader()) err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
......
...@@ -60,7 +60,11 @@ func NewCommand() *cobra.Command { ...@@ -60,7 +60,11 @@ func NewCommand() *cobra.Command {
} }
func run(command *cobra.Command, args []string) { func run(command *cobra.Command, args []string) {
instance := instance.CreateInstance() instance, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error during Upload: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
var path *paths.Path var path *paths.Path
if len(args) > 0 { if len(args) > 0 {
...@@ -68,7 +72,7 @@ func run(command *cobra.Command, args []string) { ...@@ -68,7 +72,7 @@ func run(command *cobra.Command, args []string) {
} }
sketchPath := initSketchPath(path) sketchPath := initSketchPath(path)
_, err := upload.Upload(context.Background(), &rpc.UploadReq{ if _, err := upload.Upload(context.Background(), &rpc.UploadReq{
Instance: instance, Instance: instance,
Fqbn: fqbn, Fqbn: fqbn,
SketchPath: sketchPath.String(), SketchPath: sketchPath.String(),
...@@ -76,9 +80,7 @@ func run(command *cobra.Command, args []string) { ...@@ -76,9 +80,7 @@ func run(command *cobra.Command, args []string) {
Verbose: verbose, Verbose: verbose,
Verify: verify, Verify: verify,
ImportFile: importFile, ImportFile: importFile,
}, os.Stdout, os.Stderr) }, os.Stdout, os.Stderr); err != nil {
if err != nil {
feedback.Errorf("Error during Upload: %v", err) feedback.Errorf("Error during Upload: %v", err)
os.Exit(errorcodes.ErrGeneric) os.Exit(errorcodes.ErrGeneric)
} }
......
...@@ -59,6 +59,13 @@ type InstanceContainer interface { ...@@ -59,6 +59,13 @@ type InstanceContainer interface {
GetInstance() *rpc.Instance GetInstance() *rpc.Instance
} }
type createInstanceResult struct {
Pm *packagemanager.PackageManager
Lm *librariesmanager.LibrariesManager
PlatformIndexErrors []string
LibrariesIndexError string
}
// GetInstance returns a CoreInstance for the given ID, or nil if ID // GetInstance returns a CoreInstance for the given ID, or nil if ID
// doesn't exist // doesn't exist
func GetInstance(id int32) *CoreInstance { func GetInstance(id int32) *CoreInstance {
...@@ -125,20 +132,19 @@ func (instance *CoreInstance) checkForBuiltinTools(downloadCB DownloadProgressCB ...@@ -125,20 +132,19 @@ func (instance *CoreInstance) checkForBuiltinTools(downloadCB DownloadProgressCB
} }
// Init FIXMEDOC // Init FIXMEDOC
func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, taskCB TaskProgressCB, downloaderHeaders http.Header) (*rpc.InitResp, error) { func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, taskCB TaskProgressCB,
inConfig := req.GetConfiguration() downloaderHeaders http.Header) (*rpc.InitResp, error) {
if inConfig == nil {
return nil, fmt.Errorf("invalid request")
}
pm, lm, reqPltIndex, reqLibIndex, err := createInstance(ctx, req.GetLibraryManagerOnly()) res, err := createInstance(ctx, req.GetLibraryManagerOnly())
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot initialize package manager: %s", err) return nil, fmt.Errorf("cannot initialize package manager: %s", err)
} }
instance := &CoreInstance{ instance := &CoreInstance{
PackageManager: pm, PackageManager: res.Pm,
lm: lm, lm: res.Lm,
getLibOnly: req.GetLibraryManagerOnly()} getLibOnly: req.GetLibraryManagerOnly(),
}
handle := instancesCount handle := instancesCount
instancesCount++ instancesCount++
instances[handle] = instance instances[handle] = instance
...@@ -150,8 +156,8 @@ func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, ...@@ -150,8 +156,8 @@ func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB,
return &rpc.InitResp{ return &rpc.InitResp{
Instance: &rpc.Instance{Id: handle}, Instance: &rpc.Instance{Id: handle},
PlatformsIndexErrors: reqPltIndex, PlatformsIndexErrors: res.PlatformIndexErrors,
LibrariesIndexError: reqLibIndex, LibrariesIndexError: res.LibrariesIndexError,
}, nil }, nil
} }
...@@ -252,88 +258,103 @@ func Rescan(instanceID int32) (*rpc.RescanResp, error) { ...@@ -252,88 +258,103 @@ func Rescan(instanceID int32) (*rpc.RescanResp, error) {
return nil, fmt.Errorf("invalid handle") return nil, fmt.Errorf("invalid handle")
} }
pm, lm, reqPltIndex, reqLibIndex, err := createInstance(context.Background(), coreInstance.getLibOnly) res, err := createInstance(context.Background(), coreInstance.getLibOnly)
if err != nil { if err != nil {
return nil, fmt.Errorf("rescanning filesystem: %s", err) return nil, fmt.Errorf("rescanning filesystem: %s", err)
} }
coreInstance.PackageManager = pm coreInstance.PackageManager = res.Pm
coreInstance.lm = lm coreInstance.lm = res.Lm
return &rpc.RescanResp{ return &rpc.RescanResp{
PlatformsIndexErrors: reqPltIndex, PlatformsIndexErrors: res.PlatformIndexErrors,
LibrariesIndexError: reqLibIndex, LibrariesIndexError: res.LibrariesIndexError,
}, nil }, nil
} }
func createInstance(ctx context.Context, getLibOnly bool) ( func createInstance(ctx context.Context, getLibOnly bool) (*createInstanceResult, error) {
*packagemanager.PackageManager, *librariesmanager.LibrariesManager, []string, string, error) { res := &createInstanceResult{}
var pm *packagemanager.PackageManager
platformIndexErrors := []string{} // setup downloads directory
dataDir := paths.New(viper.GetString("directories.Data"))
downloadsDir := paths.New(viper.GetString("directories.Downloads")) downloadsDir := paths.New(viper.GetString("directories.Downloads"))
if downloadsDir.NotExist() {
err := downloadsDir.MkdirAll()
if err != nil {
return nil, err
}
}
// setup data directory
dataDir := paths.New(viper.GetString("directories.Data"))
packagesDir := configuration.PackagesDir()
if packagesDir.NotExist() {
err := packagesDir.MkdirAll()
if err != nil {
return nil, err
}
}
if !getLibOnly { if !getLibOnly {
pm = packagemanager.NewPackageManager( res.Pm = packagemanager.NewPackageManager(dataDir, configuration.PackagesDir(),
dataDir, downloadsDir, dataDir.Join("tmp"))
configuration.PackagesDir(),
downloadsDir,
dataDir.Join("tmp"))
urls := []string{globals.DefaultIndexURL} urls := []string{globals.DefaultIndexURL}
urls = append(urls, viper.GetStringSlice("board_manager.additional_urls")...) urls = append(urls, viper.GetStringSlice("board_manager.additional_urls")...)
for _, u := range urls { for _, u := range urls {
URL, err := url.Parse(u) URL, err := url.Parse(u)
if err != nil { if err != nil {
logrus.Warnf("unable to parse additional URL: %s", u) logrus.Warnf("Unable to parse index URL: %s, skip...", u)
continue continue
} }
if err := pm.LoadPackageIndex(URL); err != nil {
platformIndexErrors = append(platformIndexErrors, err.Error()) if err := res.Pm.LoadPackageIndex(URL); err != nil {
res.PlatformIndexErrors = append(res.PlatformIndexErrors, err.Error())
} }
} }
if err := pm.LoadHardware(); err != nil { if err := res.Pm.LoadHardware(); err != nil {
return nil, nil, nil, "", fmt.Errorf("loading hardware packages: %s", err) return res, fmt.Errorf("error loading hardware packages: %s", err)
} }
} }
if len(platformIndexErrors) == 0 {
platformIndexErrors = nil if len(res.PlatformIndexErrors) == 0 {
res.PlatformIndexErrors = nil
} }
// Initialize library manager // Initialize library manager
// -------------------------- // --------------------------
lm := librariesmanager.NewLibraryManager(dataDir, downloadsDir) res.Lm = librariesmanager.NewLibraryManager(dataDir, downloadsDir)
// Add IDE builtin libraries dir // Add IDE builtin libraries dir
if bundledLibsDir := configuration.IDEBundledLibrariesDir(); bundledLibsDir != nil { if bundledLibsDir := configuration.IDEBundledLibrariesDir(); bundledLibsDir != nil {
lm.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn) res.Lm.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn)
} }
// Add user libraries dir // Add user libraries dir
libDir := configuration.LibrariesDir() libDir := configuration.LibrariesDir()
lm.AddLibrariesDir(libDir, libraries.User) res.Lm.AddLibrariesDir(libDir, libraries.User)
// Add libraries dirs from installed platforms // Add libraries dirs from installed platforms
if pm != nil { if res.Pm != nil {
for _, targetPackage := range pm.Packages { for _, targetPackage := range res.Pm.Packages {
for _, platform := range targetPackage.Platforms { for _, platform := range targetPackage.Platforms {
if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil { if platformRelease := res.Pm.GetInstalledPlatformRelease(platform); platformRelease != nil {
lm.AddPlatformReleaseLibrariesDir(platformRelease, libraries.PlatformBuiltIn) res.Lm.AddPlatformReleaseLibrariesDir(platformRelease, libraries.PlatformBuiltIn)
} }
} }
} }
} }
// Load index and auto-update it if needed // Load index and auto-update it if needed
librariesIndexError := "" if err := res.Lm.LoadIndex(); err != nil {
if err := lm.LoadIndex(); err != nil { res.LibrariesIndexError = err.Error()
librariesIndexError = err.Error()
} }
// Scan for libraries // Scan for libraries
if err := lm.RescanLibraries(); err != nil { if err := res.Lm.RescanLibraries(); err != nil {
return nil, nil, nil, "", fmt.Errorf("libraries rescan: %s", err) return res, fmt.Errorf("libraries rescan: %s", err)
} }
return pm, lm, platformIndexErrors, librariesIndexError, nil
return res, nil
} }
// Download FIXMEDOC // Download FIXMEDOC
......
...@@ -93,27 +93,17 @@ service ArduinoCore { ...@@ -93,27 +93,17 @@ service ArduinoCore {
rpc LibraryList(LibraryListReq) returns (LibraryListResp); rpc LibraryList(LibraryListReq) returns (LibraryListResp);
} }
// Configuration contains information to instantiate an Arduino Platform Service // DEPRECATED
// Configuration is deprecated, use the Settings Service instead
message Configuration { message Configuration {
// dataDir represents the current root of the arduino tree (defaulted to
// `$HOME/.arduino15` on linux).
string dataDir = 1; string dataDir = 1;
// sketchbookDir represents the current root of the sketchbooks tree
// (defaulted to `$HOME/Arduino`).
string sketchbookDir = 2; string sketchbookDir = 2;
// ArduinoIDEDirectory is the directory of the Arduino IDE if the CLI runs
// together with it.
string downloadsDir = 3; string downloadsDir = 3;
// BoardManagerAdditionalUrls contains the additional URL for 3rd party
// packages
repeated string boardManagerAdditionalUrls = 4; repeated string boardManagerAdditionalUrls = 4;
} }
message InitReq { message InitReq {
Configuration configuration = 1; Configuration configuration = 1; // DEPRECATED this field will be ignored
bool library_manager_only = 2; bool library_manager_only = 2;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment