60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
// Copyright (c) 2024 Joshua Rich <joshua.rich@gmail.com>
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
//revive:disable:unused-receiver
|
|
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/joshuar/go-hass-agent/internal/agent"
|
|
"github.com/joshuar/go-hass-agent/internal/hass/sensor/registry"
|
|
"github.com/joshuar/go-hass-agent/internal/logging"
|
|
"github.com/joshuar/go-hass-agent/internal/preferences"
|
|
)
|
|
|
|
type ResetCmd struct{}
|
|
|
|
func (r *ResetCmd) Help() string {
|
|
return showHelpTxt("reset-help")
|
|
}
|
|
|
|
func (r *ResetCmd) Run(opts *CmdOpts) error {
|
|
agentCtx, cancelFunc := newContext(opts)
|
|
defer cancelFunc()
|
|
|
|
var errs error
|
|
|
|
agentCtx = agent.LoadCtx(agentCtx,
|
|
agent.SetHeadless(opts.Headless))
|
|
|
|
// Reset agent.
|
|
if err := agent.Reset(agentCtx); err != nil {
|
|
errs = errors.Join(fmt.Errorf("agent reset failed: %w", err))
|
|
}
|
|
// Reset registry.
|
|
if err := registry.Reset(agentCtx); err != nil {
|
|
errs = errors.Join(fmt.Errorf("registry reset failed: %w", err))
|
|
}
|
|
// Reset preferences.
|
|
if err := preferences.Reset(agentCtx); err != nil {
|
|
errs = errors.Join(fmt.Errorf("preferences reset failed: %w", err))
|
|
}
|
|
// Reset the log.
|
|
if err := logging.Reset(agentCtx); err != nil {
|
|
errs = errors.Join(fmt.Errorf("logging reset failed: %w", err))
|
|
}
|
|
|
|
if errs != nil {
|
|
slog.Warn("Reset completed with errors", slog.Any("errors", errs))
|
|
} else {
|
|
slog.Info("Reset completed.")
|
|
}
|
|
|
|
return nil
|
|
}
|