Commit 4428a46d authored by Umberto Baldi's avatar Umberto Baldi

factor out init step for instance creation in `update`commands

parent 06e96b9a
...@@ -43,24 +43,7 @@ func initUpdateIndexCommand() *cobra.Command { ...@@ -43,24 +43,7 @@ func initUpdateIndexCommand() *cobra.Command {
func runUpdateIndexCommand(cmd *cobra.Command, args []string) { func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino core update-index`") logrus.Info("Executing `arduino core update-index`")
// We don't initialize any CoreInstance when updating indexes since we don't need to. inst := instance.CreateInstanceAndRunFirstUpdate()
// 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 := instance.Create()
if status != nil {
feedback.Errorf(tr("Error creating instance: %v"), status)
os.Exit(errorcodes.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 := instance.FirstUpdate(inst); err != nil {
feedback.Errorf(tr("Error updating indexes: %v"), status)
os.Exit(errorcodes.ErrGeneric)
}
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{ _, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
Instance: inst, Instance: inst,
......
...@@ -140,3 +140,27 @@ func FirstUpdate(instance *rpc.Instance) error { ...@@ -140,3 +140,27 @@ func FirstUpdate(instance *rpc.Instance) error {
return nil 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.Errorf(tr("Error creating instance: %v"), status)
os.Exit(errorcodes.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.Errorf(tr("Error updating indexes: %v"), status)
os.Exit(errorcodes.ErrGeneric)
}
return inst
}
...@@ -25,35 +25,25 @@ import ( ...@@ -25,35 +25,25 @@ import (
"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/cc/arduino/cli/commands/v1" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func initUpdateIndexCommand() *cobra.Command { func initUpdateIndexCommand() *cobra.Command {
return &cobra.Command{ updateIndexCommand := &cobra.Command{
Use: "update-index", Use: "update-index",
Short: tr("Updates the libraries index."), Short: tr("Updates the libraries index."),
Long: tr("Updates the libraries index to the latest version."), Long: tr("Updates the libraries index to the latest version."),
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: runUpdateIndexCommand,
// 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 := instance.Create()
if status != nil {
feedback.Errorf(tr("Error creating instance: %v"), status)
os.Exit(errorcodes.ErrGeneric)
} }
return updateIndexCommand
}
// In case this is the first time the CLI is run we need to update indexes func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
// to make it work correctly, we must do this explicitly in this command since logrus.Info("Executing `arduino lib update-index`")
// we must use instance.Create instead of instance.CreateAndInit for the inst := instance.CreateInstanceAndRunFirstUpdate()
// reason stated above.
if err := instance.FirstUpdate(inst); err != nil {
feedback.Errorf(tr("Error updating indexes: %v"), status)
os.Exit(errorcodes.ErrGeneric)
}
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{ err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
Instance: inst, Instance: inst,
...@@ -62,6 +52,4 @@ func initUpdateIndexCommand() *cobra.Command { ...@@ -62,6 +52,4 @@ func initUpdateIndexCommand() *cobra.Command {
feedback.Errorf(tr("Error updating library index: %v"), err) feedback.Errorf(tr("Error updating library index: %v"), err)
os.Exit(errorcodes.ErrGeneric) os.Exit(errorcodes.ErrGeneric)
} }
},
}
} }
...@@ -53,24 +53,7 @@ var updateFlags struct { ...@@ -53,24 +53,7 @@ var updateFlags struct {
func runUpdateCommand(cmd *cobra.Command, args []string) { func runUpdateCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino update`") logrus.Info("Executing `arduino update`")
// We don't initialize any CoreInstance when updating indexes since we don't need to. inst := instance.CreateInstanceAndRunFirstUpdate()
// 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 := instance.Create()
if status != nil {
feedback.Errorf(tr("Error creating instance: %v"), status)
os.Exit(errorcodes.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 := instance.FirstUpdate(inst); err != nil {
feedback.Errorf(tr("Error updating indexes: %v"), status)
os.Exit(errorcodes.ErrGeneric)
}
err := commands.UpdateCoreLibrariesIndex(context.Background(), &rpc.UpdateCoreLibrariesIndexRequest{ err := commands.UpdateCoreLibrariesIndex(context.Background(), &rpc.UpdateCoreLibrariesIndexRequest{
Instance: inst, Instance: inst,
......
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