Skip to content

Commit 462ad83

Browse files
committed
Perform first-update automatically in gRPC Init
1 parent 5710d4a commit 462ad83

File tree

6 files changed

+36
-82
lines changed

6 files changed

+36
-82
lines changed

Diff for: commands/instances.go

+33
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
240240
})
241241
}
242242

243+
// Perform first-update of indexes if needed
244+
firstUpdate(context.Background(), req.GetInstance(), downloadCallback)
245+
243246
// Try to extract profile if specified
244247
var profile *sketch.Profile
245248
if req.GetProfile() != "" {
@@ -595,3 +598,33 @@ func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketc
595598
RootFolderFiles: rootFolderFiles,
596599
}, nil
597600
}
601+
602+
// firstUpdate downloads libraries and packages indexes if they don't exist.
603+
// This ideally is only executed the first time the CLI is run.
604+
func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(msg *rpc.DownloadProgress)) error {
605+
// Gets the data directory to verify if library_index.json and package_index.json exist
606+
dataDir := configuration.DataDir(configuration.Settings)
607+
libraryIndex := dataDir.Join("library_index.json")
608+
packageIndex := dataDir.Join("package_index.json")
609+
610+
if libraryIndex.NotExist() {
611+
// The library_index.json file doesn't exists, that means the CLI is run for the first time
612+
// so we proceed with the first update that downloads the file
613+
req := &rpc.UpdateLibrariesIndexRequest{Instance: instance}
614+
if err := UpdateLibrariesIndex(ctx, req, downloadCb); err != nil {
615+
return err
616+
}
617+
}
618+
619+
if packageIndex.NotExist() {
620+
// The package_index.json file doesn't exists, that means the CLI is run for the first time,
621+
// similarly to the library update we download that file and all the other package indexes
622+
// from additional_urls
623+
req := &rpc.UpdateIndexRequest{Instance: instance, IgnoreCustomPackageIndexes: true}
624+
if err := UpdateIndex(ctx, req, downloadCb); err != nil {
625+
return err
626+
}
627+
}
628+
629+
return nil
630+
}

Diff for: internal/cli/core/update_index.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func initUpdateIndexCommand() *cobra.Command {
4040
}
4141

4242
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
43-
inst := instance.CreateInstanceAndRunFirstUpdate()
43+
inst := instance.CreateAndInit()
4444
logrus.Info("Executing `arduino-cli core update-index`")
4545
UpdateIndex(inst)
4646
}

Diff for: internal/cli/instance/instance.go

-76
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
package instance
1717

1818
import (
19-
"context"
20-
2119
"github.com/arduino/arduino-cli/commands"
22-
"github.com/arduino/arduino-cli/configuration"
2320
"github.com/arduino/arduino-cli/i18n"
2421
"github.com/arduino/arduino-cli/internal/cli/feedback"
2522
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -71,12 +68,6 @@ func Init(instance *rpc.Instance) {
7168
// In case of loading failures return a list of errors for each platform or library that we failed to load.
7269
// Required Package and library indexes files are automatically downloaded.
7370
func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *paths.Path) *rpc.Profile {
74-
// In case the CLI is executed for the first time
75-
if err := FirstUpdate(instance); err != nil {
76-
feedback.Warning(tr("Error initializing instance: %v", err))
77-
return nil
78-
}
79-
8071
downloadCallback := feedback.ProgressBar()
8172
taskCallback := feedback.TaskProgress()
8273

@@ -110,70 +101,3 @@ func InitWithProfile(instance *rpc.Instance, profileName string, sketchPath *pat
110101

111102
return profile
112103
}
113-
114-
// FirstUpdate downloads libraries and packages indexes if they don't exist.
115-
// This ideally is only executed the first time the CLI is run.
116-
func FirstUpdate(instance *rpc.Instance) error {
117-
// Gets the data directory to verify if library_index.json and package_index.json exist
118-
dataDir := configuration.DataDir(configuration.Settings)
119-
120-
libraryIndex := dataDir.Join("library_index.json")
121-
packageIndex := dataDir.Join("package_index.json")
122-
123-
if libraryIndex.Exist() && packageIndex.Exist() {
124-
return nil
125-
}
126-
127-
// The library_index.json file doesn't exists, that means the CLI is run for the first time
128-
// so we proceed with the first update that downloads the file
129-
if libraryIndex.NotExist() {
130-
err := commands.UpdateLibrariesIndex(context.Background(),
131-
&rpc.UpdateLibrariesIndexRequest{
132-
Instance: instance,
133-
},
134-
feedback.ProgressBar(),
135-
)
136-
if err != nil {
137-
return err
138-
}
139-
}
140-
141-
// The package_index.json file doesn't exists, that means the CLI is run for the first time,
142-
// similarly to the library update we download that file and all the other package indexes
143-
// from additional_urls
144-
if packageIndex.NotExist() {
145-
err := commands.UpdateIndex(context.Background(),
146-
&rpc.UpdateIndexRequest{
147-
Instance: instance,
148-
IgnoreCustomPackageIndexes: true,
149-
},
150-
feedback.ProgressBar())
151-
if err != nil {
152-
return err
153-
}
154-
}
155-
156-
return nil
157-
}
158-
159-
// CreateInstanceAndRunFirstUpdate creates an instance and runs `FirstUpdate`.
160-
// It is mandatory for all `update-index` commands to call this
161-
func CreateInstanceAndRunFirstUpdate() *rpc.Instance {
162-
// We don't initialize any CoreInstance when updating indexes since we don't need to.
163-
// Also meaningless errors might be returned when calling this command with --additional-urls
164-
// since the CLI would be searching for a corresponding file for the additional urls set
165-
// as argument but none would be obviously found.
166-
inst, status := Create()
167-
if status != nil {
168-
feedback.Fatal(tr("Error creating instance: %v", status), feedback.ErrGeneric)
169-
}
170-
171-
// In case this is the first time the CLI is run we need to update indexes
172-
// to make it work correctly, we must do this explicitly in this command since
173-
// we must use instance.Create instead of instance.CreateAndInit for the
174-
// reason stated above.
175-
if err := FirstUpdate(inst); err != nil {
176-
feedback.Fatal(tr("Error updating indexes: %v", err), feedback.ErrGeneric)
177-
}
178-
return inst
179-
}

Diff for: internal/cli/lib/update_index.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func initUpdateIndexCommand() *cobra.Command {
4040
}
4141

4242
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
43-
inst := instance.CreateInstanceAndRunFirstUpdate()
43+
inst := instance.CreateAndInit()
4444
logrus.Info("Executing `arduino-cli lib update-index`")
4545
UpdateIndex(inst)
4646
}

Diff for: internal/cli/update/update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewCommand() *cobra.Command {
4747
}
4848

4949
func runUpdateCommand(showOutdated bool) {
50-
inst := instance.CreateInstanceAndRunFirstUpdate()
50+
inst := instance.CreateAndInit()
5151
logrus.Info("Executing `arduino-cli update`")
5252
lib.UpdateIndex(inst)
5353
core.UpdateIndex(inst)

Diff for: internal/integrationtest/daemon/daemon_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ func createEnvForDaemon(t *testing.T) (*integrationtest.Environment, *integratio
111111
UseSharedStagingFolder: true,
112112
})
113113

114-
_, _, err := cli.Run("core", "update-index")
115-
require.NoError(t, err)
116-
117114
_ = cli.StartDaemon(false)
118115
return env, cli
119116
}

0 commit comments

Comments
 (0)