19
19
package main
20
20
21
21
import (
22
+ _ "embed"
22
23
"encoding/json"
23
24
"flag"
24
25
"io/ioutil"
82
83
crashreport = iniConf .Bool ("crashreport" , false , "enable crashreport logging" )
83
84
)
84
85
86
+ //go:embed config.ini
87
+ var configContent []byte
88
+
85
89
// global clients
86
90
var (
87
91
Tools tools.Tools
@@ -181,33 +185,62 @@ func loop() {
181
185
return
182
186
}
183
187
188
+ log .SetLevel (log .InfoLevel )
189
+ log .SetOutput (os .Stdout )
190
+
191
+ // the important folders of the agent
184
192
src , _ := os .Executable ()
185
- srcPath := paths .New (src )
186
- srcDir := srcPath .Parent ()
193
+ srcPath := paths .New (src ) // The path of the agent's binary
194
+ srcDir := srcPath .Parent () // The directory of the agent's binary
195
+ usr , _ := user .Current ()
196
+ usrDir := paths .New (usr .HomeDir ) // The user folder, on linux/macos /home/<usr>/
197
+ agentDir := usrDir .Join (".arduino-create" )
187
198
199
+ // Instantiate Tools
200
+ Tools = tools.Tools {
201
+ Directory : agentDir .String (),
202
+ IndexURL : * indexURL ,
203
+ Logger : func (msg string ) {
204
+ mapD := map [string ]string {"DownloadStatus" : "Pending" , "Msg" : msg }
205
+ mapB , _ := json .Marshal (mapD )
206
+ h .broadcastSys <- mapB
207
+ },
208
+ }
209
+ Tools .Init (requiredToolsAPILevel )
210
+
211
+ // Let's handle the config
188
212
var configPath * paths.Path
189
213
190
- // see if the env var is defined, if it is take the config from there
214
+ // see if the env var is defined, if it is take the config from there, this will override the default path
191
215
envConfig := os .Getenv ("ARDUINO_CREATE_AGENT_CONFIG" )
192
216
if envConfig != "" {
193
217
configPath = paths .New (envConfig )
194
218
if configPath .NotExist () {
195
219
log .Panicf ("config from env var %s does not exists" , envConfig )
196
220
}
221
+ log .Infof ("using config from env variable: %s" , configPath )
222
+ // by default take the config from the ~/.arduino-create/config.ini file
223
+ } else if agentDir .Join ("config.ini" ).Exist () {
224
+ configPath = agentDir .Join ("config.ini" )
225
+ log .Infof ("using config from default: %s" , configPath )
226
+ // take the config from the old folder where the agent's binary sits
197
227
} else {
198
- // take the config from the folder where the binary sits
199
- configPath = srcDir . Join ( "config.ini" )
200
-
201
- if configPath . NotExist () {
202
- // probably we are on macOS, where the config is in a different dir
203
- configPath = srcDir . Parent (). Join ( "Resources" , "config.ini" )
204
- if configPath . NotExist () {
205
- log .Panicf ( "config.ini file not found in %s" , configPath )
228
+ oldConfigPath := srcDir . Join ( " config.ini" )
229
+ if oldConfigPath . Exist () {
230
+ err := oldConfigPath . CopyTo ( agentDir . Join ( "config.ini" ))
231
+ if err != nil {
232
+ log . Errorf ( "cannot copy old %s, to %s, generating new config" , oldConfigPath , configPath )
233
+ } else {
234
+ configPath = agentDir . Join ( "config.ini" )
235
+ log .Infof ( "copied old %s, to %s", oldConfigPath , configPath )
206
236
}
207
237
}
208
238
}
239
+ if configPath == nil {
240
+ configPath = generateConfig (agentDir )
241
+ }
209
242
210
- // Parse default ini config
243
+ // Parse the config.ini
211
244
args , err := parseIni (configPath .String ())
212
245
if err != nil {
213
246
log .Panicf ("config.ini cannot be parsed: %s" , err )
@@ -216,7 +249,6 @@ func loop() {
216
249
if err != nil {
217
250
log .Panicf ("cannot parse arguments: %s" , err )
218
251
}
219
- log .Infof ("using config from %s" , configPath )
220
252
221
253
// Parse additional ini config if defined
222
254
if len (* additionalConfig ) > 0 {
@@ -236,25 +268,6 @@ func loop() {
236
268
}
237
269
}
238
270
239
- // Instantiate Tools
240
- usr , _ := user .Current ()
241
- usrDir := paths .New (usr .HomeDir )
242
- agentDir := usrDir .Join (".arduino-create" )
243
- Tools = tools.Tools {
244
- Directory : agentDir .String (),
245
- IndexURL : * indexURL ,
246
- Logger : func (msg string ) {
247
- mapD := map [string ]string {"DownloadStatus" : "Pending" , "Msg" : msg }
248
- mapB , _ := json .Marshal (mapD )
249
- h .broadcastSys <- mapB
250
- },
251
- }
252
- Tools .Init (requiredToolsAPILevel )
253
-
254
- log .SetLevel (log .InfoLevel )
255
-
256
- log .SetOutput (os .Stdout )
257
-
258
271
// see if we are supposed to wait 5 seconds
259
272
if * isLaunchSelf {
260
273
launchSelfLater ()
@@ -700,3 +713,18 @@ func parseIni(filename string) (args []string, err error) {
700
713
701
714
return args , nil
702
715
}
716
+
717
+ // generateConfig function will take a path as an input
718
+ // and will write the default config,ini file to that path,
719
+ // it will panic if something goes wrong
720
+ func generateConfig (destDir * paths.Path ) * paths.Path {
721
+ // generate the config.ini file directly in destDir
722
+ configPath := destDir .Join ("config.ini" )
723
+ err := configPath .WriteFile (configContent )
724
+ if err != nil {
725
+ // if we do not have a config there's nothing else we can do
726
+ log .Panicf ("cannot generate config: %s" , err )
727
+ }
728
+ log .Infof ("generated config in %s" , configPath )
729
+ return configPath
730
+ }
0 commit comments