Skip to content

Commit 67ce938

Browse files
committed
Moved config stuff in config.go
1 parent 640587a commit 67ce938

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

config.go

+20
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
package main
1717

1818
import (
19+
_ "embed"
1920
"fmt"
2021
"os"
2122

2223
"github.com/arduino/go-paths-helper"
24+
log "github.com/sirupsen/logrus"
2325
)
2426

2527
// getDefaultArduinoCreateConfigDir returns the full path to the default arduino create agent data directory
@@ -50,3 +52,21 @@ func getDefaultArduinoCreateConfigDir() (*paths.Path, error) {
5052
}
5153
return agentConfigDir, nil
5254
}
55+
56+
//go:embed config.ini
57+
var configContent []byte
58+
59+
// generateConfig function will take a directory path as an input
60+
// and will write the default config,ini file to that directory,
61+
// it will panic if something goes wrong
62+
func generateConfig(destDir *paths.Path) *paths.Path {
63+
configPath := destDir.Join("config.ini")
64+
65+
// generate the config.ini file directly in destDir
66+
if err := configPath.WriteFile(configContent); err != nil {
67+
// if we do not have a config there's nothing else we can do
68+
panic("cannot generate config: " + err.Error())
69+
}
70+
log.Infof("generated config in %s", configPath)
71+
return configPath
72+
}

main.go

+6-26
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package main
2020

2121
import (
22-
_ "embed"
2322
"encoding/json"
2423
"flag"
2524
"io/ioutil"
@@ -82,9 +81,6 @@ var (
8281
crashreport = iniConf.Bool("crashreport", false, "enable crashreport logging")
8382
)
8483

85-
//go:embed config.ini
86-
var configContent []byte
87-
8884
// global clients
8985
var (
9086
Tools tools.Tools
@@ -209,26 +205,25 @@ func loop() {
209205
var configPath *paths.Path
210206

211207
// see if the env var is defined, if it is take the config from there, this will override the default path
212-
envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG")
213-
if envConfig != "" {
208+
if envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG"); envConfig != "" {
214209
configPath = paths.New(envConfig)
215210
if configPath.NotExist() {
216211
log.Panicf("config from env var %s does not exists", envConfig)
217212
}
218213
log.Infof("using config from env variable: %s", configPath)
214+
} else if defaultConfigPath := agentDir.Join("config.ini"); defaultConfigPath.Exist() {
219215
// by default take the config from the ~/.arduino-create/config.ini file
220-
} else if agentDir.Join("config.ini").Exist() {
221-
configPath = agentDir.Join("config.ini")
216+
configPath = defaultConfigPath
222217
log.Infof("using config from default: %s", configPath)
223-
// take the config from the old folder where the agent's binary sits
224218
} else {
219+
// take the config from the old folder where the agent's binary sits
225220
oldConfigPath := srcDir.Join("config.ini")
226221
if oldConfigPath.Exist() {
227-
err := oldConfigPath.CopyTo(agentDir.Join("config.ini"))
222+
err := oldConfigPath.CopyTo(defaultConfigPath)
228223
if err != nil {
229224
log.Errorf("cannot copy old %s, to %s, generating new config", oldConfigPath, configPath)
230225
} else {
231-
configPath = agentDir.Join("config.ini")
226+
configPath = defaultConfigPath
232227
log.Infof("copied old %s, to %s", oldConfigPath, configPath)
233228
}
234229
}
@@ -710,18 +705,3 @@ func parseIni(filename string) (args []string, err error) {
710705

711706
return args, nil
712707
}
713-
714-
// generateConfig function will take a path as an input
715-
// and will write the default config,ini file to that path,
716-
// it will panic if something goes wrong
717-
func generateConfig(destDir *paths.Path) *paths.Path {
718-
// generate the config.ini file directly in destDir
719-
configPath := destDir.Join("config.ini")
720-
err := configPath.WriteFile(configContent)
721-
if err != nil {
722-
// if we do not have a config there's nothing else we can do
723-
log.Panicf("cannot generate config: %s", err)
724-
}
725-
log.Infof("generated config in %s", configPath)
726-
return configPath
727-
}

0 commit comments

Comments
 (0)