Adds server options

This commit is contained in:
Miguel de la Cruz 2023-07-04 12:48:16 +02:00
parent dee94e838d
commit 6c14403ff4
4 changed files with 59 additions and 23 deletions

View file

@ -34,12 +34,13 @@ $ make run
- [X] Define how to read the birthdays info - [X] Define how to read the birthdays info
- [X] Add a logger instance to the config - [X] Add a logger instance to the config
- [ ] Add config validation on server creation - [ ] Add config validation on server creation
- [ ] Add options to the server - [X] Add options to the server
- [ ] Pass logger to the server through an option - [X] Pass logger to the server through an option
- [ ] Configure logger through config (levels and such)
- [ ] Reduce logger verbosity (through levels)
- [ ] Create a configurable template to fill with each notification - [ ] Create a configurable template to fill with each notification
- [ ] Create different message systems to use with the bot - [ ] Create different message systems to use with the bot
- [X] Telegram - [X] Telegram
- [ ] Email - [ ] Email
- [ ] Mattermost - [ ] Mattermost
- [ ] Reduce logger verbosity
- [ ] Enjoy! - [ ] Enjoy!

View file

@ -11,24 +11,35 @@ import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
) )
func configureLogger(config *model.Config, logger *log.Logger) {
// ToDo: apply config to logger
}
func main() { func main() {
configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file") configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file")
flag.Parse() flag.Parse()
logger := log.New(os.Stderr)
config, err := model.ReadConfig(*configFlag) config, err := model.ReadConfig(*configFlag)
if err != nil { if err != nil {
log.Error("error reading config", "configPath", *configFlag, "error", err) logger.Error("error reading config", "configPath", *configFlag, "error", err)
os.Exit(1) os.Exit(1)
} }
if err := config.IsValid(); err != nil { if err := config.IsValid(); err != nil {
log.Error("invalid config", "configPath", *configFlag, "error", err) logger.Error("invalid config", "configPath", *configFlag, "error", err)
os.Exit(1) os.Exit(1)
} }
srv, err := server.New(config) configureLogger(config, logger)
srv, err := server.New(
server.WithLogger(logger),
server.WithConfig(config),
)
if err != nil { if err != nil {
log.Error("error creating server", "error", err) logger.Error("error creating server", "error", err)
os.Exit(1) os.Exit(1)
} }

22
server/options.go Normal file
View file

@ -0,0 +1,22 @@
package server
import (
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"github.com/charmbracelet/log"
)
type Option func(*Server) *Server
func WithConfig(config *model.Config) Option {
return func(server *Server) *Server {
server.config = config
return server
}
}
func WithLogger(logger *log.Logger) Option {
return func(server *Server) *Server {
server.Logger = logger
return server
}
}

View file

@ -3,7 +3,6 @@ package server
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"git.ctrlz.es/mgdelacroix/birthdaybot/model" "git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/notification" "git.ctrlz.es/mgdelacroix/birthdaybot/notification"
@ -39,30 +38,33 @@ func createNotificationServices(logger *log.Logger, config *model.Config) ([]not
return notificationServices, nil return notificationServices, nil
} }
func New(config *model.Config) (*Server, error) { func New(options ...Option) (*Server, error) {
logger := log.New(os.Stderr) srv := &Server{}
for _, option := range options {
logger.Debug("parsing CSV file", "birthdayFile", config.BirthdayFile) srv = option(srv)
birthdays, err := parser.ParseCSV(config.BirthdayFile)
if err != nil {
logger.Error("cannot parse CSV file", "birthdayFile", config.BirthdayFile, "error", err)
return nil, fmt.Errorf("cannot parse CSV file %s: %w", config.BirthdayFile, err)
} }
logger.Debug("creating notification services from config") srv.Logger.Debug("parsing CSV file", "birthdayFile", srv.config.BirthdayFile)
notificationServices, err := createNotificationServices(logger, config) birthdays, err := parser.ParseCSV(srv.config.BirthdayFile)
if err != nil { if err != nil {
logger.Error("error creating notification services", "error", err) 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.Debug("creating notification services from config")
notificationServices, err := createNotificationServices(srv.Logger, srv.config)
if err != nil {
srv.Logger.Error("error creating notification services", "error", err)
return nil, err return nil, err
} }
logger.Info("creating server") srv.Logger.Info("creating server")
server := &Server{ server := &Server{
Logger: logger, Logger: srv.Logger,
config: config, config: srv.config,
birthdays: birthdays, birthdays: birthdays,
notificationServices: notificationServices, notificationServices: notificationServices,
} }