Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove global clients #1014

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
90cce2c
fix
dido18 Jan 20, 2025
c96490b
feat(config): add default configuration file and update config handling
dido18 Jan 21, 2025
7b4859a
fix(.gitignore): add entry to ignore config.ini file
dido18 Jan 21, 2025
279de2a
test(config): add test for writing default config.ini file and update…
dido18 Jan 21, 2025
4f3ac5a
test(config): add tests for retrieving config paths from XDG_CONFIG_H…
dido18 Jan 21, 2025
c41b815
feat(config): pass config path to main loop and update handling
dido18 Jan 22, 2025
55ca9e5
remove global systray
dido18 Jan 22, 2025
f07103e
WIP: remove hub and sh globals
dido18 Jan 22, 2025
ef94d97
remove global clients: use callback to avoid nesting import
dido18 Jan 24, 2025
d2d3aa6
feat(workflow): add Go installation step to release workflow
dido18 Mar 31, 2025
640e644
refactor: rename variable 'h' to 'hub' for clarity in main.go and upd…
dido18 Mar 31, 2025
99f8235
docs: add comments to clarify functions and handlers in config, hub, …
dido18 Mar 31, 2025
5e2161e
refactor: rename 'hub' and 'serialhub' types to 'Hub' and 'Serialhub'…
dido18 Mar 31, 2025
910bac2
refactor: rename 'Hub' and 'Serialhub' types to 'hub' and 'serialhub'…
dido18 Mar 31, 2025
17ed6b3
revert config changes
dido18 Mar 31, 2025
96f0f51
refactor: update logger handling and improve config path management
dido18 Mar 31, 2025
36af1ed
remove globas tools and index
dido18 Mar 31, 2025
f939e32
refactor: enhance logging mechanism by implementing logWriter and upd…
dido18 Apr 1, 2025
e22281f
remove log websocket
dido18 Apr 1, 2025
3bbf371
remove unused global clients comment from hub struct
dido18 Apr 1, 2025
0773f99
refactor: integrate systray into hub structure and update newHub func…
dido18 Apr 1, 2025
9c2ca54
pass tests
dido18 Apr 1, 2025
7e73b6c
Merge branch 'main' into remove-global-clients
dido18 Apr 1, 2025
fc51194
chore: add comment regarding potential removal of sysStray dependencies
dido18 Apr 1, 2025
8106c84
feat: move serial port into new file
dido18 Apr 2, 2025
4dba1a8
feat: add serialhub implementation for managing serial ports
dido18 Apr 2, 2025
7ea21c0
refactor: remove serialhub parameter from newHub function and instant…
dido18 Apr 2, 2025
a152b02
feat: move `spErr` `spClose` `spWrite` into hub (from serialhub)
dido18 Apr 2, 2025
fac0b10
refactor: remove newSerialHub instantiation from hub initialization i…
dido18 Apr 2, 2025
f04d5cc
move `spHandlerOpen` to hub from serialport.go
dido18 Apr 2, 2025
47213d1
refactor: remove unused serial package import from serialport.go
dido18 Apr 2, 2025
14ddd43
renamed h into hub
dido18 Apr 2, 2025
1f9b999
fix: add hub reference to PLogger initialization in uploadHandler
dido18 Apr 2, 2025
4585769
move `spErr, spWrite, spClose spHandlerOpen at the end for redaibility
dido18 Apr 2, 2025
283064c
refactor: clean up serialport.go and serialportlist.go for clarity
dido18 Apr 2, 2025
14d72e8
refactor: clarify the impact of removing serialPorts.List() in writer…
dido18 Apr 2, 2025
9e13dcb
move serialportlist inside hub
dido18 Apr 2, 2025
a69c0dc
refactor on callback
dido18 Apr 2, 2025
7e34071
refactor: simplify hub initialization in upload handler tests
dido18 Apr 3, 2025
815a791
refactor: remove unused port listing in checkCmd function
dido18 Apr 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/config-default.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)
40 changes: 39 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
return paths.New(homeDir)
}

//go:embed config.ini
//go:embed config-default.ini
var configContent []byte

// GenerateConfig function will take a directory path as an input
Expand Down Expand Up @@ -142,3 +142,41 @@
}
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

}
94 changes: 94 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package config

import (
"os"
"testing"
"time"

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
)

func TestGetConfigPathFromXDG_CONFIG_HOME(t *testing.T) {
// read config from $XDG_CONFIG_HOME/ArduinoCreateAgent/config.ini
os.Setenv("XDG_CONFIG_HOME", "./testdata/fromxdghome")
defer os.Unsetenv("XDG_CONFIG_HOME")
configPath := GetConfigPath()
assert.Equal(t, "testdata/fromxdghome/ArduinoCreateAgent/config.ini", configPath.String())
}

func TestGetConfigPathFromHOME(t *testing.T) {
// Test case 2: read config from $HOME/.config/ArduinoCreateAgent/config.ini "
os.Setenv("HOME", "./testdata/fromhome")
defer os.Unsetenv("HOME")
configPath := GetConfigPath()
assert.Equal(t, "testdata/fromhome/.config/ArduinoCreateAgent/config.ini", configPath.String())

}

func TestGetConfigPathFromARDUINO_CREATE_AGENT_CONFIG(t *testing.T) {
// read config from ARDUINO_CREATE_AGENT_CONFIG/config.ini"
os.Setenv("HOME", "./fromhome")
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())
}

func TestGetConfigPathFromLegacyConfig(t *testing.T) {
// If no config is found, copy the legacy config to the new location
src, err := os.Executable()
if err != nil {
t.Fatal(err)
}
legacyConfigPath, err := paths.New(src).Parent().Join("config.ini").Create()
if err != nil {
t.Fatal(err)
}
// adding a timestamp to the content to make it unique
legacyContent := "hostname = legacy-config-file-" + time.Now().String()
n, err := legacyConfigPath.WriteString(legacyContent)
if err != nil || n <= 0 {
t.Fatalf("Failed to write legacy config file: %v", err)
}

// remove any existing config.ini in the into the location pointed by $HOME
err = os.Remove("./testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini")
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}

// Expectation: it copies the "legacy" config.ini into the location pointed by $HOME
os.Setenv("HOME", "./testdata/fromlegacy")
defer os.Unsetenv("HOME")

configPath := GetConfigPath()
assert.Equal(t, "testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini", configPath.String())

given, err := paths.New(configPath.String()).ReadFile()
assert.Nil(t, err)
assert.Equal(t, legacyContent, string(given))
}

// func TestGetConfigPathCreateDefaultConfig(t *testing.T) {
// os.Setenv("HOME", "./testdata/noconfig")
// os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")

// // ensure the config.ini does not exist in HOME directory
// os.Remove("./testdata/noconfig/.config/ArduinoCreateAgent/config.ini")
// // ensure the config.ini does not exist in target directory
// os.Remove("./testdata/fromdefault/.config/ArduinoCreateAgent/config.ini")

// configPath := GetConfigPath()

// assert.Equal(t, "testdata/fromdefault/.config/ArduinoCreateAgent/config.ini", configPath.String())

// givenContent, err := paths.New(configPath.String()).ReadFile()
// if err != nil {
// t.Fatal(err)
// }

// assert.Equal(t, string(configContent), string(givenContent))

// }
1 change: 1 addition & 0 deletions config/testdata/fromdefault/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.ini
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 = this-is-a-config-file-from-home-dir-from-ARDUINO_CREATE_AGENT_CONFIG-env
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
gc = std
hostname = this-is-a-config-file-from-home-dir
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
2 changes: 2 additions & 0 deletions config/testdata/fromlegacy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ingore because this config file
config.ini
10 changes: 10 additions & 0 deletions config/testdata/fromxdghome/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)
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 = this-is-a-config-file-from-home-dir
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)
Loading
Loading