Skip to content

Commit 96f0f51

Browse files
committed
refactor: update logger handling and improve config path management
1 parent 17ed6b3 commit 96f0f51

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

hub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func (h *hub) checkCmd(m []byte) {
270270
func logAction(sl string) {
271271
if strings.HasPrefix(sl, "log on") {
272272
*logDump = "on"
273-
//TODO: pass the loggerSw in the constructor and enable again the log e
273+
// FIXME: pass the loggerSw in the constructor and enable again the log e
274274
// multiWriter := io.MultiWriter(&loggerWs, os.Stderr)
275275
// log.SetOutput(multiWriter)
276276
} else if strings.HasPrefix(sl, "log off") {

main.go

+45-22
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
_ "embed"
2323
"encoding/json"
2424
"flag"
25-
"fmt"
2625
"html/template"
2726
"io"
2827
"os"
@@ -103,18 +102,21 @@ var homeTemplateHTML string
103102

104103
// global clients
105104
var (
106-
Tools *tools.Tools
107-
Index *index.Resource
105+
Tools *tools.Tools
106+
Systray systray.Systray
107+
Index *index.Resource
108108
)
109109

110-
// TODO: enable it
110+
// FIXME; the loggerWS is useind in the multiwrite in the hub
111111
// type logWriter struct{}
112112

113113
// func (u *logWriter) Write(p []byte) (n int, err error) {
114114
// h.broadcastSys <- p
115115
// return len(p), nil
116116
// }
117117

118+
// var loggerWs logWriter
119+
118120
func homeHandler(c *gin.Context) {
119121
homeTemplate.Execute(c.Writer, c.Request.Host)
120122
}
@@ -140,13 +142,9 @@ func main() {
140142
// Check if certificates made with Agent <=1.2.7 needs to be moved over the new location
141143
cert.MigrateCertificatesGeneratedWithOldAgentVersions(config.GetCertificatesDir())
142144

143-
configPath := config.GetConfigPath()
144-
fmt.Println("configPath: ", configPath)
145-
146145
// SetupSystray is the main thread
147146
configDir := config.GetDefaultConfigDir()
148-
149-
stray := &systray.Systray{
147+
stray := systray.Systray{
150148
Hibernate: *hibernate,
151149
Version: version + "-" + commit,
152150
DebugURL: func() string {
@@ -155,29 +153,24 @@ func main() {
155153
AdditionalConfig: *additionalConfig,
156154
ConfigDir: configDir,
157155
}
158-
stray.SetCurrentConfigFile(configPath)
159156

160157
// Launch main loop in a goroutine
161-
go loop(stray, configPath)
158+
go loop(&stray)
162159

163160
if src, err := os.Executable(); err != nil {
164161
panic(err)
165162
} else if restartPath := updater.Start(src); restartPath != "" {
166-
stray.RestartWith(restartPath)
163+
Systray.RestartWith(restartPath)
167164
} else {
168-
stray.Start()
165+
Systray.Start()
169166
}
170167
}
171168

172-
func loop(stray *systray.Systray, configPath *paths.Path) {
169+
func loop(stray *systray.Systray) {
173170
if *hibernate {
174171
return
175172
}
176173

177-
if configPath == nil {
178-
log.Panic("configPath is nil")
179-
}
180-
181174
log.SetLevel(log.InfoLevel)
182175
log.SetOutput(os.Stdout)
183176

@@ -190,12 +183,8 @@ func loop(stray *systray.Systray, configPath *paths.Path) {
190183
os.Exit(0)
191184
}
192185

193-
// serialPorts contains the ports attached to the machine
194186
serialPorts := newSerialPortList()
195187
serialHub := newSerialHub()
196-
197-
// var loggerWs logWriter
198-
199188
hub := newHub(serialHub, serialPorts)
200189

201190
logger := func(msg string) {
@@ -204,6 +193,39 @@ func loop(stray *systray.Systray, configPath *paths.Path) {
204193
hub.broadcastSys <- mapB
205194
}
206195

196+
// Let's handle the config
197+
configDir := config.GetDefaultConfigDir()
198+
var configPath *paths.Path
199+
200+
// see if the env var is defined, if it is take the config from there, this will override the default path
201+
if envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG"); envConfig != "" {
202+
configPath = paths.New(envConfig)
203+
if configPath.NotExist() {
204+
log.Panicf("config from env var %s does not exists", envConfig)
205+
}
206+
log.Infof("using config from env variable: %s", configPath)
207+
} else if defaultConfigPath := configDir.Join("config.ini"); defaultConfigPath.Exist() {
208+
// by default take the config from the ~/.arduino-create/config.ini file
209+
configPath = defaultConfigPath
210+
log.Infof("using config from default: %s", configPath)
211+
} else {
212+
// Fall back to the old config.ini location
213+
src, _ := os.Executable()
214+
oldConfigPath := paths.New(src).Parent().Join("config.ini")
215+
if oldConfigPath.Exist() {
216+
err := oldConfigPath.CopyTo(defaultConfigPath)
217+
if err != nil {
218+
log.Errorf("cannot copy old %s, to %s, generating new config", oldConfigPath, configPath)
219+
} else {
220+
configPath = defaultConfigPath
221+
log.Infof("copied old %s, to %s", oldConfigPath, configPath)
222+
}
223+
}
224+
}
225+
if configPath == nil {
226+
configPath = config.GenerateConfig(configDir)
227+
}
228+
207229
// if the default browser is Safari, prompt the user to install HTTPS certificates
208230
// and eventually install them
209231
if runtime.GOOS == "darwin" {
@@ -241,6 +263,7 @@ func loop(stray *systray.Systray, configPath *paths.Path) {
241263
if err != nil {
242264
log.Panicf("cannot parse arguments: %s", err)
243265
}
266+
Systray.SetCurrentConfigFile(configPath)
244267

245268
// Parse additional ini config if defined
246269
if len(*additionalConfig) > 0 {

0 commit comments

Comments
 (0)