Skip to content

Commit dce3afb

Browse files
cmaglieumbynos
andauthored
Add "Open config file" menu item (#763)
* handle additional config, now they can be added in `~/.arduino-create` * add a menu in the tray menu to open the config file fix #734 --------- Co-authored-by: Umberto Baldi <u.baldi@arduino.cc>
1 parent bd3ba84 commit dce3afb

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

Diff for: main.go

+6
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,18 @@ func main() {
136136
go loop()
137137

138138
// SetupSystray is the main thread
139+
configDir, err := getDefaultArduinoCreateConfigDir()
140+
if err != nil {
141+
log.Panicf("Can't open defaul configuration dir: %s", err)
142+
}
139143
Systray = systray.Systray{
140144
Hibernate: *hibernate,
141145
Version: version + "-" + commit,
142146
DebugURL: func() string {
143147
return "http://" + *address + port
144148
},
145149
AdditionalConfig: *additionalConfig,
150+
ConfigDir: configDir,
146151
}
147152

148153
path, err := os.Executable()
@@ -250,6 +255,7 @@ func loop() {
250255
if err != nil {
251256
log.Panicf("cannot parse arguments: %s", err)
252257
}
258+
Systray.SetCurrentConfigFile(configPath)
253259

254260
// Parse additional ini config if defined
255261
if len(*additionalConfig) > 0 {

Diff for: systray/systray.go

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os/exec"
2222
"strings"
2323

24+
"github.com/arduino/go-paths-helper"
2425
log "github.com/sirupsen/logrus"
2526
)
2627

@@ -34,8 +35,12 @@ type Systray struct {
3435
DebugURL func() string
3536
// The active configuration file
3637
AdditionalConfig string
38+
// The path to the directory containing the configuration files
39+
ConfigDir *paths.Path
3740
// The path of the exe (only used in update)
3841
path string
42+
// The path of the configuration file
43+
currentConfigFilePath *paths.Path
3944
}
4045

4146
// Restart restarts the program
@@ -92,3 +97,9 @@ func (s *Systray) Update(path string) {
9297
s.path = path
9398
s.Restart()
9499
}
100+
101+
// SetCurrentConfigFile allows to specify the path of the configuration file the agent
102+
// is using. The tray menu with this info can display an "open config file" option.
103+
func (s *Systray) SetCurrentConfigFile(configPath *paths.Path) {
104+
s.currentConfigFilePath = configPath
105+
}

Diff for: systray/systray_real.go

+24-28
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
package systray
2121

2222
import (
23-
"fmt"
2423
"os"
2524
"os/user"
26-
"path/filepath"
2725

2826
log "github.com/sirupsen/logrus"
2927

@@ -59,6 +57,7 @@ func (s *Systray) start() {
5957
// Add links
6058
mURL := systray.AddMenuItem("Go to Arduino Create", "Arduino Create")
6159
mDebug := systray.AddMenuItem("Open Debug Console", "Debug console")
60+
mConfig := systray.AddMenuItem("Open Configuration", "Config File")
6261

6362
// Remove crash-reports
6463
mRmCrashes := systray.AddMenuItem("Remove crash reports", "")
@@ -80,6 +79,8 @@ func (s *Systray) start() {
8079
_ = open.Start("https://create.arduino.cc")
8180
case <-mDebug.ClickedCh:
8281
_ = open.Start(s.DebugURL())
82+
case <-mConfig.ClickedCh:
83+
_ = open.Start(s.currentConfigFilePath.String())
8384
case <-mRmCrashes.ClickedCh:
8485
s.RemoveCrashes()
8586
s.updateMenuItem(mRmCrashes, s.CrashesIsEmpty())
@@ -155,7 +156,7 @@ func (s *Systray) end() {
155156
func (s *Systray) addConfigs() {
156157
var mConfigCheckbox []*systray.MenuItem
157158

158-
configs := getConfigs()
159+
configs := s.getConfigs()
159160
if len(configs) > 1 {
160161
for _, config := range configs {
161162
entry := systray.AddMenuItem(config.Name, "")
@@ -185,35 +186,30 @@ type configIni struct {
185186
Location string
186187
}
187188

188-
// getconfigs parses all config files in the executable folder
189-
func getConfigs() []configIni {
190-
// config.ini must be there, so call it Default
191-
src, _ := os.Executable() // TODO change path
192-
dest := filepath.Dir(src)
193-
189+
// getConfigs parses all config files in the .arduino-create folder
190+
func (s *Systray) getConfigs() []configIni {
194191
var configs []configIni
195192

196-
err := filepath.Walk(dest, func(path string, f os.FileInfo, _ error) error {
197-
if !f.IsDir() {
198-
if filepath.Ext(path) == ".ini" {
199-
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true, AllowPythonMultilineValues: true}, filepath.Join(dest, f.Name()))
200-
if err != nil {
201-
return err
202-
}
203-
defaultSection, err := cfg.GetSection("")
204-
name := defaultSection.Key("name").String()
205-
if name == "" || err != nil {
206-
name = "Default config"
207-
}
208-
conf := configIni{Name: name, Location: f.Name()}
209-
configs = append(configs, conf)
193+
files, err := s.ConfigDir.ReadDir()
194+
if err != nil {
195+
log.Errorf("cannot read the content of %s", s.ConfigDir)
196+
return nil
197+
}
198+
files.FilterOutDirs()
199+
files.FilterSuffix(".ini")
200+
for _, file := range files {
201+
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true, AllowPythonMultilineValues: true}, file.String())
202+
if err != nil {
203+
log.Errorf("error walking through executable configuration: %s", err)
204+
} else {
205+
defaultSection, err := cfg.GetSection("")
206+
name := defaultSection.Key("name").String()
207+
if name == "" || err != nil {
208+
name = "Default config"
210209
}
210+
conf := configIni{Name: name, Location: file.String()}
211+
configs = append(configs, conf)
211212
}
212-
return nil
213-
})
214-
215-
if err != nil {
216-
fmt.Println("error walking through executable configuration: %w", err)
217213
}
218214

219215
return configs

0 commit comments

Comments
 (0)