Restructures configuration for birthdays and adds pictures directory
This commit is contained in:
parent
c7399eb9da
commit
ec2cdfdeaa
9 changed files with 55 additions and 31 deletions
|
@ -28,7 +28,7 @@ func testConfig(t *testing.T) *model.Config {
|
|||
require.NoError(t, f.Close())
|
||||
require.NoError(t, os.Remove(f.Name()))
|
||||
|
||||
return &model.Config{BirthdayFile: f.Name()}
|
||||
return &model.Config{Birthdays: &model.BirthdaysConfig{File: f.Name()}}
|
||||
}
|
||||
|
||||
func SetupTestHelper(t *testing.T) *TestHelper {
|
||||
|
|
|
@ -10,7 +10,7 @@ type Option func(*Server) *Server
|
|||
|
||||
func WithConfig(config *model.Config) Option {
|
||||
return func(server *Server) *Server {
|
||||
server.config = config
|
||||
server.Config = config
|
||||
return server
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,18 +19,18 @@ var (
|
|||
|
||||
type Server struct {
|
||||
Logger *log.Logger
|
||||
config *model.Config
|
||||
Config *model.Config
|
||||
workers []Worker
|
||||
birthdays []*model.Birthday
|
||||
notificationServices []notification.NotificationService
|
||||
tmpl *template.Template
|
||||
}
|
||||
|
||||
func createNotificationServices(logger *log.Logger, config *model.Config) ([]notification.NotificationService, error) {
|
||||
func (s *Server) createNotificationServices() ([]notification.NotificationService, error) {
|
||||
notificationServices := []notification.NotificationService{}
|
||||
|
||||
if config.TelegramNotifications != nil {
|
||||
telegramNotificationService, err := notification.NewTelegramNotificationService(logger, config.TelegramNotifications)
|
||||
if s.Config.TelegramNotifications != nil {
|
||||
telegramNotificationService, err := notification.NewTelegramNotificationService(s.Logger, s.Config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create telegram notification service: %w", err)
|
||||
}
|
||||
|
@ -54,18 +54,18 @@ func New(options ...Option) (*Server, error) {
|
|||
return nil, ErrNoLogger
|
||||
}
|
||||
|
||||
if srv.config == nil {
|
||||
if srv.Config == nil {
|
||||
return nil, ErrNoConfig
|
||||
}
|
||||
|
||||
if len(srv.birthdays) == 0 {
|
||||
srv.Logger.Debug("parsing CSV file", "birthdayFile", srv.config.BirthdayFile)
|
||||
srv.Logger.Debug("parsing CSV file", "birthdayFile", srv.Config.Birthdays.File)
|
||||
|
||||
var err error
|
||||
srv.birthdays, err = parser.ParseCSV(srv.config.BirthdayFile)
|
||||
srv.birthdays, err = parser.ParseCSV(srv.Config.Birthdays.File)
|
||||
if err != nil {
|
||||
srv.Logger.Error("cannot parse CSV file", "birthdayFile", srv.config.BirthdayFile, "error", err)
|
||||
return nil, fmt.Errorf("cannot parse CSV file %s: %w", srv.config.BirthdayFile, err)
|
||||
srv.Logger.Error("cannot parse CSV file", "birthdayFile", srv.Config.Birthdays.File, "error", err)
|
||||
return nil, fmt.Errorf("cannot parse CSV file %s: %w", srv.Config.Birthdays.File, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@ func New(options ...Option) (*Server, error) {
|
|||
srv.Logger.Debug("creating notification services from config")
|
||||
|
||||
var err error
|
||||
srv.notificationServices, err = createNotificationServices(srv.Logger, srv.config)
|
||||
srv.notificationServices, err = srv.createNotificationServices()
|
||||
if err != nil {
|
||||
srv.Logger.Error("error creating notification services", "error", err)
|
||||
return nil, err
|
||||
|
@ -86,13 +86,13 @@ func New(options ...Option) (*Server, error) {
|
|||
srv.workers = []Worker{NewSimpleWorker(srv)}
|
||||
}
|
||||
|
||||
if srv.config.BirthdayTemplate != "" {
|
||||
srv.Logger.Debug("parsing birthday template", "file", srv.config.BirthdayTemplate)
|
||||
if srv.Config.Birthdays.Template != "" {
|
||||
srv.Logger.Debug("parsing birthday template", "file", srv.Config.Birthdays.Template)
|
||||
|
||||
var err error
|
||||
srv.tmpl, err = template.ParseFiles(srv.config.BirthdayTemplate)
|
||||
srv.tmpl, err = template.ParseFiles(srv.Config.Birthdays.Template)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse template file %q: %w", srv.config.BirthdayTemplate, err)
|
||||
return nil, fmt.Errorf("cannot parse template file %q: %w", srv.Config.Birthdays.Template, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ func TestNotify(t *testing.T) {
|
|||
birthday := th.srv.birthdays[0]
|
||||
th.mockNotificationService.
|
||||
EXPECT().
|
||||
Notify(birthday).
|
||||
Notify(birthday, nil).
|
||||
Return(nil).
|
||||
Times(1)
|
||||
|
||||
|
@ -27,7 +27,7 @@ func TestNotify(t *testing.T) {
|
|||
birthday := th.srv.birthdays[0]
|
||||
th.mockNotificationService.
|
||||
EXPECT().
|
||||
Notify(birthday).
|
||||
Notify(birthday, nil).
|
||||
Return(mockErr).
|
||||
Times(1)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue