1919package main
2020
2121import (
22+ _ "embed"
2223 "encoding/json"
2324 "flag"
2425 "io/ioutil"
8283 crashreport = iniConf .Bool ("crashreport" , false , "enable crashreport logging" )
8384)
8485
86+ //go:embed config.ini
87+ var configContent []byte
88+
8589// global clients
8690var (
8791 Tools tools.Tools
@@ -181,33 +185,62 @@ func loop() {
181185 return
182186 }
183187
188+ log .SetLevel (log .InfoLevel )
189+ log .SetOutput (os .Stdout )
190+
191+ // the important folders of the agent
184192 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" )
187198
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
188212 var configPath * paths.Path
189213
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
191215 envConfig := os .Getenv ("ARDUINO_CREATE_AGENT_CONFIG" )
192216 if envConfig != "" {
193217 configPath = paths .New (envConfig )
194218 if configPath .NotExist () {
195219 log .Panicf ("config from env var %s does not exists" , envConfig )
196220 }
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
197227 } 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 )
206236 }
207237 }
208238 }
239+ if configPath == nil {
240+ configPath = generateConfig (agentDir )
241+ }
209242
210- // Parse default ini config
243+ // Parse the config.ini
211244 args , err := parseIni (configPath .String ())
212245 if err != nil {
213246 log .Panicf ("config.ini cannot be parsed: %s" , err )
@@ -216,7 +249,6 @@ func loop() {
216249 if err != nil {
217250 log .Panicf ("cannot parse arguments: %s" , err )
218251 }
219- log .Infof ("using config from %s" , configPath )
220252
221253 // Parse additional ini config if defined
222254 if len (* additionalConfig ) > 0 {
@@ -236,25 +268,6 @@ func loop() {
236268 }
237269 }
238270
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-
258271 // see if we are supposed to wait 5 seconds
259272 if * isLaunchSelf {
260273 launchSelfLater ()
@@ -700,3 +713,18 @@ func parseIni(filename string) (args []string, err error) {
700713
701714 return args , nil
702715}
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