Skip to content

Commit 90cce2c

Browse files
committed
fix
1 parent 1b94ccc commit 90cce2c

File tree

6 files changed

+128
-31
lines changed

6 files changed

+128
-31
lines changed

config/config.go

+38
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,41 @@ func SetInstallCertsIni(filename string, value string) error {
142142
}
143143
return nil
144144
}
145+
146+
func GetConfigPath() *paths.Path {
147+
// Let's handle the config
148+
configDir := GetDefaultConfigDir()
149+
var configPath *paths.Path
150+
151+
// see if the env var is defined, if it is take the config from there, this will override the default path
152+
if envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG"); envConfig != "" {
153+
configPath = paths.New(envConfig)
154+
if configPath.NotExist() {
155+
log.Panicf("config from env var %s does not exists", envConfig)
156+
}
157+
log.Infof("using config from env variable: %s", configPath)
158+
} else if defaultConfigPath := configDir.Join("config.ini"); defaultConfigPath.Exist() {
159+
// by default take the config from the ~/.arduino-create/config.ini file
160+
configPath = defaultConfigPath
161+
log.Infof("using config from default: %s", configPath)
162+
} else {
163+
// Fall back to the old config.ini location
164+
src, _ := os.Executable()
165+
oldConfigPath := paths.New(src).Parent().Join("config.ini")
166+
if oldConfigPath.Exist() {
167+
err := oldConfigPath.CopyTo(defaultConfigPath)
168+
if err != nil {
169+
log.Errorf("cannot copy old %s, to %s, generating new config", oldConfigPath, configPath)
170+
} else {
171+
configPath = defaultConfigPath
172+
log.Infof("copied old %s, to %s", oldConfigPath, configPath)
173+
}
174+
}
175+
}
176+
if configPath == nil {
177+
configPath = GenerateConfig(configDir)
178+
}
179+
180+
return configPath
181+
182+
}

config/config_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/arduino/go-paths-helper"
9+
"github.com/sirupsen/logrus"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestGetConfigPath(t *testing.T) {
14+
t.Run("read config.ini from ARDUINO_CREATE_AGENT_CONFIG", func(t *testing.T) {
15+
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/fromenv/config.ini")
16+
defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
17+
configPath := GetConfigPath()
18+
assert.Equal(t, "./testdata/fromenv/config.ini", configPath.String())
19+
})
20+
21+
t.Run("panic if config.ini does not exist", func(t *testing.T) {
22+
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/nonexistent_config.ini")
23+
defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
24+
25+
defer func() {
26+
if r := recover(); r != nil {
27+
entry, ok := r.(*logrus.Entry)
28+
if !ok {
29+
t.Errorf("Expected panic of type *logrus.Entry but got %T", r)
30+
} else {
31+
assert.Equal(t, "config from env var ./testdata/nonexistent_config.ini does not exists", entry.Message)
32+
}
33+
} else {
34+
t.Errorf("Expected panic but did not get one")
35+
}
36+
}()
37+
38+
GetConfigPath()
39+
})
40+
41+
t.Run("read config.ini from $HOME", func(t *testing.T) {
42+
os.Setenv("HOME", "./testdata/home")
43+
defer os.Unsetenv("HOME")
44+
configPath := GetConfigPath()
45+
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
46+
})
47+
48+
t.Run("fallback old : read config.ini where the binary is launched", func(t *testing.T) {
49+
src, _ := os.Executable()
50+
paths.New(src).Parent().Join("config.ini").Create() // create a config.ini in the same directory as the binary
51+
// The fallback path is the directory where the binary is launched
52+
fmt.Println(src)
53+
os.Setenv("HOME", "./testdata/noconfig") // force to not have a config in the home directory
54+
defer os.Unsetenv("HOME")
55+
56+
// expect it creates a config.ini in the same directory as the binary
57+
configPath := GetConfigPath()
58+
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
59+
})
60+
61+
}

config/testdata/fromenv/config.ini

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
gc = std
2+
hostname = unknown-hostname
3+
regex = usb|acm|com
4+
v = true
5+
appName = CreateAgent/Stable
6+
updateUrl = https://downloads.arduino.cc/
7+
origins = https://local.arduino.cc:8000, https://local.arduino.cc:8001, https://create-dev.arduino.cc, https://*.sparklyunicorn.cc, https://*.iot-cloud-arduino-cc.pages.dev, https://cloud.oniudra.cc, https://app.oniudra.cc,https://*.iot-cloud-arduino-cc.pages.dev
8+
crashreport = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
gc = std
2+
hostname = unknown-hostname
3+
regex = usb|acm|com
4+
v = true
5+
appName = config-from-home-dir
6+
updateUrl = https://downloads.arduino.cc/
7+
origins = https://local.arduino.cc:8000, https://local.arduino.cc:8001, https://*.iot-cloud-arduino-cc.pages.dev
8+
crashreport = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
gc = std # Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)
2+
hostname = unknown-hostname # Override the hostname we get from the OS
3+
regex = usb|acm|com # Regular expression to filter serial port list
4+
v = true # show debug logging
5+
appName = CreateAgent/Stable
6+
updateUrl = https://downloads.arduino.cc/
7+
origins = https://local.arduino.cc:8000
8+
#httpProxy = http://your.proxy:port # Proxy server for HTTP requests
9+
crashreport = false # enable crashreport logging
10+
autostartMacOS = true # the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)

main.go

+3-31
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
_ "embed"
2323
"encoding/json"
2424
"flag"
25+
"fmt"
2526
"html/template"
2627
"io"
2728
"os"
@@ -188,38 +189,9 @@ func loop() {
188189
h.broadcastSys <- mapB
189190
}
190191

191-
// Let's handle the config
192-
configDir := config.GetDefaultConfigDir()
193-
var configPath *paths.Path
192+
configPath := config.GetConfigPath()
194193

195-
// see if the env var is defined, if it is take the config from there, this will override the default path
196-
if envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG"); envConfig != "" {
197-
configPath = paths.New(envConfig)
198-
if configPath.NotExist() {
199-
log.Panicf("config from env var %s does not exists", envConfig)
200-
}
201-
log.Infof("using config from env variable: %s", configPath)
202-
} else if defaultConfigPath := configDir.Join("config.ini"); defaultConfigPath.Exist() {
203-
// by default take the config from the ~/.arduino-create/config.ini file
204-
configPath = defaultConfigPath
205-
log.Infof("using config from default: %s", configPath)
206-
} else {
207-
// Fall back to the old config.ini location
208-
src, _ := os.Executable()
209-
oldConfigPath := paths.New(src).Parent().Join("config.ini")
210-
if oldConfigPath.Exist() {
211-
err := oldConfigPath.CopyTo(defaultConfigPath)
212-
if err != nil {
213-
log.Errorf("cannot copy old %s, to %s, generating new config", oldConfigPath, configPath)
214-
} else {
215-
configPath = defaultConfigPath
216-
log.Infof("copied old %s, to %s", oldConfigPath, configPath)
217-
}
218-
}
219-
}
220-
if configPath == nil {
221-
configPath = config.GenerateConfig(configDir)
222-
}
194+
fmt.Println("configPath: ", configPath)
223195

224196
// if the default browser is Safari, prompt the user to install HTTPS certificates
225197
// and eventually install them

0 commit comments

Comments
 (0)