Adds logger config
This commit is contained in:
parent
e43cb05755
commit
f11b9b1d93
4 changed files with 91 additions and 6 deletions
|
@ -33,10 +33,10 @@ $ make run
|
|||
- [X] Create the bot scaffold
|
||||
- [X] Define how to read the birthdays info
|
||||
- [X] Add a logger instance to the config
|
||||
- [ ] Add config validation on server creation
|
||||
- [X] Add config validation on server creation
|
||||
- [X] Add options to the server
|
||||
- [X] Pass logger to the server through an option
|
||||
- [ ] Configure logger through config (levels and such)
|
||||
- [X] 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
|
||||
|
|
|
@ -13,8 +13,11 @@ import (
|
|||
)
|
||||
|
||||
func loggerFromConfig(config *model.Config) *log.Logger {
|
||||
// ToDo: apply config to logger
|
||||
return log.New(os.Stderr)
|
||||
logger := log.New(os.Stderr)
|
||||
logger.SetReportCaller(config.Logger.ReportCaller)
|
||||
logger.SetLevel(log.ParseLevel(config.Logger.Level))
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
---
|
||||
birthday_file: birthdays.csv
|
||||
|
||||
logger:
|
||||
level: debug
|
||||
report_caller: true
|
||||
|
||||
telegram_notifications:
|
||||
bot_token: "123456789:YOUR_BOT_TOKEN"
|
||||
channel_id: "-100012345"
|
||||
|
|
|
@ -1,23 +1,99 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrConfigBirthdayFileEmpty = 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"`
|
||||
Logger *LoggerConfig `yaml:"logger"`
|
||||
TelegramNotifications *TelegramNotificationsConfig `yaml:"telegram_notifications"`
|
||||
}
|
||||
|
||||
// ToDo: to be implemented
|
||||
func (c *Config) IsValid() error {
|
||||
if c.BirthdayFile == "" {
|
||||
return ErrConfigBirthdayFileEmpty
|
||||
}
|
||||
|
||||
if err := c.Logger.IsValid(); err != nil {
|
||||
return fmt.Errorf("invalid logger config: %w", err)
|
||||
}
|
||||
|
||||
if c.TelegramNotifications != nil {
|
||||
if err := c.TelegramNotifications.IsValid(); err != nil {
|
||||
return fmt.Errorf("invalid telegram notifications config: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) SetDefaults() {
|
||||
if c.Logger == nil {
|
||||
c.Logger = &LoggerConfig{}
|
||||
}
|
||||
|
||||
c.Logger.SetDefaults()
|
||||
|
||||
if c.TelegramNotifications != nil {
|
||||
c.TelegramNotifications.SetDefaults()
|
||||
}
|
||||
}
|
||||
|
||||
type LoggerConfig struct {
|
||||
Level string `yaml:"level"`
|
||||
ReportCaller bool `yaml:"report_caller"`
|
||||
}
|
||||
|
||||
func (lc *LoggerConfig) SetDefaults() {}
|
||||
|
||||
func (lc *LoggerConfig) IsValid() error {
|
||||
level := strings.TrimSpace(strings.ToLower(lc.Level))
|
||||
|
||||
found := false
|
||||
for _, ll := range []string{"debug", "info", "warn", "error", "fatal", ""} {
|
||||
if level == ll {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return ErrLoggerConfigBadLevel
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type TelegramNotificationsConfig struct {
|
||||
BotToken string `yaml:"bot_token"`
|
||||
ChannelID string `yaml:"channel_id"`
|
||||
}
|
||||
|
||||
// ToDo: to be implemented
|
||||
func (c *Config) IsValid() error {
|
||||
func (tnc *TelegramNotificationsConfig) SetDefaults() {}
|
||||
|
||||
func (tnc *TelegramNotificationsConfig) IsValid() error {
|
||||
if tnc.BotToken == "" {
|
||||
return ErrTelegramNotificationsConfigEmptyBotToken
|
||||
}
|
||||
|
||||
if tnc.ChannelID == "" {
|
||||
return ErrTelegramNotificationsConfigEmptyChannelID
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -32,5 +108,7 @@ func ReadConfig(path string) (*Config, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
config.SetDefaults()
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue