Skip to content

Commit

Permalink
fix(agent): 🐛 don't bail on registration if no preferences file was f…
Browse files Browse the repository at this point in the history
…ound but default preferences were returned
  • Loading branch information
joshuar committed Jun 13, 2024
1 parent aeca410 commit 74b52f1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 1 addition & 2 deletions internal/agent/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"net/url"
"os"

"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -103,7 +102,7 @@ func (agent *Agent) performRegistration(ctx context.Context) error {

func (agent *Agent) checkRegistration(trk SensorTracker) error {
prefs, err := preferences.Load()
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, preferences.ErrNoPreferences) {
return fmt.Errorf("could not load preferences: %w", err)
}

Expand Down
8 changes: 6 additions & 2 deletions internal/preferences/prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package preferences

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -39,6 +40,9 @@ var (
var (
preferencesPath = filepath.Join(xdg.ConfigHome, "go-hass-agent")
preferencesFile = "preferences.toml"

ErrNoPreferences = errors.New("no preferences file found, using defaults")
ErrFileContents = errors.New("could not read file contents")
)

//nolint:tagalign
Expand Down Expand Up @@ -273,12 +277,12 @@ func Load() (*Preferences, error) {

b, err := os.ReadFile(file)
if err != nil {
return prefs, fmt.Errorf("could not read preferences file: %w", err)
return prefs, errors.Join(ErrNoPreferences, err)
}

err = toml.Unmarshal(b, &prefs)
if err != nil {
return prefs, fmt.Errorf("could not parse preferences file: %w", err)
return prefs, errors.Join(ErrFileContents, err)
}

return prefs, nil
Expand Down

0 comments on commit 74b52f1

Please sign in to comment.