diff --git a/README.md b/README.md index 049e241..f6698a7 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,13 @@ $ make run - [X] Define how to read the birthdays info - [X] Add a logger instance to the config - [ ] Add config validation on server creation -- [ ] Add options to the server -- [ ] Pass logger to the server through an option +- [X] Add options to the server +- [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 different message systems to use with the bot - [X] Telegram - [ ] Email - [ ] Mattermost -- [ ] Reduce logger verbosity - [ ] Enjoy! diff --git a/cmd/birthdaybot/main.go b/cmd/birthdaybot/main.go index 4d7056d..ddf5439 100644 --- a/cmd/birthdaybot/main.go +++ b/cmd/birthdaybot/main.go @@ -11,24 +11,35 @@ import ( "github.com/charmbracelet/log" ) +func configureLogger(config *model.Config, logger *log.Logger) { + // ToDo: apply config to logger +} + func main() { configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file") flag.Parse() + logger := log.New(os.Stderr) + config, err := model.ReadConfig(*configFlag) if err != nil { - log.Error("error reading config", "configPath", *configFlag, "error", err) + logger.Error("error reading config", "configPath", *configFlag, "error", err) os.Exit(1) } 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) } - srv, err := server.New(config) + configureLogger(config, logger) + + srv, err := server.New( + server.WithLogger(logger), + server.WithConfig(config), + ) if err != nil { - log.Error("error creating server", "error", err) + logger.Error("error creating server", "error", err) os.Exit(1) } diff --git a/server/options.go b/server/options.go new file mode 100644 index 0000000..99a33cd --- /dev/null +++ b/server/options.go @@ -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 + } +} diff --git a/server/server.go b/server/server.go index 8074e40..0921de7 100644 --- a/server/server.go +++ b/server/server.go @@ -3,7 +3,6 @@ package server import ( "errors" "fmt" - "os" "git.ctrlz.es/mgdelacroix/birthdaybot/model" "git.ctrlz.es/mgdelacroix/birthdaybot/notification" @@ -39,30 +38,33 @@ func createNotificationServices(logger *log.Logger, config *model.Config) ([]not return notificationServices, nil } -func New(config *model.Config) (*Server, error) { - logger := log.New(os.Stderr) - - logger.Debug("parsing CSV file", "birthdayFile", config.BirthdayFile) - - 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) +func New(options ...Option) (*Server, error) { + srv := &Server{} + for _, option := range options { + srv = option(srv) } - 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 { - 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 } - logger.Info("creating server") + srv.Logger.Info("creating server") server := &Server{ - Logger: logger, - config: config, + Logger: srv.Logger, + config: srv.config, birthdays: birthdays, notificationServices: notificationServices, }