Skip to content

Commit 3b8b935

Browse files
umbynoscmaglie
authored andcommitted
handle additional config, now they can be added in ~/.arduino-create
1 parent bd3ba84 commit 3b8b935

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

main.go

+5
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()

systray/systray.go

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Systray struct {
3434
DebugURL func() string
3535
// The active configuration file
3636
AdditionalConfig string
37+
// The path to the directory containing the configuration files
38+
ConfigDir *paths.Path
3739
// The path of the exe (only used in update)
3840
path string
3941
}

systray/systray_real.go

+21-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

@@ -155,7 +153,7 @@ func (s *Systray) end() {
155153
func (s *Systray) addConfigs() {
156154
var mConfigCheckbox []*systray.MenuItem
157155

158-
configs := getConfigs()
156+
configs := s.getConfigs()
159157
if len(configs) > 1 {
160158
for _, config := range configs {
161159
entry := systray.AddMenuItem(config.Name, "")
@@ -185,35 +183,30 @@ type configIni struct {
185183
Location string
186184
}
187185

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-
186+
// getConfigs parses all config files in the .arduino-create folder
187+
func (s *Systray) getConfigs() []configIni {
194188
var configs []configIni
195189

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)
190+
files, err := s.ConfigDir.ReadDir()
191+
if err != nil {
192+
log.Errorf("cannot read the content of %s", s.ConfigDir)
193+
return nil
194+
}
195+
files.FilterOutDirs()
196+
files.FilterSuffix(".ini")
197+
for _, file := range files {
198+
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true, AllowPythonMultilineValues: true}, file.String())
199+
if err != nil {
200+
log.Errorf("error walking through executable configuration: %s", err)
201+
} else {
202+
defaultSection, err := cfg.GetSection("")
203+
name := defaultSection.Key("name").String()
204+
if name == "" || err != nil {
205+
name = "Default config"
210206
}
207+
conf := configIni{Name: name, Location: file.String()}
208+
configs = append(configs, conf)
211209
}
212-
return nil
213-
})
214-
215-
if err != nil {
216-
fmt.Println("error walking through executable configuration: %w", err)
217210
}
218211

219212
return configs

0 commit comments

Comments
 (0)