Skip to content

Commit 7a2659d

Browse files
committed
factor out init step for instance creation in updatecommands
1 parent 60ff290 commit 7a2659d

File tree

5 files changed

+56
-82
lines changed

5 files changed

+56
-82
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.CreateInstanceForUpdate()
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+
// CreateInstanceForUpdate creates an instance and runs `FirstUpdate`.
145+
// This is a mandatory to run all `update-index` commands
146+
func CreateInstanceForUpdate() *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.CreateInstanceForUpdate()
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.CreateInstanceForUpdate()
7457

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

i18n/data/en.po

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ msgstr "Connected to %s! Press CTRL-C to exit."
440440
msgid "Core"
441441
msgstr "Core"
442442

443-
#: cli/update/update.go:99
443+
#: cli/update/update.go:82
444444
msgid "Core name"
445445
msgstr "Core name"
446446

@@ -639,11 +639,9 @@ msgid "Error copying output file %s"
639639
msgstr "Error copying output file %s"
640640

641641
#: cli/core/search.go:66
642-
#: cli/core/update_index.go:52
643642
#: cli/instance/instance.go:42
643+
#: cli/instance/instance.go:153
644644
#: cli/lib/search.go:57
645-
#: cli/lib/update_index.go:45
646-
#: cli/update/update.go:62
647645
msgid "Error creating instance: %v"
648646
msgstr "Error creating instance: %v"
649647

@@ -800,7 +798,7 @@ msgstr "Error in FQBN: %s"
800798
#: cli/core/search.go:81
801799
#: cli/instance/instance.go:46
802800
#: cli/lib/search.go:71
803-
#: cli/update/update.go:87
801+
#: cli/update/update.go:70
804802
msgid "Error initializing instance: %v"
805803
msgstr "Error initializing instance: %v"
806804

@@ -865,7 +863,7 @@ msgid "Error retrieving core list: %v"
865863
msgstr "Error retrieving core list: %v"
866864

867865
#: cli/outdated/outdated.go:57
868-
#: cli/update/update.go:94
866+
#: cli/update/update.go:77
869867
msgid "Error retrieving outdated cores and libraries: %v"
870868
msgstr "Error retrieving outdated cores and libraries: %v"
871869

@@ -920,23 +918,21 @@ msgstr "Error uninstalling platform %s"
920918
msgid "Error uninstalling tool %s"
921919
msgstr "Error uninstalling tool %s"
922920

923-
#: cli/update/update.go:79
921+
#: cli/update/update.go:62
924922
msgid "Error updating core and libraries index: %v"
925923
msgstr "Error updating core and libraries index: %v"
926924

927925
#: cli/core/search.go:75
928-
#: cli/core/update_index.go:69
926+
#: cli/core/update_index.go:52
929927
msgid "Error updating index: %v"
930928
msgstr "Error updating index: %v"
931929

932-
#: cli/core/update_index.go:61
933-
#: cli/lib/update_index.go:54
934-
#: cli/update/update.go:71
930+
#: cli/instance/instance.go:162
935931
msgid "Error updating indexes: %v"
936932
msgstr "Error updating indexes: %v"
937933

938934
#: cli/lib/search.go:66
939-
#: cli/lib/update_index.go:62
935+
#: cli/lib/update_index.go:52
940936
msgid "Error updating library index: %v"
941937
msgstr "Error updating library index: %v"
942938

@@ -1176,8 +1172,8 @@ msgstr "Installed %s"
11761172

11771173
#: cli/outdated/outdated.go:62
11781174
#: cli/outdated/outdated.go:72
1179-
#: cli/update/update.go:99
1180-
#: cli/update/update.go:109
1175+
#: cli/update/update.go:82
1176+
#: cli/update/update.go:92
11811177
msgid "Installed version"
11821178
msgstr "Installed version"
11831179

@@ -1349,7 +1345,7 @@ msgid "Library installed"
13491345
msgstr "Library installed"
13501346

13511347
#: cli/outdated/outdated.go:72
1352-
#: cli/update/update.go:109
1348+
#: cli/update/update.go:92
13531349
msgid "Library name"
13541350
msgstr "Library name"
13551351

@@ -1497,8 +1493,8 @@ msgstr "Name: \"%s\""
14971493

14981494
#: cli/outdated/outdated.go:62
14991495
#: cli/outdated/outdated.go:72
1500-
#: cli/update/update.go:99
1501-
#: cli/update/update.go:109
1496+
#: cli/update/update.go:82
1497+
#: cli/update/update.go:92
15021498
msgid "New version"
15031499
msgstr "New version"
15041500

@@ -2178,11 +2174,11 @@ msgstr "Updates the index of cores to the latest version."
21782174
msgid "Updates the index of cores."
21792175
msgstr "Updates the index of cores."
21802176

2181-
#: cli/lib/update_index.go:35
2177+
#: cli/lib/update_index.go:36
21822178
msgid "Updates the libraries index to the latest version."
21832179
msgstr "Updates the libraries index to the latest version."
21842180

2185-
#: cli/lib/update_index.go:34
2181+
#: cli/lib/update_index.go:35
21862182
msgid "Updates the libraries index."
21872183
msgstr "Updates the libraries index."
21882184

0 commit comments

Comments
 (0)