diff --git a/main.go b/main.go index e0bbb3be1..723ab7e19 100755 --- a/main.go +++ b/main.go @@ -136,6 +136,10 @@ func main() { go loop() // SetupSystray is the main thread + configDir, err := getDefaultArduinoCreateConfigDir() + if err != nil { + log.Panicf("Can't open defaul configuration dir: %s", err) + } Systray = systray.Systray{ Hibernate: *hibernate, Version: version + "-" + commit, @@ -143,6 +147,7 @@ func main() { return "http://" + *address + port }, AdditionalConfig: *additionalConfig, + ConfigDir: configDir, } path, err := os.Executable() @@ -250,6 +255,7 @@ func loop() { if err != nil { log.Panicf("cannot parse arguments: %s", err) } + Systray.SetCurrentConfigFile(configPath) // Parse additional ini config if defined if len(*additionalConfig) > 0 { diff --git a/systray/systray.go b/systray/systray.go index b103395f7..f3593c4b4 100644 --- a/systray/systray.go +++ b/systray/systray.go @@ -21,6 +21,7 @@ import ( "os/exec" "strings" + "github.com/arduino/go-paths-helper" log "github.com/sirupsen/logrus" ) @@ -34,8 +35,12 @@ type Systray struct { DebugURL func() string // The active configuration file AdditionalConfig string + // The path to the directory containing the configuration files + ConfigDir *paths.Path // The path of the exe (only used in update) path string + // The path of the configuration file + currentConfigFilePath *paths.Path } // Restart restarts the program @@ -92,3 +97,9 @@ func (s *Systray) Update(path string) { s.path = path s.Restart() } + +// SetCurrentConfigFile allows to specify the path of the configuration file the agent +// is using. The tray menu with this info can display an "open config file" option. +func (s *Systray) SetCurrentConfigFile(configPath *paths.Path) { + s.currentConfigFilePath = configPath +} diff --git a/systray/systray_real.go b/systray/systray_real.go index 47494aa28..106306542 100644 --- a/systray/systray_real.go +++ b/systray/systray_real.go @@ -20,10 +20,8 @@ package systray import ( - "fmt" "os" "os/user" - "path/filepath" log "github.com/sirupsen/logrus" @@ -59,6 +57,7 @@ func (s *Systray) start() { // Add links mURL := systray.AddMenuItem("Go to Arduino Create", "Arduino Create") mDebug := systray.AddMenuItem("Open Debug Console", "Debug console") + mConfig := systray.AddMenuItem("Open Configuration", "Config File") // Remove crash-reports mRmCrashes := systray.AddMenuItem("Remove crash reports", "") @@ -80,6 +79,8 @@ func (s *Systray) start() { _ = open.Start("https://create.arduino.cc") case <-mDebug.ClickedCh: _ = open.Start(s.DebugURL()) + case <-mConfig.ClickedCh: + _ = open.Start(s.currentConfigFilePath.String()) case <-mRmCrashes.ClickedCh: s.RemoveCrashes() s.updateMenuItem(mRmCrashes, s.CrashesIsEmpty()) @@ -155,7 +156,7 @@ func (s *Systray) end() { func (s *Systray) addConfigs() { var mConfigCheckbox []*systray.MenuItem - configs := getConfigs() + configs := s.getConfigs() if len(configs) > 1 { for _, config := range configs { entry := systray.AddMenuItem(config.Name, "") @@ -185,35 +186,30 @@ type configIni struct { Location string } -// getconfigs parses all config files in the executable folder -func getConfigs() []configIni { - // config.ini must be there, so call it Default - src, _ := os.Executable() // TODO change path - dest := filepath.Dir(src) - +// getConfigs parses all config files in the .arduino-create folder +func (s *Systray) getConfigs() []configIni { var configs []configIni - err := filepath.Walk(dest, func(path string, f os.FileInfo, _ error) error { - if !f.IsDir() { - if filepath.Ext(path) == ".ini" { - cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true, AllowPythonMultilineValues: true}, filepath.Join(dest, f.Name())) - if err != nil { - return err - } - defaultSection, err := cfg.GetSection("") - name := defaultSection.Key("name").String() - if name == "" || err != nil { - name = "Default config" - } - conf := configIni{Name: name, Location: f.Name()} - configs = append(configs, conf) + files, err := s.ConfigDir.ReadDir() + if err != nil { + log.Errorf("cannot read the content of %s", s.ConfigDir) + return nil + } + files.FilterOutDirs() + files.FilterSuffix(".ini") + for _, file := range files { + cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true, AllowPythonMultilineValues: true}, file.String()) + if err != nil { + log.Errorf("error walking through executable configuration: %s", err) + } else { + defaultSection, err := cfg.GetSection("") + name := defaultSection.Key("name").String() + if name == "" || err != nil { + name = "Default config" } + conf := configIni{Name: name, Location: file.String()} + configs = append(configs, conf) } - return nil - }) - - if err != nil { - fmt.Println("error walking through executable configuration: %w", err) } return configs