Skip to content

Commit

Permalink
refactor(components/registry): 🔥 remove need for registry to fetch it…
Browse files Browse the repository at this point in the history
…s path from the context
  • Loading branch information
joshuar committed Jan 15, 2025
1 parent 5c8534c commit c12ac07
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 68 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"linux/power",
"linux/battery",
"hass/api",
"hass/discovery"
"hass/discovery",
"components/registry"
],
"go.testFlags": ["-v"],
"[markdown]": {
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/resetCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ 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.
if err := preferences.Reset(ctx); err != nil {
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))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/runCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
19 changes: 4 additions & 15 deletions internal/components/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
Expand Down
9 changes: 3 additions & 6 deletions internal/components/registry/registry_gob.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package registry

import (
"context"
"encoding/gob"
"errors"
"fmt"
Expand All @@ -15,8 +14,6 @@ import (
"os"
"path/filepath"
"sync"

"github.com/joshuar/go-hass-agent/internal/components/preferences"
)

const (
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 5 additions & 10 deletions internal/components/registry/registry_gob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@
package registry

import (
"context"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/joshuar/go-hass-agent/internal/components/preferences"
)

var mockSensors = map[string]metadata{
"disabledSensor": {Disabled: true, Registered: true},
"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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions internal/components/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
})
Expand Down
25 changes: 0 additions & 25 deletions internal/components/registry/state_generated.go

This file was deleted.

0 comments on commit c12ac07

Please sign in to comment.