From af92353a89188ee4c1d4f37db0382cf0b962f8f9 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Fri, 30 Jun 2023 10:38:29 +0200 Subject: [PATCH] Add logger to the server --- README.md | 2 +- cmd/birthdaybot/main.go | 15 ++++++++++++++- server/server.go | 11 +++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6a6e65c..6a0eaf4 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ information of the subject so you can reach them easily. - [ ] Create the bot scaffold - [X] Define how to read the birthdays info -- [ ] Add a logger instance to the config +- [X] Add a logger instance to the config - [ ] Add config validation on server creation - [ ] Create a configurable template to fill with each notification - [ ] Create different message systems to use with the bot diff --git a/cmd/birthdaybot/main.go b/cmd/birthdaybot/main.go index a9faa93..6860f40 100644 --- a/cmd/birthdaybot/main.go +++ b/cmd/birthdaybot/main.go @@ -2,7 +2,10 @@ package main import ( "flag" + "os" + "git.ctrlz.es/mgdelacroix/birthdaybot/model" + "git.ctrlz.es/mgdelacroix/birthdaybot/server" "github.com/charmbracelet/log" ) @@ -10,5 +13,15 @@ func main() { configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file") flag.Parse() - log.Info("Starting, with config file", "config", *configFlag) + config, err := model.ReadConfig(*configFlag) + if err != nil { + log.Error("error reading config", "configPath", *configFlag, "error", err) + os.Exit(1) + } + + _, err = server.New(config) + if err != nil { + log.Error("error creating server", "error", err) + os.Exit(1) + } } diff --git a/server/server.go b/server/server.go index bf8efa5..f27846a 100644 --- a/server/server.go +++ b/server/server.go @@ -2,23 +2,34 @@ package server import ( "fmt" + "os" "git.ctrlz.es/mgdelacroix/birthdaybot/model" "git.ctrlz.es/mgdelacroix/birthdaybot/parser" + "github.com/charmbracelet/log" ) type Server struct { + logger *log.Logger config *model.Config birthdays []*model.Birthday } func New(config *model.Config) (*Server, error) { + logger := log.New(os.Stderr) + + logger.Info("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) } + logger.Info("creating server") + server := &Server{ + logger: logger, config: config, birthdays: birthdays, }