Skip to content

Commit 6633d28

Browse files
committed
Change update indexes commands to not reload instance after
1 parent b7c77fe commit 6633d28

File tree

7 files changed

+59
-48
lines changed

7 files changed

+59
-48
lines changed

cli/core/search.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ func initSearchCommand() *cobra.Command {
6161
const indexUpdateInterval = "24h"
6262

6363
func runSearchCommand(cmd *cobra.Command, args []string) {
64-
inst := instance.CreateAndInit()
64+
inst, status := instance.Create()
65+
if status != nil {
66+
feedback.Errorf("Error creating instance: %v", status)
67+
os.Exit(errorcodes.ErrGeneric)
68+
}
6569

6670
if indexesNeedUpdating(indexUpdateInterval) {
6771
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
@@ -73,6 +77,10 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
7377
}
7478
}
7579

80+
for _, err := range instance.Init(inst) {
81+
feedback.Errorf("Error initializing instance: %v", err)
82+
}
83+
7684
arguments := strings.ToLower(strings.Join(args, " "))
7785
logrus.Infof("Executing `arduino core search` with args: '%s'", arguments)
7886

cli/core/update_index.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,23 @@ func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
4747
// Also meaningless errors might be returned when calling this command with --additional-urls
4848
// since the CLI would be searching for a corresponding file for the additional urls set
4949
// as argument but none would be obviously found.
50-
instance, status := instance.Create()
50+
inst, status := instance.Create()
5151
if status != nil {
5252
feedback.Errorf("Error creating instance: %v", status)
5353
os.Exit(errorcodes.ErrGeneric)
5454
}
5555

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("Error updating indexes: %v", status)
62+
os.Exit(errorcodes.ErrGeneric)
63+
}
64+
5665
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
57-
Instance: instance,
66+
Instance: inst,
5867
}, output.ProgressBar())
5968
if err != nil {
6069
feedback.Errorf("Error updating index: %v", err)

cli/instance/instance.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Init(instance *rpc.Instance) []*status.Status {
6464
errs := []*status.Status{}
6565

6666
// In case the CLI is executed for the first time
67-
if err := firstUpdate(instance); err != nil {
67+
if err := FirstUpdate(instance); err != nil {
6868
return append(errs, err)
6969
}
7070

@@ -96,9 +96,9 @@ func Init(instance *rpc.Instance) []*status.Status {
9696
return errs
9797
}
9898

99-
// firstUpdate downloads libraries and packages indexes if they don't exist.
99+
// FirstUpdate downloads libraries and packages indexes if they don't exist.
100100
// This ideally is only executed the first time the CLI is run.
101-
func firstUpdate(instance *rpc.Instance) *status.Status {
101+
func FirstUpdate(instance *rpc.Instance) *status.Status {
102102
// Gets the data directory to verify if library_index.json and package_index.json exist
103103
dataDir := paths.New(configuration.Settings.GetString("directories.data"))
104104

cli/lib/search.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,27 @@ var searchFlags struct {
5252
}
5353

5454
func runSearchCommand(cmd *cobra.Command, args []string) {
55-
instance := instance.CreateAndInit()
55+
inst, status := instance.Create()
56+
if status != nil {
57+
feedback.Errorf("Error creating instance: %v", status)
58+
os.Exit(errorcodes.ErrGeneric)
59+
}
5660

5761
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
58-
Instance: instance,
62+
Instance: inst,
5963
}, output.ProgressBar())
6064
if err != nil {
6165
feedback.Errorf("Error updating library index: %v", err)
6266
os.Exit(errorcodes.ErrGeneric)
6367
}
6468

69+
for _, err := range instance.Init(inst) {
70+
feedback.Errorf("Error initializing instance: %v", err)
71+
}
72+
6573
logrus.Info("Executing `arduino lib search`")
6674
searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
67-
Instance: instance,
75+
Instance: inst,
6876
Query: (strings.Join(args, " ")),
6977
})
7078
if err != nil {

cli/lib/update_index.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,23 @@ func initUpdateIndexCommand() *cobra.Command {
4040
// Also meaningless errors might be returned when calling this command with --additional-urls
4141
// since the CLI would be searching for a corresponding file for the additional urls set
4242
// as argument but none would be obviously found.
43-
instance, status := instance.Create()
43+
inst, status := instance.Create()
4444
if status != nil {
4545
feedback.Errorf("Error creating instance: %v", status)
4646
os.Exit(errorcodes.ErrGeneric)
4747
}
48+
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("Error updating indexes: %v", status)
55+
os.Exit(errorcodes.ErrGeneric)
56+
}
57+
4858
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
49-
Instance: instance,
59+
Instance: inst,
5060
}, output.ProgressBar())
5161
if err != nil {
5262
feedback.Errorf("Error updating library index: %v", err)

cli/update/update.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,28 @@ var updateFlags struct {
4949
}
5050

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

6372
err := commands.UpdateCoreLibrariesIndex(context.Background(), &rpc.UpdateCoreLibrariesIndexRequest{
64-
Instance: instance,
73+
Instance: inst,
6574
}, output.ProgressBar())
6675
if err != nil {
6776
feedback.Errorf("Error updating core and libraries index: %v", err)
@@ -70,7 +79,7 @@ func runUpdateCommand(cmd *cobra.Command, args []string) {
7079

7180
if updateFlags.showOutdated {
7281
outdatedResp, err := commands.Outdated(context.Background(), &rpc.OutdatedRequest{
73-
Instance: instance,
82+
Instance: inst,
7483
})
7584
if err != nil {
7685
feedback.Errorf("Error retrieving outdated cores and libraries: %v", err)

commands/instances.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -331,22 +331,6 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
331331
if d.Error() != nil {
332332
return d.Error()
333333
}
334-
335-
// Reinitializes an existing instance
336-
initChan, status := Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: req.Instance.Id}})
337-
if status != nil {
338-
return status.Err()
339-
}
340-
// Handle responses
341-
for response := range initChan {
342-
if err := response.GetError(); err != nil {
343-
// We return right away without iterating all the errors, the chance
344-
// of failure in this case is slim but it would be great in the future
345-
// to handle errors when updating the libraries indexes much like we
346-
// do when initializing an instance.
347-
return fmt.Errorf("rescanning filesystem: %s", err)
348-
}
349-
}
350334
return nil
351335
}
352336

@@ -467,23 +451,6 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB Do
467451
}
468452
}
469453
}
470-
471-
// Reinitializes an existing instance
472-
initChan, status := Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: req.Instance.Id}})
473-
if status != nil {
474-
return nil, status.Err()
475-
}
476-
// Handle responses
477-
for response := range initChan {
478-
if err := response.GetError(); err != nil {
479-
// We return right away without iterating all the errors, the chance
480-
// of failure in this case is slim but it would be great in the future
481-
// to handle errors when updating the platforms indexes much like we
482-
// do when initializing an instance.
483-
return nil, fmt.Errorf("rescanning filesystem: %s", err)
484-
}
485-
}
486-
487454
return &rpc.UpdateIndexResponse{}, nil
488455
}
489456

0 commit comments

Comments
 (0)