From 84422d93929c4fad8451f9faeab12d76f7e14156 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 1 Jun 2026 23:20:02 +0000 Subject: [PATCH 1/3] Initial plan From 195a17f6fc53a3b483a10d01f699ac492042bc11 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 1 Jun 2026 23:23:07 +0000 Subject: [PATCH 2/3] feat: add config loading and default startup config lookup --- config.go | 43 +++++++++++++++++++++++++++++++++---- config_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ redeye/main.go | 4 ++++ 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 2373e2b..c75f46b 100644 --- a/config.go +++ b/config.go @@ -1,17 +1,19 @@ package redeye import ( + "errors" "fmt" "encoding/json" - "os" "net/http" + "os" + "path/filepath" ) type Configuration struct { - HTTPAddr string `json:"addr"` // http address and port - HTMLPath string `json:"basepath"` // html basepath - MQTTBroker string `json:"broker"` // MQTT Broker + HTTPAddr string `json:"addr"` // http address and port + HTMLPath string `json:"basepath"` // html basepath + MQTTBroker string `json:"broker"` // MQTT Broker VideoDevice int `json:"video-device"` // Capture device Image string `json:"image"` // Single image Video string `json:"video"` @@ -48,6 +50,39 @@ func (c *Configuration) Save(path string) (err error) { return err } +func (c *Configuration) Load(path string) error { + buf, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("Config Load [%s] failed to read file: [%w]", path, err) + } + + if err := json.Unmarshal(buf, c); err != nil { + return fmt.Errorf("Config Load [%s] failed json.Unmarshal config [%w]", path, err) + } + + return nil +} + +func (c *Configuration) LoadDefault() error { + paths := []string{"redeye.json"} + if homeDir, err := os.UserHomeDir(); err == nil { + paths = append(paths, filepath.Join(homeDir, ".redeye.json")) + } + + for _, path := range paths { + err := c.Load(path) + if errors.Is(err, os.ErrNotExist) { + continue + } + if err != nil { + return err + } + return nil + } + + return nil +} + // ServeHTTP provides the Web service for the configuration module func (c Configuration) ServeHTTP(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(c) diff --git a/config_test.go b/config_test.go index 6668123..a252f1a 100644 --- a/config_test.go +++ b/config_test.go @@ -41,6 +41,63 @@ func TestConfigurationSaveError(t *testing.T) { assert.Contains(t, err.Error(), "failed to save file") } +func TestConfigurationLoad(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + require.NoError(t, os.WriteFile(path, []byte(`{"addr":"127.0.0.1:9000","video-device":5}`), 0o644)) + + cfg := &Configuration{} + require.NoError(t, cfg.Load(path)) + assert.Equal(t, "127.0.0.1:9000", cfg.HTTPAddr) + assert.Equal(t, 5, cfg.VideoDevice) +} + +func TestConfigurationLoadError(t *testing.T) { + cfg := &Configuration{} + err := cfg.Load(filepath.Join(t.TempDir(), "missing.json")) + require.Error(t, err) + assert.Contains(t, err.Error(), "failed to read file") +} + +func TestConfigurationLoadDefault(t *testing.T) { + origWD, err := os.Getwd() + require.NoError(t, err) + t.Cleanup(func() { _ = os.Chdir(origWD) }) + + tempWD := t.TempDir() + require.NoError(t, os.Chdir(tempWD)) + + homeDir := t.TempDir() + t.Setenv("HOME", homeDir) + + localPath := filepath.Join(tempWD, "redeye.json") + homePath := filepath.Join(homeDir, ".redeye.json") + require.NoError(t, os.WriteFile(localPath, []byte(`{"addr":"127.0.0.1:10000"}`), 0o644)) + require.NoError(t, os.WriteFile(homePath, []byte(`{"addr":"127.0.0.1:20000"}`), 0o644)) + + cfg := &Configuration{} + require.NoError(t, cfg.LoadDefault()) + assert.Equal(t, "127.0.0.1:10000", cfg.HTTPAddr) +} + +func TestConfigurationLoadDefaultFallsBackToHome(t *testing.T) { + origWD, err := os.Getwd() + require.NoError(t, err) + t.Cleanup(func() { _ = os.Chdir(origWD) }) + + tempWD := t.TempDir() + require.NoError(t, os.Chdir(tempWD)) + + homeDir := t.TempDir() + t.Setenv("HOME", homeDir) + + homePath := filepath.Join(homeDir, ".redeye.json") + require.NoError(t, os.WriteFile(homePath, []byte(`{"addr":"127.0.0.1:20000"}`), 0o644)) + + cfg := &Configuration{} + require.NoError(t, cfg.LoadDefault()) + assert.Equal(t, "127.0.0.1:20000", cfg.HTTPAddr) +} + func TestConfigurationServeHTTP(t *testing.T) { cfg := Configuration{HTTPAddr: "0.0.0.0:8080", Debug: true} req := httptest.NewRequest(http.MethodGet, "/config", nil) diff --git a/redeye/main.go b/redeye/main.go index e692914..c867f99 100644 --- a/redeye/main.go +++ b/redeye/main.go @@ -30,6 +30,10 @@ func init() { } func main() { + if err := config.LoadDefault(); err != nil { + log.Fatalf("failed to load default config: %+v", err) + } + flag.Parse() // list filters and exit if command list says so From bd5a39f4ee4f1c420f1489b6e7ea4a1b0d6bfa4a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 1 Jun 2026 23:23:59 +0000 Subject: [PATCH 3/3] chore: complete validation for config load changes --- go.mod | 3 +-- go.sum | 12 ++++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index f8f155f..08eac96 100644 --- a/go.mod +++ b/go.mod @@ -7,9 +7,8 @@ toolchain go1.24.13 require ( github.com/eclipse/paho.mqtt.golang v1.5.1 github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e - - gocv.io/x/gocv v0.39.0 github.com/stretchr/testify v1.10.0 + gocv.io/x/gocv v0.39.0 nhooyr.io/websocket v1.8.17 ) diff --git a/go.sum b/go.sum index 06ce741..fdc81e3 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +1,24 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE= +github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e h1:xCcwD5FOXul+j1dn8xD16nbrhJkkum/Cn+jTd/u1LhY= github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -gocv.io/x/gocv v0.36.1 h1:6XkEaPOk7h/umjy+MXgSEtSeCIgcPJhccUjrJFhjdTY= -gocv.io/x/gocv v0.36.1/go.mod h1:lmS802zoQmnNvXETpmGriBqWrENPei2GxYx5KUxJsMA= gocv.io/x/gocv v0.39.0 h1:vWHupDE22LebZW6id2mVeT767j1YS8WqGt+ZiV7XJXE= gocv.io/x/gocv v0.39.0/go.mod h1:zYdWMj29WAEznM3Y8NsU3A0TRq/wR/cy75jeUypThqU= +golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I= +golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY= +golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= +golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y= +nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=