diff --git a/.gitignore b/.gitignore index fc2e460..0e9e508 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist -config.yml \ No newline at end of file +config.yml +pics \ No newline at end of file diff --git a/example-config.yml b/example-config.yml index ec2ed99..34d1a22 100644 --- a/example-config.yml +++ b/example-config.yml @@ -1,6 +1,8 @@ --- -birthday_file: birthdays.csv -birthday_template: ./birthday_message.tmpl +birthdays: + file: birthdays.csv + template: ./birthday_message.tmpl + pictures: ./pics logger: level: debug diff --git a/model/config.go b/model/config.go index c90e06d..b7d563c 100644 --- a/model/config.go +++ b/model/config.go @@ -10,22 +10,21 @@ import ( ) var ( - ErrConfigBirthdayFileEmpty = errors.New("birthday file cannot be empty") + ErrBirthdaysConfigFileEmpty = errors.New("birthday file cannot be empty") ErrLoggerConfigBadLevel = errors.New("logger level needs to be one of debug, info, warn, error, fatal or left empty") ErrTelegramNotificationsConfigEmptyBotToken = errors.New("bot token cannot be empty") ErrTelegramNotificationsConfigEmptyChannelID = errors.New("channel ID cannot be empty") ) type Config struct { - BirthdayFile string `yaml:"birthday_file"` - BirthdayTemplate string `yaml:"birthday_template"` + Birthdays *BirthdaysConfig `yaml:"birthdays"` Logger *LoggerConfig `yaml:"logger"` TelegramNotifications *TelegramNotificationsConfig `yaml:"telegram_notifications"` } func (c *Config) IsValid() error { - if c.BirthdayFile == "" { - return ErrConfigBirthdayFileEmpty + if err := c.Birthdays.IsValid(); err != nil { + return fmt.Errorf("invalid birthdays config: %w", err) } if err := c.Logger.IsValid(); err != nil { @@ -42,6 +41,12 @@ func (c *Config) IsValid() error { } func (c *Config) SetDefaults() { + if c.Birthdays == nil { + c.Birthdays = &BirthdaysConfig{} + } + + c.Birthdays.SetDefaults() + if c.Logger == nil { c.Logger = &LoggerConfig{} } @@ -53,6 +58,22 @@ func (c *Config) SetDefaults() { } } +type BirthdaysConfig struct { + File string `yaml:"file"` + Template string `yaml:"template"` + Picures string `yaml:"pictures"` +} + +func (bc *BirthdaysConfig) IsValid() error { + if bc.File == "" { + return ErrBirthdaysConfigFileEmpty + } + + return nil +} + +func (bc *BirthdaysConfig) SetDefaults() {} + type LoggerConfig struct { Level string `yaml:"level"` ReportCaller bool `yaml:"report_caller"` diff --git a/model/config_test.go b/model/config_test.go index 3d9433a..b744133 100644 --- a/model/config_test.go +++ b/model/config_test.go @@ -15,13 +15,13 @@ func TestReadConfig(t *testing.T) { require.NoError(t, err) defer os.Remove(f.Name()) - _, werr := io.WriteString(f, "---\nbirthday_file: birthday.csv") + _, werr := io.WriteString(f, "---\nbirthdays:\n file: birthday.csv") require.NoError(t, werr) require.NoError(t, f.Close()) config, err := ReadConfig(f.Name()) require.NoError(t, err) - require.Equal(t, "birthday.csv", config.BirthdayFile) + require.Equal(t, "birthday.csv", config.Birthdays.File) }) t.Run("should fail if the file doesn't exist", func(t *testing.T) { diff --git a/notification/service_telegram.go b/notification/service_telegram.go index 4c94465..82d363d 100644 --- a/notification/service_telegram.go +++ b/notification/service_telegram.go @@ -13,12 +13,12 @@ import ( type TelegramNotificationService struct { logger *log.Logger - config *model.TelegramNotificationsConfig + config *model.Config bot *tgbotapi.BotAPI } -func NewTelegramNotificationService(logger *log.Logger, config *model.TelegramNotificationsConfig) (*TelegramNotificationService, error) { - bot, err := tgbotapi.NewBotAPI(config.BotToken) +func NewTelegramNotificationService(logger *log.Logger, config *model.Config) (*TelegramNotificationService, error) { + bot, err := tgbotapi.NewBotAPI(config.TelegramNotifications.BotToken) if err != nil { return nil, fmt.Errorf("cannot create bot: %w", err) } diff --git a/server/helpers_test.go b/server/helpers_test.go index 0fb7bd7..1cdc576 100644 --- a/server/helpers_test.go +++ b/server/helpers_test.go @@ -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 { diff --git a/server/options.go b/server/options.go index 1d5b618..a9c5cc7 100644 --- a/server/options.go +++ b/server/options.go @@ -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 } } diff --git a/server/server.go b/server/server.go index 2811a7e..ead52aa 100644 --- a/server/server.go +++ b/server/server.go @@ -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) } } diff --git a/server/server_test.go b/server/server_test.go index c54a82b..6d8d5d9 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -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)