Skip to content

Commit c96490b

Browse files
committed
feat(config): add default configuration file and update config handling
1 parent 90cce2c commit c96490b

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

config/config-default.ini

Lines changed: 10 additions & 0 deletions
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)

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func GetDefaultHomeDir() *paths.Path {
108108
return paths.New(homeDir)
109109
}
110110

111-
//go:embed config.ini
111+
//go:embed config-default.ini
112112
var configContent []byte
113113

114114
// GenerateConfig function will take a directory path as an input

config/config_test.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package config
22

33
import (
4-
"fmt"
54
"os"
65
"testing"
6+
"time"
77

88
"github.com/arduino/go-paths-helper"
99
"github.com/sirupsen/logrus"
@@ -38,24 +38,52 @@ func TestGetConfigPath(t *testing.T) {
3838
GetConfigPath()
3939
})
4040

41-
t.Run("read config.ini from $HOME", func(t *testing.T) {
41+
t.Run("read config.ini from $HOME/.config/ArduinoCreateAgent folder", func(t *testing.T) {
4242
os.Setenv("HOME", "./testdata/home")
4343
defer os.Unsetenv("HOME")
4444
configPath := GetConfigPath()
4545
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
4646
})
4747

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
48+
t.Run("legacy config are copied to new location", func(t *testing.T) {
49+
50+
createLegacyConfig := func() string {
51+
// Create a "legacy" config.ini in the same directory as the binary executable
52+
src, err := os.Executable()
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
legacyConfigPath, err := paths.New(src).Parent().Join("config.ini").Create()
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
// adding a timestamp to the content to make it unique
61+
c := "hostname = legacy-config-file-" + time.Now().String()
62+
n, err := legacyConfigPath.WriteString(c)
63+
if err != nil || n <= 0 {
64+
t.Fatalf("Failed to write legacy config file: %v", err)
65+
}
66+
return c
67+
}
68+
69+
wantContent := createLegacyConfig()
70+
71+
// Expectation: it copies the "legacy" config.ini into the location pointed by $HOME
72+
os.Setenv("HOME", "./testdata/fromlegacy")
5473
defer os.Unsetenv("HOME")
5574

56-
// expect it creates a config.ini in the same directory as the binary
75+
// remove any existing config.ini in the into the location pointed by $HOME
76+
err := os.Remove("./testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini")
77+
if err != nil && !os.IsNotExist(err) {
78+
t.Fatal(err)
79+
}
80+
5781
configPath := GetConfigPath()
58-
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
82+
assert.Equal(t, "testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini", configPath.String())
83+
84+
given, err := paths.New(configPath.String()).ReadFile()
85+
assert.Nil(t, err)
86+
assert.Equal(t, wantContent, string(given))
5987
})
6088

6189
}

config/testdata/fromlegacy/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config.ini

0 commit comments

Comments
 (0)