From 90cce2cbee999127646c4b73b0a69dcebfc9752d Mon Sep 17 00:00:00 2001 From: Davide N Date: Mon, 20 Jan 2025 17:48:28 +0100 Subject: [PATCH] fix --- config/config.go | 38 ++++++++++++ config/config_test.go | 61 +++++++++++++++++++ config/testdata/fromenv/config.ini | 8 +++ .../.config/ArduinoCreateAgent/config.ini | 8 +++ .../.config/ArduinoCreateAgent/config.ini | 10 +++ main.go | 34 +---------- 6 files changed, 128 insertions(+), 31 deletions(-) create mode 100644 config/config_test.go create mode 100644 config/testdata/fromenv/config.ini create mode 100644 config/testdata/home/.config/ArduinoCreateAgent/config.ini create mode 100644 config/testdata/noconfig/.config/ArduinoCreateAgent/config.ini diff --git a/config/config.go b/config/config.go index 69d29eeee..50978eb82 100644 --- a/config/config.go +++ b/config/config.go @@ -142,3 +142,41 @@ func SetInstallCertsIni(filename string, value string) error { } return nil } + +func GetConfigPath() *paths.Path { + // Let's handle the config + configDir := GetDefaultConfigDir() + var configPath *paths.Path + + // see if the env var is defined, if it is take the config from there, this will override the default path + if envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG"); envConfig != "" { + configPath = paths.New(envConfig) + if configPath.NotExist() { + log.Panicf("config from env var %s does not exists", envConfig) + } + log.Infof("using config from env variable: %s", configPath) + } else if defaultConfigPath := configDir.Join("config.ini"); defaultConfigPath.Exist() { + // by default take the config from the ~/.arduino-create/config.ini file + configPath = defaultConfigPath + log.Infof("using config from default: %s", configPath) + } else { + // Fall back to the old config.ini location + src, _ := os.Executable() + oldConfigPath := paths.New(src).Parent().Join("config.ini") + if oldConfigPath.Exist() { + err := oldConfigPath.CopyTo(defaultConfigPath) + if err != nil { + log.Errorf("cannot copy old %s, to %s, generating new config", oldConfigPath, configPath) + } else { + configPath = defaultConfigPath + log.Infof("copied old %s, to %s", oldConfigPath, configPath) + } + } + } + if configPath == nil { + configPath = GenerateConfig(configDir) + } + + return configPath + +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 000000000..76e6988c0 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,61 @@ +package config + +import ( + "fmt" + "os" + "testing" + + "github.com/arduino/go-paths-helper" + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +func TestGetConfigPath(t *testing.T) { + t.Run("read config.ini from ARDUINO_CREATE_AGENT_CONFIG", func(t *testing.T) { + os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/fromenv/config.ini") + defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG") + configPath := GetConfigPath() + assert.Equal(t, "./testdata/fromenv/config.ini", configPath.String()) + }) + + t.Run("panic if config.ini does not exist", func(t *testing.T) { + os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/nonexistent_config.ini") + defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG") + + defer func() { + if r := recover(); r != nil { + entry, ok := r.(*logrus.Entry) + if !ok { + t.Errorf("Expected panic of type *logrus.Entry but got %T", r) + } else { + assert.Equal(t, "config from env var ./testdata/nonexistent_config.ini does not exists", entry.Message) + } + } else { + t.Errorf("Expected panic but did not get one") + } + }() + + GetConfigPath() + }) + + t.Run("read config.ini from $HOME", func(t *testing.T) { + os.Setenv("HOME", "./testdata/home") + defer os.Unsetenv("HOME") + configPath := GetConfigPath() + assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String()) + }) + + t.Run("fallback old : read config.ini where the binary is launched", func(t *testing.T) { + src, _ := os.Executable() + paths.New(src).Parent().Join("config.ini").Create() // create a config.ini in the same directory as the binary + // The fallback path is the directory where the binary is launched + fmt.Println(src) + os.Setenv("HOME", "./testdata/noconfig") // force to not have a config in the home directory + defer os.Unsetenv("HOME") + + // expect it creates a config.ini in the same directory as the binary + configPath := GetConfigPath() + assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String()) + }) + +} diff --git a/config/testdata/fromenv/config.ini b/config/testdata/fromenv/config.ini new file mode 100644 index 000000000..5b31315b9 --- /dev/null +++ b/config/testdata/fromenv/config.ini @@ -0,0 +1,8 @@ +gc = std +hostname = unknown-hostname +regex = usb|acm|com +v = true +appName = CreateAgent/Stable +updateUrl = https://downloads.arduino.cc/ +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 +crashreport = false diff --git a/config/testdata/home/.config/ArduinoCreateAgent/config.ini b/config/testdata/home/.config/ArduinoCreateAgent/config.ini new file mode 100644 index 000000000..92f231faf --- /dev/null +++ b/config/testdata/home/.config/ArduinoCreateAgent/config.ini @@ -0,0 +1,8 @@ +gc = std +hostname = unknown-hostname +regex = usb|acm|com +v = true +appName = config-from-home-dir +updateUrl = https://downloads.arduino.cc/ +origins = https://local.arduino.cc:8000, https://local.arduino.cc:8001, https://*.iot-cloud-arduino-cc.pages.dev +crashreport = false diff --git a/config/testdata/noconfig/.config/ArduinoCreateAgent/config.ini b/config/testdata/noconfig/.config/ArduinoCreateAgent/config.ini new file mode 100644 index 000000000..f63377db5 --- /dev/null +++ b/config/testdata/noconfig/.config/ArduinoCreateAgent/config.ini @@ -0,0 +1,10 @@ +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) +hostname = unknown-hostname # Override the hostname we get from the OS +regex = usb|acm|com # Regular expression to filter serial port list +v = true # show debug logging +appName = CreateAgent/Stable +updateUrl = https://downloads.arduino.cc/ +origins = https://local.arduino.cc:8000 +#httpProxy = http://your.proxy:port # Proxy server for HTTP requests +crashreport = false # enable crashreport logging +autostartMacOS = true # the Arduino Create Agent is able to start automatically after login on macOS (launchd agent) \ No newline at end of file diff --git a/main.go b/main.go index 1ca857b02..a83d7f4c5 100755 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( _ "embed" "encoding/json" "flag" + "fmt" "html/template" "io" "os" @@ -188,38 +189,9 @@ func loop() { h.broadcastSys <- mapB } - // Let's handle the config - configDir := config.GetDefaultConfigDir() - var configPath *paths.Path + configPath := config.GetConfigPath() - // see if the env var is defined, if it is take the config from there, this will override the default path - if envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG"); envConfig != "" { - configPath = paths.New(envConfig) - if configPath.NotExist() { - log.Panicf("config from env var %s does not exists", envConfig) - } - log.Infof("using config from env variable: %s", configPath) - } else if defaultConfigPath := configDir.Join("config.ini"); defaultConfigPath.Exist() { - // by default take the config from the ~/.arduino-create/config.ini file - configPath = defaultConfigPath - log.Infof("using config from default: %s", configPath) - } else { - // Fall back to the old config.ini location - src, _ := os.Executable() - oldConfigPath := paths.New(src).Parent().Join("config.ini") - if oldConfigPath.Exist() { - err := oldConfigPath.CopyTo(defaultConfigPath) - if err != nil { - log.Errorf("cannot copy old %s, to %s, generating new config", oldConfigPath, configPath) - } else { - configPath = defaultConfigPath - log.Infof("copied old %s, to %s", oldConfigPath, configPath) - } - } - } - if configPath == nil { - configPath = config.GenerateConfig(configDir) - } + fmt.Println("configPath: ", configPath) // if the default browser is Safari, prompt the user to install HTTPS certificates // and eventually install them