Skip to content

Add "Open config file" menu item #763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -136,13 +136,18 @@ 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,
DebugURL: func() string {
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 {
11 changes: 11 additions & 0 deletions systray/systray.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 24 additions & 28 deletions systray/systray_real.go
Original file line number Diff line number Diff line change
@@ -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