Skip to content

Automatically download indexes, if missing, in gRPC Init call #2119

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 6 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Perform first-update automatically in gRPC Init
  • Loading branch information
cmaglie committed Jun 16, 2023
commit b581464b5a25db3dde1cfd2cd200449627897c14
33 changes: 33 additions & 0 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
})
}

// Perform first-update of indexes if needed
firstUpdate(context.Background(), req.GetInstance(), downloadCallback)

// Try to extract profile if specified
var profile *sketch.Profile
if req.GetProfile() != "" {
Expand Down Expand Up @@ -595,3 +598,33 @@ func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketc
RootFolderFiles: rootFolderFiles,
}, nil
}

// firstUpdate downloads libraries and packages indexes if they don't exist.
// This ideally is only executed the first time the CLI is run.
func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(msg *rpc.DownloadProgress)) error {
// Gets the data directory to verify if library_index.json and package_index.json exist
dataDir := configuration.DataDir(configuration.Settings)
libraryIndex := dataDir.Join("library_index.json")
packageIndex := dataDir.Join("package_index.json")

if libraryIndex.NotExist() {
// The library_index.json file doesn't exists, that means the CLI is run for the first time
// so we proceed with the first update that downloads the file
req := &rpc.UpdateLibrariesIndexRequest{Instance: instance}
if err := UpdateLibrariesIndex(ctx, req, downloadCb); err != nil {
return err
}
}

if packageIndex.NotExist() {
// The package_index.json file doesn't exists, that means the CLI is run for the first time,
// similarly to the library update we download that file and all the other package indexes
// from additional_urls
req := &rpc.UpdateIndexRequest{Instance: instance, IgnoreCustomPackageIndexes: true}
if err := UpdateIndex(ctx, req, downloadCb); err != nil {
return err
}
}

return nil
}
2 changes: 1 addition & 1 deletion internal/cli/core/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func initUpdateIndexCommand() *cobra.Command {
}

func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
inst := instance.CreateInstanceAndRunFirstUpdate()
inst := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli core update-index`")
UpdateIndex(inst)
}
Expand Down
76 changes: 0 additions & 76 deletions internal/cli/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
package instance

import (
"context"

"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/configuration"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/internal/cli/feedback"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand Down Expand Up @@ -71,12 +68,6 @@ func Init(instance *rpc.Instance) {
// In case of loading failures return a list of errors for each platform or library that we failed to load.
// Required Package and library indexes files are automatically downloaded.
func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *paths.Path) *rpc.Profile {
// In case the CLI is executed for the first time
if err := FirstUpdate(instance); err != nil {
feedback.Warning(tr("Error initializing instance: %v", err))
return nil
}

downloadCallback := feedback.ProgressBar()
taskCallback := feedback.TaskProgress()

Expand Down Expand Up @@ -110,70 +101,3 @@ func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *pat

return profile
}

// FirstUpdate downloads libraries and packages indexes if they don't exist.
// This ideally is only executed the first time the CLI is run.
func FirstUpdate(instance *rpc.Instance) error {
// Gets the data directory to verify if library_index.json and package_index.json exist
dataDir := configuration.DataDir(configuration.Settings)

libraryIndex := dataDir.Join("library_index.json")
packageIndex := dataDir.Join("package_index.json")

if libraryIndex.Exist() && packageIndex.Exist() {
return nil
}

// The library_index.json file doesn't exists, that means the CLI is run for the first time
// so we proceed with the first update that downloads the file
if libraryIndex.NotExist() {
err := commands.UpdateLibrariesIndex(context.Background(),
&rpc.UpdateLibrariesIndexRequest{
Instance: instance,
},
feedback.ProgressBar(),
)
if err != nil {
return err
}
}

// The package_index.json file doesn't exists, that means the CLI is run for the first time,
// similarly to the library update we download that file and all the other package indexes
// from additional_urls
if packageIndex.NotExist() {
err := commands.UpdateIndex(context.Background(),
&rpc.UpdateIndexRequest{
Instance: instance,
IgnoreCustomPackageIndexes: true,
},
feedback.ProgressBar())
if err != nil {
return err
}
}

return nil
}

// CreateInstanceAndRunFirstUpdate creates an instance and runs `FirstUpdate`.
// It is mandatory for all `update-index` commands to call this
func CreateInstanceAndRunFirstUpdate() *rpc.Instance {
// We don't initialize any CoreInstance when updating indexes since we don't need to.
// Also meaningless errors might be returned when calling this command with --additional-urls
// since the CLI would be searching for a corresponding file for the additional urls set
// as argument but none would be obviously found.
inst, status := Create()
if status != nil {
feedback.Fatal(tr("Error creating instance: %v", status), feedback.ErrGeneric)
}

// In case this is the first time the CLI is run we need to update indexes
// to make it work correctly, we must do this explicitly in this command since
// we must use instance.Create instead of instance.CreateAndInit for the
// reason stated above.
if err := FirstUpdate(inst); err != nil {
feedback.Fatal(tr("Error updating indexes: %v", err), feedback.ErrGeneric)
}
return inst
}
2 changes: 1 addition & 1 deletion internal/cli/lib/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func initUpdateIndexCommand() *cobra.Command {
}

func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
inst := instance.CreateInstanceAndRunFirstUpdate()
inst := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli lib update-index`")
UpdateIndex(inst)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewCommand() *cobra.Command {
}

func runUpdateCommand(showOutdated bool) {
inst := instance.CreateInstanceAndRunFirstUpdate()
inst := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli update`")
lib.UpdateIndex(inst)
core.UpdateIndex(inst)
Expand Down
3 changes: 0 additions & 3 deletions internal/integrationtest/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ func createEnvForDaemon(t *testing.T) (*integrationtest.Environment, *integratio
UseSharedStagingFolder: true,
})

_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

_ = cli.StartDaemon(false)
return env, cli
}
Expand Down