Skip to content

Fix update index on fresh installs #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ var attachFlags struct {
}

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
if len(args) > 1 {
Expand All @@ -62,13 +66,12 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
path = initSketchPath(path)
}

_, err := board.Attach(context.Background(), &rpc.BoardAttachReq{
if _, err = board.Attach(context.Background(), &rpc.BoardAttachReq{
Instance: instance,
BoardUri: args[0],
SketchPath: path.String(),
SearchTimeout: attachFlags.searchTimeout,
}, output.TaskProgress())
if err != nil {
}, output.TaskProgress()); err != nil {
feedback.Errorf("Attach board error: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
Expand Down
8 changes: 7 additions & 1 deletion cli/board/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ var detailsCommand = &cobra.Command{
}

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{
Instance: instance.CreateInstance(),
Instance: inst,
Fqbn: args[0],
})

Expand Down
8 changes: 7 additions & 1 deletion cli/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ func runListCommand(cmd *cobra.Command, args []string) {
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 {
feedback.Errorf("Error detecting boards: %v", err)
os.Exit(errorcodes.ErrNetwork)
Expand Down
8 changes: 6 additions & 2 deletions cli/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ var listAllCommand = &cobra.Command{

// runListAllCommand list all installed boards
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{
Instance: instance,
Instance: inst,
SearchArgs: args,
})
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ func NewCommand() *cobra.Command {
}

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
if len(args) > 0 {
Expand All @@ -95,8 +99,8 @@ func run(cmd *cobra.Command, args []string) {

sketchPath := initSketchPath(path)

_, err := compile.Compile(context.Background(), &rpc.CompileReq{
Instance: instance,
_, err = compile.Compile(context.Background(), &rpc.CompileReq{
Instance: inst,
Fqbn: fqbn,
SketchPath: sketchPath.String(),
ShowProperties: showProperties,
Expand All @@ -118,7 +122,7 @@ func run(cmd *cobra.Command, args []string) {

if uploadAfterCompile {
_, err := upload.Upload(context.Background(), &rpc.UploadReq{
Instance: instance,
Instance: inst,
Fqbn: fqbn,
SketchPath: sketchPath.String(),
Port: port,
Expand Down
9 changes: 7 additions & 2 deletions cli/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ func initDownloadCommand() *cobra.Command {
}

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`")

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

for i, platformRef := range platformsRefs {
platformDownloadreq := &rpc.PlatformDownloadReq{
Instance: instance,
Instance: inst,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
Version: platformRef.Version,
Expand Down
9 changes: 7 additions & 2 deletions cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func initInstallCommand() *cobra.Command {
}

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`")

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

for _, platformRef := range platformsRefs {
plattformInstallReq := &rpc.PlatformInstallReq{
Instance: instance,
Instance: inst,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
Version: platformRef.Version,
Expand Down
9 changes: 7 additions & 2 deletions cli/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ var listFlags struct {
}

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`")

platforms, err := core.GetPlatforms(instance.Id, listFlags.updatableOnly)
platforms, err := core.GetPlatforms(inst.Id, listFlags.updatableOnly)
if err != nil {
feedback.Errorf("Error listing platforms: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
9 changes: 7 additions & 2 deletions cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ func initSearchCommand() *cobra.Command {
}

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`")

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 {
feedback.Errorf("Error saerching for platforms: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
9 changes: 7 additions & 2 deletions cli/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ func initUninstallCommand() *cobra.Command {
}

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`")

platformsRefs, err := globals.ParseReferenceArgs(args, true)
Expand All @@ -61,7 +66,7 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
}
for _, platformRef := range platformsRefs {
_, err := core.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
Instance: instance,
Instance: inst,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
}, output.NewTaskProgressCB())
Expand Down
2 changes: 1 addition & 1 deletion cli/core/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func initUpdateIndexCommand() *cobra.Command {
}

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

_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{
Expand Down
11 changes: 8 additions & 3 deletions cli/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ func initUpgradeCommand() *cobra.Command {
}

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`")

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

r := &rpc.PlatformUpgradeReq{
Instance: instance,
Instance: inst,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
}
Expand Down
111 changes: 67 additions & 44 deletions cli/instance/instance.go
Original file line number Diff line number Diff line change
@@ -1,77 +1,100 @@
// 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 [email protected].

package instance

import (
"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/output"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/pkg/errors"
"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.
func CreateInstaceIgnorePlatformIndexErrors() *rpc.Instance {
return initInstance().GetInstance()
func CreateInstanceIgnorePlatformIndexErrors() *rpc.Instance {
i, _ := getInitResponse()
return i.GetInstance()
}

// CreateInstance creates and return an instance of the Arduino Core engine
func CreateInstance() *rpc.Instance {
resp := initInstance()
if resp.GetPlatformsIndexErrors() != nil {
for _, err := range resp.GetPlatformsIndexErrors() {
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)
func CreateInstance() (*rpc.Instance, error) {
resp, err := getInitResponse()
if err != nil {
return nil, err
}
return resp.GetInstance()

return resp.GetInstance(), checkPlatformErrors(resp)
}

func initInstance() *rpc.InitResp {
logrus.Info("Initializing package manager")
req := packageManagerInitReq()
func getInitResponse() (*rpc.InitResp, error) {
// invoke Init()
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 {
feedback.Errorf("Error initializing package manager: %v", err)
os.Exit(errorcodes.ErrGeneric)
return nil, errors.Wrap(err, "creating instance")
}

// Init() succeeded but there were errors loading library indexes,
// let's rescan and try again
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())
rescResp, err := commands.Rescan(resp.GetInstance().GetId())
if rescResp.GetLibrariesIndexError() != "" {
feedback.Errorf("Error loading library index: %v", rescResp.GetLibrariesIndexError())
os.Exit(errorcodes.ErrGeneric)
if err != nil {
return nil, errors.Wrap(err, "updating the library index")
}

// rescan libraries
rescanResp, err := commands.Rescan(resp.GetInstance().GetId())
if err != nil {
feedback.Errorf("Error loading library index: %v", err)
os.Exit(errorcodes.ErrGeneric)
return nil, errors.Wrap(err, "during rescan")
}
resp.LibrariesIndexError = rescResp.LibrariesIndexError
resp.PlatformsIndexErrors = rescResp.PlatformsIndexErrors
}
return resp
}

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

for _, URL := range viper.GetStringSlice("board_manager.additional_urls") {
urls = append(urls, URL)
// succeeded, copy over PlatformsIndexErrors in case errors occurred
// during rescan
resp.LibrariesIndexError = ""
resp.PlatformsIndexErrors = rescanResp.PlatformsIndexErrors
}

conf := &rpc.Configuration{}
conf.DataDir = viper.GetString("directories.Data")
conf.DownloadsDir = viper.GetString("directories.Downloads")
conf.BoardManagerAdditionalUrls = urls
conf.SketchbookDir = viper.GetString("directories.User")
return resp, nil
}

func checkPlatformErrors(resp *rpc.InitResp) error {
// 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
}
Loading