Skip to content

Commit 98129b9

Browse files
committed
Moved config stuff in config.go
1 parent 54db5f9 commit 98129b9

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,12 +16,14 @@
1616
package main
1717

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

2324
"github.com/arduino/go-paths-helper"
2425
"github.com/arduino/go-win32-utils"
26+
log "github.com/sirupsen/logrus"
2527
)
2628

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

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)