Skip to content

Commit 047a2c2

Browse files
committed
Settings are now stored in CoreInstance
1 parent 00c935a commit 047a2c2

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

arduino/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ type CommandError interface {
4141
ToRPCStatus() *status.Status
4242
}
4343

44+
// MissingConfigFileError is returned when trying to create a new instance
45+
// without providing a config file path
46+
type MissingConfigFileError struct{}
47+
48+
func (e *MissingConfigFileError) Error() string {
49+
return tr("Missing config file path")
50+
}
51+
52+
// ToRPCStatus converts the error into a *status.Status
53+
func (e *MissingConfigFileError) ToRPCStatus() *status.Status {
54+
return status.New(codes.InvalidArgument, e.Error())
55+
}
56+
4457
// InvalidInstanceError is returned if the instance used in the command is not valid.
4558
type InvalidInstanceError struct{}
4659

commands/instances.go

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
4141
paths "github.com/arduino/go-paths-helper"
4242
"github.com/sirupsen/logrus"
43+
"github.com/spf13/viper"
4344
"go.bug.st/downloader/v2"
4445
"google.golang.org/grpc/codes"
4546
"google.golang.org/grpc/status"
@@ -56,13 +57,21 @@ var instancesCount int32 = 1
5657
// instantiate as many as needed by providing a different configuration
5758
// for each one.
5859
type CoreInstance struct {
60+
id int32
5961
packageManager *packagemanager.PackageManager
6062
librariesManager *librariesmanager.LibrariesManager
63+
Settings *viper.Viper
6164
}
6265

