Skip to content

Commit b4fbe92

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

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

systray/systray_real.go

+23-25
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

@@ -185,35 +183,35 @@ type configIni struct {
185183
Location string
186184
}
187185

188-
// getconfigs parses all config files in the executable folder
186+
// getConfigs parses all config files in the .arduino-create folder
189187
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)
188+
189+
usr, _ := user.Current()
190+
usrDir := paths.New(usr.HomeDir) // The user folder, on linux/macos /home/<usr>/
191+
agentDir := usrDir.Join(".arduino-create")
193192

194193
var configs []configIni
195194

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

219217
return configs

0 commit comments

Comments
 (0)