Skip to content

Commit 4428a46

Browse files
committed
factor out init step for instance creation in updatecommands
1 parent 06e96b9 commit 4428a46

File tree

4 files changed

+41
-63
lines changed

4 files changed

+41
-63
lines changed

cli/core/update_index.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,7 @@ func initUpdateIndexCommand() *cobra.Command {
4343

4444
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
4545
logrus.Info("Executing `arduino core update-index`")
46-
// We don't initialize any CoreInstance when updating indexes since we don't need to.
47-
// Also meaningless errors might be returned when calling this command with --additional-urls
48-
// since the CLI would be searching for a corresponding file for the additional urls set
49-
// as argument but none would be obviously found.
50-
inst, status := instance.Create()
51-
if status != nil {
52-
feedback.Errorf(tr("Error creating instance: %v"), status)
53-
os.Exit(errorcodes.ErrGeneric)
54-
}
55-
56-
// In case this is the first time the CLI is run we need to update indexes
57-
// to make it work correctly, we must do this explicitly in this command since
58-
// we must use instance.Create instead of instance.CreateAndInit for the
59-
// reason stated above.
60-
if err := instance.FirstUpdate(inst); err != nil {
61-
feedback.Errorf(tr("Error updating indexes: %v"), status)
62-
os.Exit(errorcodes.ErrGeneric)
63-
}
46+
inst := instance.CreateInstanceAndRunFirstUpdate()
6447

6548
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
6649
Instance: inst,

cli/instance/instance.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,27 @@ func FirstUpdate(instance *rpc.Instance) error {
140140

141141
return nil
142142
}
143+
144+
// CreateInstanceAndRunFirstUpdate creates an instance and runs `FirstUpdate`.
145+
// It is mandatory for all `update-index` commands to call this
146+
func CreateInstanceAndRunFirstUpdate() *rpc.Instance {
147+
// We don't initialize any CoreInstance when updating indexes since we don't need to.
148+
// Also meaningless errors might be returned when calling this command with --additional-urls
149+
// since the CLI would be searching for a corresponding file for the additional urls set
150+
// as argument but none would be obviously found.
151+
inst, status := Create()
152+
if status != nil {
153+
feedback.Errorf(tr("Error creating instance: %v"), status)
154+
os.Exit(errorcodes.ErrGeneric)
155+
}
156+
157+
// In case this is the first time the CLI is run we need to update indexes
158+
// to make it work correctly, we must do this explicitly in this command since
159+
// we must use instance.Create instead of instance.CreateAndInit for the
160+
// reason stated above.
161+
if err := FirstUpdate(inst); err != nil {
162+
feedback.Errorf(tr("Error updating indexes: %v"), status)
163+
os.Exit(errorcodes.ErrGeneric)
164+
}
165+
return inst
166+
}

cli/lib/update_index.go

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,31 @@ import (
2525
"github.com/arduino/arduino-cli/cli/output"
2626
"github.com/arduino/arduino-cli/commands"
2727
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
28+
"github.com/sirupsen/logrus"
2829
"github.com/spf13/cobra"
2930
)
3031

3132
func initUpdateIndexCommand() *cobra.Command {
32-
return &cobra.Command{
33+
updateIndexCommand := &cobra.Command{
3334
Use: "update-index",
3435
Short: tr("Updates the libraries index."),
3536
Long: tr("Updates the libraries index to the latest version."),
3637
Example: " " + os.Args[0] + " lib update-index",
3738
Args: cobra.NoArgs,
38-
Run: func(cmd *cobra.Command, args []string) {
39-
// We don't initialize any CoreInstance when updating indexes since we don't need to.
40-
// Also meaningless errors might be returned when calling this command with --additional-urls
41-
// since the CLI would be searching for a corresponding file for the additional urls set
42-
// as argument but none would be obviously found.
43-
inst, status := instance.Create()
44-
if status != nil {
45-
feedback.Errorf(tr("Error creating instance: %v"), status)
46-
os.Exit(errorcodes.ErrGeneric)
47-
}
39+
Run: runUpdateIndexCommand,
40+
}
41+
return updateIndexCommand
42+
}
4843

49-
// In case this is the first time the CLI is run we need to update indexes
50-
// to make it work correctly, we must do this explicitly in this command since
51-
// we must use instance.Create instead of instance.CreateAndInit for the
52-
// reason stated above.
53-
if err := instance.FirstUpdate(inst); err != nil {
54-
feedback.Errorf(tr("Error updating indexes: %v"), status)
55-
os.Exit(errorcodes.ErrGeneric)
56-
}
44+
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
45+
logrus.Info("Executing `arduino lib update-index`")
46+
inst := instance.CreateInstanceAndRunFirstUpdate()
5747

58-
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
59-
Instance: inst,
60-
}, output.ProgressBar())
61-
if err != nil {
62-
feedback.Errorf(tr("Error updating library index: %v"), err)
63-
os.Exit(errorcodes.ErrGeneric)
64-
}
65-
},
48+
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
49+
Instance: inst,
50+
}, output.ProgressBar())
51+
if err != nil {
52+
feedback.Errorf(tr("Error updating library index: %v"), err)
53+
os.Exit(errorcodes.ErrGeneric)
6654
}
6755
}

cli/update/update.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,7 @@ var updateFlags struct {
5353

5454
func runUpdateCommand(cmd *cobra.Command, args []string) {
5555
logrus.Info("Executing `arduino update`")
56-
// We don't initialize any CoreInstance when updating indexes since we don't need to.
57-
// Also meaningless errors might be returned when calling this command with --additional-urls
58-
// since the CLI would be searching for a corresponding file for the additional urls set
59-
// as argument but none would be obviously found.
60-
inst, status := instance.Create()
61-
if status != nil {
62-
feedback.Errorf(tr("Error creating instance: %v"), status)
63-
os.Exit(errorcodes.ErrGeneric)
64-
}
65-
66-
// In case this is the first time the CLI is run we need to update indexes
67-
// to make it work correctly, we must do this explicitly in this command since
68-
// we must use instance.Create instead of instance.CreateAndInit for the
69-
// reason stated above.
70-
if err := instance.FirstUpdate(inst); err != nil {
71-
feedback.Errorf(tr("Error updating indexes: %v"), status)
72-
os.Exit(errorcodes.ErrGeneric)
73-
}
56+
inst := instance.CreateInstanceAndRunFirstUpdate()
7457

7558
err := commands.UpdateCoreLibrariesIndex(context.Background(), &rpc.UpdateCoreLibrariesIndexRequest{
7659
Instance: inst,

0 commit comments

Comments
 (0)