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 {
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino core update-index`")
// 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)
}
// 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)
}
inst := instance.CreateInstanceAndRunFirstUpdate()
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
Instance: inst,
......
......@@ -140,3 +140,27 @@ func FirstUpdate(instance *rpc.Instance) error {
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 (
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func initUpdateIndexCommand() *cobra.Command {
return &cobra.Command{
updateIndexCommand := &cobra.Command{
Use: "update-index",
Short: tr("Updates the libraries index."),
Long: tr("Updates the libraries index to the latest version."),
Example: " " + os.Args[0] + " lib update-index",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// 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)
Run: runUpdateIndexCommand,
}
return updateIndexCommand
}
// 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)
}
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino lib update-index`")
inst := instance.CreateInstanceAndRunFirstUpdate()
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
Instance: inst,
......@@ -62,6 +52,4 @@ func initUpdateIndexCommand() *cobra.Command {
feedback.Errorf(tr("Error updating library index: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
},
}
}
......@@ -53,24 +53,7 @@ var updateFlags struct {
func runUpdateCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino update`")
// 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)
}
// 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)
}
inst := instance.CreateInstanceAndRunFirstUpdate()
err := commands.UpdateCoreLibrariesIndex(context.Background(), &rpc.UpdateCoreLibrariesIndexRequest{
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