Skip to content

Commit

Permalink
Merge branch 'main' into remove-open-not-used-buffer-algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
dido18 committed Jan 21, 2025
2 parents c042f10 + 34d4452 commit 9d2b0ab
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 34 deletions.
38 changes: 38 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,41 @@ func SetInstallCertsIni(filename string, value string) error {
}
return nil
}

func GetConfigPath() *paths.Path {

Check failure on line 146 in config/config.go

View workflow job for this annotation

GitHub Actions / check-style (./)

exported function GetConfigPath should have comment or be unexported

Check failure on line 146 in config/config.go

View workflow job for this annotation

GitHub Actions / check-style (./)

exported function GetConfigPath should have comment or be unexported
// 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

}
61 changes: 61 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -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())
})

}
8 changes: 8 additions & 0 deletions config/testdata/fromenv/config.ini
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions config/testdata/home/.config/ArduinoCreateAgent/config.ini
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions config/testdata/noconfig/.config/ArduinoCreateAgent/config.ini
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 3 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"os"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/base64"
"io"
"strconv"
"sync/atomic"
"time"

log "github.com/sirupsen/logrus"
Expand All @@ -42,7 +43,7 @@ type serport struct {

// Keep track of whether we're being actively closed
// just so we don't show scary error messages
isClosing bool
isClosing atomic.Bool

isClosingDueToError bool

Expand Down Expand Up @@ -79,7 +80,7 @@ func (p *serport) reader() {
n, err := p.portIo.Read(serialBuffer)

//if we detect that port is closing, break out of this for{} loop.
if p.isClosing {
if p.isClosing.Load() {
strmsg := "Shutting down reader on " + p.portConf.Name
log.Println(strmsg)
h.broadcastSys <- []byte(strmsg)
Expand Down Expand Up @@ -298,7 +299,8 @@ func spHandlerOpen(portname string, baud int) {
}

func (p *serport) Close() {
p.isClosing = true
p.isClosing.Store(true)

p.bufferFlow.Close()
p.portIo.Close()
serialPorts.MarkPortAsClosed(p.portName)
Expand Down

0 comments on commit 9d2b0ab

Please sign in to comment.