|
20 | 20 | package systray
|
21 | 21 |
|
22 | 22 | import (
|
23 |
| - "fmt" |
24 | 23 | "os"
|
25 | 24 | "os/user"
|
26 |
| - "path/filepath" |
27 | 25 |
|
28 | 26 | log "github.com/sirupsen/logrus"
|
29 | 27 |
|
@@ -155,7 +153,7 @@ func (s *Systray) end() {
|
155 | 153 | func (s *Systray) addConfigs() {
|
156 | 154 | var mConfigCheckbox []*systray.MenuItem
|
157 | 155 |
|
158 |
| - configs := getConfigs() |
| 156 | + configs := s.getConfigs() |
159 | 157 | if len(configs) > 1 {
|
160 | 158 | for _, config := range configs {
|
161 | 159 | entry := systray.AddMenuItem(config.Name, "")
|
@@ -185,35 +183,30 @@ type configIni struct {
|
185 | 183 | Location string
|
186 | 184 | }
|
187 | 185 |
|
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 { |
194 | 188 | var configs []configIni
|
195 | 189 |
|
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" |
210 | 206 | }
|
| 207 | + conf := configIni{Name: name, Location: file.String()} |
| 208 | + configs = append(configs, conf) |
211 | 209 | }
|
212 |
| - return nil |
213 |
| - }) |
214 |
| - |
215 |
| - if err != nil { |
216 |
| - fmt.Println("error walking through executable configuration: %w", err) |
217 | 210 | }
|
218 | 211 |
|
219 | 212 | return configs
|
|
0 commit comments