From c12ac07fad26d23013ee6ad7881f8a3544b3bc39 Mon Sep 17 00:00:00 2001 From: Joshua Rich Date: Wed, 15 Jan 2025 22:33:34 +1000 Subject: [PATCH] refactor(components/registry): :fire: remove need for registry to fetch its path from the context --- .vscode/settings.json | 3 ++- internal/agent/register.go | 2 +- internal/cli/resetCmd.go | 4 +-- internal/cli/runCmd.go | 2 +- internal/components/registry/registry.go | 19 +++----------- internal/components/registry/registry_gob.go | 9 +++---- .../components/registry/registry_gob_test.go | 15 ++++------- internal/components/registry/registry_test.go | 9 ++----- .../components/registry/state_generated.go | 25 ------------------- 9 files changed, 20 insertions(+), 68 deletions(-) delete mode 100644 internal/components/registry/state_generated.go diff --git a/.vscode/settings.json b/.vscode/settings.json index 6dfc89332..e0844c933 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -37,7 +37,8 @@ "linux/power", "linux/battery", "hass/api", - "hass/discovery" + "hass/discovery", + "components/registry" ], "go.testFlags": ["-v"], "[markdown]": { diff --git a/internal/agent/register.go b/internal/agent/register.go index 5521d5e04..811579f82 100644 --- a/internal/agent/register.go +++ b/internal/agent/register.go @@ -48,7 +48,7 @@ func checkRegistration(ctx context.Context, agentUI ui) error { // If the registration was forced, reset the sensor registry. if request.ForceRegister { - if err := registry.Reset(ctx); err != nil { + if err := registry.Reset(preferences.PathFromCtx(ctx)); err != nil { logging.FromContext(ctx).Warn("Problem resetting registry.", slog.Any("error", err)) } diff --git a/internal/cli/resetCmd.go b/internal/cli/resetCmd.go index a90a2c4a1..c39df769a 100644 --- a/internal/cli/resetCmd.go +++ b/internal/cli/resetCmd.go @@ -50,7 +50,7 @@ func (r *ResetCmd) Run(opts *CmdOpts) error { errs = errors.Join(fmt.Errorf("agent reset failed: %w", err)) } // Reset registry. - if err := registry.Reset(ctx); err != nil { + if err := registry.Reset(opts.Path); err != nil { errs = errors.Join(fmt.Errorf("registry reset failed: %w", err)) } // Reset preferences. @@ -58,7 +58,7 @@ func (r *ResetCmd) Run(opts *CmdOpts) error { errs = errors.Join(fmt.Errorf("preferences reset failed: %w", err)) } // Reset the log. - if err := logging.Reset(preferences.PathFromCtx(ctx)); err != nil { + if err := logging.Reset(opts.Path); err != nil { errs = errors.Join(fmt.Errorf("logging reset failed: %w", err)) } diff --git a/internal/cli/runCmd.go b/internal/cli/runCmd.go index 9f6194c18..35ff05472 100644 --- a/internal/cli/runCmd.go +++ b/internal/cli/runCmd.go @@ -45,7 +45,7 @@ func (r *RunCmd) Run(opts *CmdOpts) error { } // Load the registry. - reg, err := registry.Load(ctx) + reg, err := registry.Load(opts.Path) if err != nil { return errors.Join(ErrRunCmdFailed, err) } diff --git a/internal/components/registry/registry.go b/internal/components/registry/registry.go index de411641d..db9216349 100644 --- a/internal/components/registry/registry.go +++ b/internal/components/registry/registry.go @@ -6,23 +6,12 @@ package registry import ( - "context" "errors" "fmt" "os" "path/filepath" - - "github.com/joshuar/go-hass-agent/internal/components/preferences" -) - -//go:generate go run golang.org/x/tools/cmd/stringer -type=state -output state_generated.go -linecomment -const ( - disabledState state = iota + 1 // disabled - registeredState // registered ) -type state int - var ( ErrNotFound = errors.New("sensor not found") ErrInvalidMetadata = errors.New("invalid sensor metadata") @@ -34,15 +23,15 @@ type metadata struct { } // Reset will handle resetting the registry. -func Reset(ctx context.Context) error { - path := filepath.Join(preferences.PathFromCtx(ctx), "sensorRegistry") +func Reset(registryPath string) error { + registryPath = filepath.Join(registryPath, "sensorRegistry") - _, err := os.Stat(path) + _, err := os.Stat(registryPath) if os.IsNotExist(err) { return fmt.Errorf("registry not found: %w", err) } - err = os.RemoveAll(path) + err = os.RemoveAll(registryPath) if err != nil { return fmt.Errorf("failed to remove registry: %w", err) } diff --git a/internal/components/registry/registry_gob.go b/internal/components/registry/registry_gob.go index ecd8e9e57..f86cdb9ae 100644 --- a/internal/components/registry/registry_gob.go +++ b/internal/components/registry/registry_gob.go @@ -6,7 +6,6 @@ package registry import ( - "context" "encoding/gob" "errors" "fmt" @@ -15,8 +14,6 @@ import ( "os" "path/filepath" "sync" - - "github.com/joshuar/go-hass-agent/internal/components/preferences" ) const ( @@ -124,13 +121,13 @@ func (g *gobRegistry) SetRegistered(id string, value bool) error { } //revive:disable:unexported-return -func Load(ctx context.Context) (*gobRegistry, error) { - path := filepath.Join(preferences.PathFromCtx(ctx), "sensorRegistry", registryFile) +func Load(path string) (*gobRegistry, error) { + registryPath := filepath.Join(path, "sensorRegistry", registryFile) reg := &gobRegistry{ sensors: make(map[string]metadata), mu: sync.Mutex{}, - file: path, + file: registryPath, } if err := checkPath(filepath.Dir(reg.file)); err != nil { diff --git a/internal/components/registry/registry_gob_test.go b/internal/components/registry/registry_gob_test.go index 0a6a4bc6d..af105afb7 100644 --- a/internal/components/registry/registry_gob_test.go +++ b/internal/components/registry/registry_gob_test.go @@ -7,7 +7,6 @@ package registry import ( - "context" "os" "path/filepath" "reflect" @@ -15,8 +14,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - "github.com/joshuar/go-hass-agent/internal/components/preferences" ) var mockSensors = map[string]metadata{ @@ -24,10 +21,10 @@ var mockSensors = map[string]metadata{ "registeredSensor": {Disabled: false, Registered: true}, } -func newMockReg(ctx context.Context, t *testing.T) *gobRegistry { +func newMockReg(t *testing.T, path string) *gobRegistry { t.Helper() - mockReg, err := Load(ctx) + mockReg, err := Load(path) require.NoError(t, err) mockReg.sensors = mockSensors err = mockReg.write() @@ -62,15 +59,14 @@ func Test_gobRegistry_write(t *testing.T) { file: tt.fields.file, } if err := g.write(); (err != nil) != tt.wantErr { - t.Errorf("gobRegistry.write() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("gobRegistry.write() error c= %v, wantErr %v", err, tt.wantErr) } }) } } func Test_gobRegistry_read(t *testing.T) { - ctx := preferences.PathToCtx(context.TODO(), t.TempDir()) - mockReg := newMockReg(ctx, t) + mockReg := newMockReg(t, t.TempDir()) invalidRegistry := filepath.Join(t.TempDir(), registryFile) err := os.WriteFile(invalidRegistry, []byte(`invalid`), 0o600) @@ -323,8 +319,7 @@ func TestLoad(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctx := preferences.PathToCtx(context.TODO(), tt.args.path) - got, err := Load(ctx) + got, err := Load(tt.args.path) if (err != nil) != tt.wantErr { t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/internal/components/registry/registry_test.go b/internal/components/registry/registry_test.go index f419ba462..8bc608dad 100644 --- a/internal/components/registry/registry_test.go +++ b/internal/components/registry/registry_test.go @@ -7,17 +7,13 @@ package registry import ( - "context" "path/filepath" "testing" - - "github.com/joshuar/go-hass-agent/internal/components/preferences" ) func TestReset(t *testing.T) { validPath := t.TempDir() - ctx := preferences.PathToCtx(context.TODO(), validPath) - newMockReg(ctx, t) + newMockReg(t, validPath) type args struct { path string @@ -39,8 +35,7 @@ func TestReset(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctx := preferences.PathToCtx(context.TODO(), tt.args.path) - if err := Reset(ctx); (err != nil) != tt.wantErr { + if err := Reset(tt.args.path); (err != nil) != tt.wantErr { t.Errorf("Reset() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/internal/components/registry/state_generated.go b/internal/components/registry/state_generated.go deleted file mode 100644 index 0202047dc..000000000 --- a/internal/components/registry/state_generated.go +++ /dev/null @@ -1,25 +0,0 @@ -// Code generated by "stringer -type=state -output state_generated.go -linecomment"; DO NOT EDIT. - -package registry - -import "strconv" - -func _() { - // An "invalid array index" compiler error signifies that the constant values have changed. - // Re-run the stringer command to generate them again. - var x [1]struct{} - _ = x[disabledState-1] - _ = x[registeredState-2] -} - -const _state_name = "disabledregistered" - -var _state_index = [...]uint8{0, 8, 18} - -func (i state) String() string { - i -= 1 - if i < 0 || i >= state(len(_state_index)-1) { - return "state(" + strconv.FormatInt(int64(i+1), 10) + ")" - } - return _state_name[_state_index[i]:_state_index[i+1]] -}