63-
// InstanceContainer FIXMEDOC
64-
type InstanceContainer interface {
65-
GetInstance() *rpc.Instance
66+
// ToRPC returns a pointer to an rpc.Instance with
67+
// the same ID as the calling CoreInstance
68+
func (c *CoreInstance) ToRPC() *rpc.Instance {
69+
return &rpc.Instance{Id: c.id}
70+
}
71+
72+
// ID returns the id of this CoreInstance
73+
func (c *CoreInstance) ID() int32 {
74+
return c.id
6675
}
6776

6877
// GetInstance returns a CoreInstance for the given ID, or nil if ID
@@ -107,10 +116,16 @@ func (instance *CoreInstance) installToolIfMissing(tool *cores.ToolRelease, down
107116

108117
// Create a new CoreInstance ready to be initialized, supporting directories are also created.
109118
func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
110-
instance := &CoreInstance{}
119+
if req.ConfigFile == "" {
120+
return nil, &arduino.MissingConfigFileError{}
121+
}
122+
123+
instance := &CoreInstance{
124+
Settings: configuration.Init(req.ConfigFile),
125+
}
111126

112127
// Setup downloads directory
113-
downloadsDir := paths.New(configuration.Settings.GetString("directories.Downloads"))
128+
downloadsDir := paths.New(instance.Settings.GetString("directories.Downloads"))
114129
if downloadsDir.NotExist() {
115130
err := downloadsDir.MkdirAll()
116131
if err != nil {
@@ -119,8 +134,8 @@ func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
119134
}
120135

121136
// Setup data directory
122-
dataDir := paths.New(configuration.Settings.GetString("directories.Data"))
123-
packagesDir := configuration.PackagesDir(configuration.Settings)
137+
dataDir := paths.New(instance.Settings.GetString("directories.Data"))
138+
packagesDir := configuration.PackagesDir(instance.Settings)
124139
if packagesDir.NotExist() {
125140
err := packagesDir.MkdirAll()
126141
if err != nil {
@@ -131,7 +146,7 @@ func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
131146
// Create package manager
132147
instance.packageManager = packagemanager.NewPackageManager(
133148
dataDir,
134-
configuration.PackagesDir(configuration.Settings),
149+
configuration.PackagesDir(instance.Settings),
135150
downloadsDir,
136151
dataDir.Join("tmp"),
137152
)
@@ -143,18 +158,19 @@ func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
143158
)
144159

145160
// Add directories of libraries bundled with IDE
146-
if bundledLibsDir := configuration.IDEBundledLibrariesDir(configuration.Settings); bundledLibsDir != nil {
161+
if bundledLibsDir := configuration.IDEBundledLibrariesDir(instance.Settings); bundledLibsDir != nil {
147162
instance.librariesManager.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn)
148163
}
149164

150165
// Add libraries directory from config file
151166
instance.librariesManager.AddLibrariesDir(
152-
configuration.LibrariesDir(configuration.Settings),
167+
configuration.LibrariesDir(instance.Settings),
153168
libraries.User,
154169
)
155170

156171
// Save instance
157172
instanceID := instancesCount
173+
instance.id = instanceID
158174
instances[instanceID] = instance
159175
instancesCount++
160176

@@ -186,7 +202,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
186202

187203
// Load Platforms
188204
urls := []string{globals.DefaultIndexURL}
189-
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
205+
urls = append(urls, instance.Settings.GetStringSlice("board_manager.additional_urls")...)
190206
for _, u := range urls {
191207
URL, err := utils.URLParse(u)
192208
if err != nil {
@@ -227,7 +243,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
227243
// We load hardware before verifying builtin tools are installed
228244
// otherwise we wouldn't find them and reinstall them each time
229245
// and they would never get reloaded.
230-
for _, err := range instance.packageManager.LoadHardware() {
246+
hardwareDirs := configuration.HardwareDirectories(instance.Settings)
247+
bundleToolsDirs := configuration.BundleToolsDirectories(instance.Settings)
248+
for _, err := range instance.packageManager.LoadHardware(hardwareDirs, bundleToolsDirs) {
231249
responseCallback(&rpc.InitResponse{
232250
Message: &rpc.InitResponse_Error{
233251
Error: err.Proto(),
@@ -290,7 +308,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
290308
if toolsHaveBeenInstalled {
291309
// We installed at least one new tool after loading hardware
292310
// so we must reload again otherwise we would never found them.
293-
for _, err := range instance.packageManager.LoadHardware() {
311+
hardwareDirs := configuration.HardwareDirectories(instance.Settings)
312+
bundleToolsDirs := configuration.BundleToolsDirectories(instance.Settings)
313+
for _, err := range instance.packageManager.LoadHardware(hardwareDirs, bundleToolsDirs) {
294314
responseCallback(&rpc.InitResponse{
295315
Message: &rpc.InitResponse_Error{
296316
Error: err.Proto(),
@@ -337,7 +357,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
337357
// Refreshes the locale used, this will change the
338358
// language of the CLI if the locale is different
339359
// after started.
340-
i18n.Init(configuration.Settings.GetString("locale"))
360+
i18n.Init(instance.Settings.GetString("locale"))
341361

342362
return nil
343363
}
@@ -356,7 +376,8 @@ func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse
356376
// UpdateLibrariesIndex updates the library_index.json
357377
func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequest, downloadCB func(*rpc.DownloadProgress)) error {
358378
logrus.Info("Updating libraries index")
359-
lm := GetLibraryManager(req.GetInstance().GetId())
379+
instanceID := req.GetInstance().GetId()
380+
lm := GetLibraryManager(instanceID)
360381
if lm == nil {
361382
return &arduino.InvalidInstanceError{}
362383
}
@@ -424,16 +445,16 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
424445

425446
// UpdateIndex FIXMEDOC
426447
func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB DownloadProgressCB) (*rpc.UpdateIndexResponse, error) {
427-
id := req.GetInstance().GetId()
428-
_, ok := instances[id]
448+
instanceID := req.GetInstance().GetId()
449+
instance, ok := instances[instanceID]
429450
if !ok {
430451
return nil, &arduino.InvalidInstanceError{}
431452
}
432453

433-
indexpath := paths.New(configuration.Settings.GetString("directories.Data"))
454+
indexpath := paths.New(instance.Settings.GetString("directories.Data"))
434455

435456
urls := []string{globals.DefaultIndexURL}
436-
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
457+
urls = append(urls, instance.Settings.GetStringSlice("board_manager.additional_urls")...)
437458
for _, u := range urls {
438459
logrus.Info("URL: ", u)
439460
URL, err := utils.URLParse(u)

0 commit comments

Comments
 (0)