Skip to content

Commit 242052a

Browse files
committedJan 16, 2018
Fix config icons and env export
1 parent 33c5d48 commit 242052a

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed
 

‎trayicon.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
log "github.com/Sirupsen/logrus"
4040
"github.com/arduino/arduino-create-agent/icon"
4141
"github.com/getlantern/systray"
42+
"github.com/go-ini/ini"
4243
"github.com/kardianos/osext"
4344
"github.com/skratchdot/open-golang/open"
4445
"github.com/vharitonsky/iniflags"
@@ -82,7 +83,13 @@ func getConfigs() []ConfigIni {
8283
filepath.Walk(dest, func(path string, f os.FileInfo, _ error) error {
8384
if !f.IsDir() {
8485
if filepath.Ext(path) == ".ini" {
85-
conf := ConfigIni{Name: f.Name(), Localtion: filepath.Join(dest, f.Name())}
86+
cfg, _ := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true}, filepath.Join(dest, f.Name()))
87+
defaultSection, err := cfg.GetSection("")
88+
name := defaultSection.Key("name").String()
89+
if name == "" || err != nil {
90+
name = "Default config"
91+
}
92+
conf := ConfigIni{Name: name, Localtion: f.Name()}
8693
configs = append(configs, conf)
8794
}
8895
}
@@ -91,6 +98,21 @@ func getConfigs() []ConfigIni {
9198
return configs
9299
}
93100

101+
func applyEnvironment(filename string) {
102+
src, _ := osext.Executable()
103+
dest := filepath.Dir(src)
104+
cfg, _ := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true}, filepath.Join(dest, filename))
105+
defaultSection, err := cfg.GetSection("env")
106+
if err != nil {
107+
return
108+
}
109+
for _, env := range defaultSection.KeyStrings() {
110+
val := defaultSection.Key(env).String()
111+
log.Info("Applying env setting: " + env + "=" + val)
112+
os.Setenv(env, val)
113+
}
114+
}
115+
94116
func setupSysTrayReal() {
95117

96118
systray.SetIcon(icon.GetIcon())
@@ -103,8 +125,14 @@ func setupSysTrayReal() {
103125
configs := getConfigs()
104126

105127
for _, config := range configs {
106-
log.Println("Adding " + config.Name)
107-
mConfigCheckbox = append(mConfigCheckbox, systray.AddMenuItem(config.Name, ""))
128+
entry := systray.AddMenuItem(config.Name, "")
129+
mConfigCheckbox = append(mConfigCheckbox, entry)
130+
// decorate configs
131+
gliph := " ☐ "
132+
if *configIni == config.Localtion {
133+
gliph = " 🗹 "
134+
}
135+
entry.SetTitle(gliph + config.Name)
108136
}
109137
//mQuit := systray.AddMenuItem("Quit Plugin", "")
110138

@@ -116,11 +144,12 @@ func setupSysTrayReal() {
116144
<-mConfigCheckbox[v].ClickedCh
117145
flag.Set("config", configs[v].Localtion)
118146
iniflags.UpdateConfig()
119-
mConfigCheckbox[v].SetTitle(" ✓ " + configs[v].Name)
147+
applyEnvironment(configs[v].Localtion)
148+
mConfigCheckbox[v].SetTitle(" 🗹 " + configs[v].Name)
120149
//mConfigCheckbox[v].Check()
121150
for j, _ := range mConfigCheckbox {
122151
if j != v {
123-
mConfigCheckbox[j].SetTitle(" " + configs[j].Name)
152+
mConfigCheckbox[j].SetTitle(" " + configs[j].Name)
124153
}
125154
}
126155
}

0 commit comments

Comments
 (0)
Please sign in to comment.