All checks were successful
Linting and tests / Linting (push) Successful in 5s
100 lines
2.1 KiB
Go
100 lines
2.1 KiB
Go
package options
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"bunker/client/internal/application"
|
|
"bunker/client/internal/services/core"
|
|
"bunker/client/internal/services/core/options/models"
|
|
)
|
|
|
|
var (
|
|
_ = core.Options(&options{})
|
|
|
|
errOptions = errors.New("options core service")
|
|
)
|
|
|
|
type options struct {
|
|
app *application.Application
|
|
logger *slog.Logger
|
|
db core.Database
|
|
mainWindow core.MainWindow
|
|
|
|
widgets map[string]*models.OptionPane
|
|
widgetsItems []string // for Fyne's list widget.
|
|
}
|
|
|
|
// Initialize initializes service.
|
|
func Initialize(app *application.Application) error {
|
|
opts := &options{
|
|
app: app,
|
|
}
|
|
|
|
if err := app.RegisterService(opts); err != nil {
|
|
return fmt.Errorf("%w: %w", errOptions, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o *options) Configure() error {
|
|
if err := o.registerMigrations(); err != nil {
|
|
return fmt.Errorf("configure: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o *options) ConnectDependencies() error {
|
|
databaseRaw := o.app.Service(core.ServiceNameDatabase)
|
|
if databaseRaw == nil {
|
|
return fmt.Errorf("connect dependencies: get database service: %w", application.ErrServiceNotFound)
|
|
}
|
|
|
|
database, valid := databaseRaw.(core.Database)
|
|
if !valid {
|
|
return fmt.Errorf("connect dependencies: type assert database service: %w", core.ErrDatabaseIsInvalid)
|
|
}
|
|
|
|
o.db = database
|
|
|
|
mainWindowRaw := o.app.Service(core.ServiceNameMainWindow)
|
|
if mainWindowRaw == nil {
|
|
return fmt.Errorf("connect dependencies: get main window: %w", application.ErrServiceNotFound)
|
|
}
|
|
|
|
mainWindow, valid := mainWindowRaw.(core.MainWindow)
|
|
if !valid {
|
|
return fmt.Errorf("connect dependencies: type assert main window: %w", core.ErrMainWindowIsInvalid)
|
|
}
|
|
|
|
o.mainWindow = mainWindow
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o *options) Initialize() error {
|
|
o.logger = o.app.NewLogger("service", core.ServiceNameOptions)
|
|
|
|
o.logger.Info("Initializing...")
|
|
|
|
o.widgets = make(map[string]*models.OptionPane)
|
|
o.widgetsItems = make([]string, 0)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o *options) Name() string {
|
|
return core.ServiceNameOptions
|
|
}
|
|
|
|
func (o *options) LaunchStartupTasks() error {
|
|
return nil
|
|
}
|
|
|
|
func (o *options) Shutdown() error {
|
|
return nil
|
|
}
|