From f6dca52870a20ef1459e5a57e3cac9fd46d7919b Mon Sep 17 00:00:00 2001 From: Joshua Rich Date: Sun, 8 Dec 2024 17:58:21 +1000 Subject: [PATCH] fix: :bug: code cleanup missed in 7591c7ac1123bf409144c650a5cd47f8eb49ee07 --- internal/agent/register.go | 5 ----- internal/hass/registration.go | 8 +++---- internal/preferences/device.go | 36 ++++++++++++----------------- internal/preferences/hass.go | 41 ++++++++++++++++++++++++++-------- internal/preferences/mqtt.go | 27 +++++++++++++++++----- internal/upgrade/upgrade.go | 29 ++++++++++++++++-------- 6 files changed, 92 insertions(+), 54 deletions(-) diff --git a/internal/agent/register.go b/internal/agent/register.go index 44d1e25dd..6666d2d69 100644 --- a/internal/agent/register.go +++ b/internal/agent/register.go @@ -20,11 +20,6 @@ import ( var ErrUserCancelledRegistration = errors.New("user canceled registration") -type registrationPrefs interface { - AgentRegistered() bool - SaveHassPreferences(ctx context.Context, details *preferences.Hass, options *preferences.Registration) error -} - func checkRegistration(ctx context.Context, agentUI ui) error { if preferences.Registered() && !ForceRegister(ctx) { return nil diff --git a/internal/hass/registration.go b/internal/hass/registration.go index ccd6e3e42..75cf6be27 100644 --- a/internal/hass/registration.go +++ b/internal/hass/registration.go @@ -32,9 +32,9 @@ func (r *registrationRequest) RequestBody() any { return r.Device } -func newRegistrationRequest(device *device.Device, token string) *registrationRequest { +func newRegistrationRequest(thisDevice *device.Device, token string) *registrationRequest { return ®istrationRequest{ - Device: device, + Device: thisDevice, Token: token, } } @@ -46,10 +46,10 @@ func RegisterDevice(ctx context.Context, registration *preferences.Registration) } registrationURL := registration.Server + RegistrationPath - device := device.NewDevice(preferences.AppIDFromContext(ctx)) + thisDevice := device.NewDevice(preferences.AppIDFromContext(ctx)) // Register the device against the registration endpoint. - registrationStatus, err := api.Send[preferences.Hass](ctx, registrationURL, newRegistrationRequest(device, registration.Token)) + registrationStatus, err := api.Send[preferences.Hass](ctx, registrationURL, newRegistrationRequest(thisDevice, registration.Token)) if err != nil { return nil, fmt.Errorf("could not register device: %w", err) } diff --git a/internal/preferences/device.go b/internal/preferences/device.go index 4dcd3494f..77a270855 100644 --- a/internal/preferences/device.go +++ b/internal/preferences/device.go @@ -5,15 +5,27 @@ package preferences +import ( + "errors" + "fmt" +) + type Device struct { ID string `toml:"id" validate:"required,ascii"` Name string `toml:"name" validate:"required,ascii"` } +var ErrSetDevicePreference = errors.New("could not set device preference") + // SetDevicePreferences sets the device preferences. func SetDevicePreferences(device *Device) error { - prefsSrc.Set("device.id", device.ID) - prefsSrc.Set("device.name", device.Name) + if err := prefsSrc.Set("device.id", device.ID); err != nil { + return fmt.Errorf("%w: %w", ErrSetDevicePreference, err) + } + + if err := prefsSrc.Set("device.name", device.Name); err != nil { + return fmt.Errorf("%w: %w", ErrSetDevicePreference, err) + } return nil } @@ -27,23 +39,3 @@ func DeviceName() string { func DeviceID() string { return prefsSrc.String("device.id") } - -// func (p *Preferences) GetDeviceInfo() *Device { -// return p.Device -// } - -// func (p *Preferences) DeviceName() string { -// if p.Device != nil { -// return p.Device.Name -// } - -// return unknownValue -// } - -// func (p *Preferences) DeviceID() string { -// if p.Device != nil { -// return p.Device.ID -// } - -// return unknownValue -// } diff --git a/internal/preferences/hass.go b/internal/preferences/hass.go index 6ce26ca19..871da8607 100644 --- a/internal/preferences/hass.go +++ b/internal/preferences/hass.go @@ -26,32 +26,49 @@ type Hass struct { WebsocketURL string `toml:"websocketurl" json:"-" validate:"required,url"` } -var ErrSaveHassPreferences = errors.New("error saving hass preferences") +var ( + ErrSaveHassPreferences = errors.New("error saving hass preferences") + ErrSetHassPreference = errors.New("could not set hass preference") +) +//revive:disable:indent-error-flow func SetHassPreferences(hassPrefs *Hass, regPrefs *Registration) error { - prefsSrc.Set("hass.secret", hassPrefs.Secret) + if err := prefsSrc.Set("hass.secret", hassPrefs.Secret); err != nil { + return fmt.Errorf("%w: %w", ErrSetHassPreference, err) + } // Generate an API URL and set preferences as appropriate. if apiURL, err := generateAPIURL(hassPrefs, regPrefs); err != nil { return fmt.Errorf("%w: %w", ErrSaveHassPreferences, err) } else { - prefsSrc.Set("hass.apiurl", apiURL) + if err := prefsSrc.Set("hass.apiurl", apiURL); err != nil { + return fmt.Errorf("%w: %w", ErrSetHassPreference, err) + } } // Generate a websocket URL and set preferences as appropriate. if websocketURL, err := generateWebsocketURL(regPrefs); err != nil { return fmt.Errorf("%w: %w", ErrSaveHassPreferences, err) } else { - prefsSrc.Set("hass.websocketurl", websocketURL) + if err := prefsSrc.Set("hass.websocketurl", websocketURL); err != nil { + return fmt.Errorf("%w: %w", ErrSetHassPreference, err) + } } // Set the webhookID if present. if hassPrefs.WebhookID != "" { - prefsSrc.Set("hass.webhook_id", hassPrefs.WebhookID) + if err := prefsSrc.Set("hass.webhook_id", hassPrefs.WebhookID); err != nil { + return fmt.Errorf("%w: %w", ErrSetHassPreference, err) + } + } + + if err := prefsSrc.Set("registration.server", regPrefs.Server); err != nil { + return fmt.Errorf("%w: %w", ErrSetHassPreference, err) } - prefsSrc.Set("registration.server", regPrefs.Server) - prefsSrc.Set("registration.token", regPrefs.Token) + if err := prefsSrc.Set("registration.token", regPrefs.Token); err != nil { + return fmt.Errorf("%w: %w", ErrSetHassPreference, err) + } return nil } @@ -83,10 +100,16 @@ func Token() string { func generateAPIURL(hassPrefs *Hass, regPrefs *Registration) (string, error) { switch { case hassPrefs.CloudhookURL != "" && regPrefs.IgnoreHassURLs: - prefsSrc.Set("hass.cloudhook_url", hassPrefs.CloudhookURL) + if err := prefsSrc.Set("hass.cloudhook_url", hassPrefs.CloudhookURL); err != nil { + return "", fmt.Errorf("%w: %w", ErrSetHassPreference, err) + } + return hassPrefs.CloudhookURL, nil case hassPrefs.RemoteUIURL != "" && hassPrefs.WebhookID != "" && !regPrefs.IgnoreHassURLs: - prefsSrc.Set("hass.remote_ui_url", hassPrefs.CloudhookURL) + if err := prefsSrc.Set("hass.remote_ui_url", hassPrefs.CloudhookURL); err != nil { + return "", fmt.Errorf("%w: %w", ErrSetHassPreference, err) + } + return hassPrefs.RemoteUIURL + WebHookPath + hassPrefs.WebhookID, nil default: apiURL, err := url.Parse(regPrefs.Server) diff --git a/internal/preferences/mqtt.go b/internal/preferences/mqtt.go index 901327700..25e4131d5 100644 --- a/internal/preferences/mqtt.go +++ b/internal/preferences/mqtt.go @@ -6,6 +6,7 @@ package preferences import ( + "errors" "fmt" mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass" @@ -20,12 +21,28 @@ type MQTT struct { MQTTEnabled bool `toml:"enabled" validate:"boolean" kong:"-"` } +var ErrSetMQTTPreference = errors.New("could not set MQTT preference") + func SetMQTTPreferences(prefs *MQTT) error { - prefsSrc.Set("mqtt.server", prefs.MQTTServer) - prefsSrc.Set("mqtt.user", prefs.MQTTUser) - prefsSrc.Set("mqtt.password", prefs.MQTTPassword) - prefsSrc.Set("mqtt.topic_prefix", prefs.MQTTTopicPrefix) - prefsSrc.Set("mqtt.enabled", prefs.MQTTEnabled) + if err := prefsSrc.Set("mqtt.server", prefs.MQTTServer); err != nil { + return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) + } + + if err := prefsSrc.Set("mqtt.user", prefs.MQTTUser); err != nil { + return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) + } + + if err := prefsSrc.Set("mqtt.password", prefs.MQTTPassword); err != nil { + return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) + } + + if err := prefsSrc.Set("mqtt.topic_prefix", prefs.MQTTTopicPrefix); err != nil { + return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) + } + + if err := prefsSrc.Set("mqtt.enabled", prefs.MQTTEnabled); err != nil { + return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) + } return nil } diff --git a/internal/upgrade/upgrade.go b/internal/upgrade/upgrade.go index e74007261..afbc65940 100644 --- a/internal/upgrade/upgrade.go +++ b/internal/upgrade/upgrade.go @@ -73,23 +73,29 @@ func Run(ctx context.Context) error { return fmt.Errorf("cannot read old preferences: %w", err) } - if err := preferences.Load(ctx); err != nil && !errors.Is(err, preferences.ErrLoadPreferences) { + if err = preferences.Load(ctx); err != nil && !errors.Is(err, preferences.ErrLoadPreferences) { return fmt.Errorf("cannot initialize new preferences: %w", err) } - // Map old preferences to new preferences. - preferences.SetRegistered(oldPrefs.Registered) + // Registered status. + if err = preferences.SetRegistered(oldPrefs.Registered); err != nil { + return fmt.Errorf("cannot set new preferences: %w", err) + } + // MQTT preferences. if oldPrefs.MQTTEnabled { - preferences.SetMQTTPreferences(&preferences.MQTT{ + if err = preferences.SetMQTTPreferences(&preferences.MQTT{ MQTTEnabled: true, MQTTServer: oldPrefs.MQTTServer, MQTTUser: oldPrefs.MQTTUser, MQTTPassword: oldPrefs.MQTTPassword, MQTTTopicPrefix: oldPrefs.MQTTTopicPrefix, - }) + }); err != nil { + return fmt.Errorf("cannot set new preferences: %w", err) + } } - preferences.SetHassPreferences( + + if err = preferences.SetHassPreferences( // Hass preferences. &preferences.Hass{ Secret: oldPrefs.Secret, @@ -103,12 +109,17 @@ func Run(ctx context.Context) error { &preferences.Registration{ Server: oldPrefs.Host, Token: oldPrefs.Token, - }) + }); err != nil { + return fmt.Errorf("cannot set new preferences: %w", err) + } + // Device preferences. - preferences.SetDevicePreferences(&preferences.Device{ + if err = preferences.SetDevicePreferences(&preferences.Device{ Name: oldPrefs.DeviceName, ID: oldPrefs.DeviceID, - }) + }); err != nil { + return fmt.Errorf("cannot set new preferences: %w", err) + } err = preferences.Save(ctx) if err != nil {