birthdaybot/model/config.go

158 lines
3.3 KiB
Go
Raw Normal View History

2023-06-29 22:46:17 +01:00
package model
import (
2023-07-10 13:55:25 +01:00
"errors"
"fmt"
2023-07-11 15:43:20 +01:00
"os"
2023-07-10 13:55:25 +01:00
"strings"
2023-06-29 22:46:17 +01:00
"gopkg.in/yaml.v3"
)
2023-07-10 13:55:25 +01:00
var (
ErrBirthdaysConfigFileEmpty = errors.New("birthday file cannot be empty")
2023-07-10 13:55:25 +01:00
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")
)
2023-06-29 22:46:17 +01:00
type Config struct {
Birthdays *BirthdaysConfig `yaml:"birthdays"`
2023-07-10 13:55:25 +01:00
Logger *LoggerConfig `yaml:"logger"`
2023-07-11 11:37:56 +01:00
Web *WebConfig `yaml:"web"`
TelegramNotifications *TelegramNotificationsConfig `yaml:"telegram_notifications"`
}
2023-07-10 13:55:25 +01:00
func (c *Config) IsValid() error {
if err := c.Birthdays.IsValid(); err != nil {
return fmt.Errorf("invalid birthdays config: %w", err)
2023-07-10 13:55:25 +01:00
}
if err := c.Logger.IsValid(); err != nil {
return fmt.Errorf("invalid logger config: %w", err)
}
2023-07-11 11:37:56 +01:00
if err := c.Web.IsValid(); err != nil {
return fmt.Errorf("invalid web config: %w", err)
}
2023-07-10 13:55:25 +01:00
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.Birthdays == nil {
c.Birthdays = &BirthdaysConfig{}
}
c.Birthdays.SetDefaults()
2023-07-10 13:55:25 +01:00
if c.Logger == nil {
c.Logger = &LoggerConfig{}
}
c.Logger.SetDefaults()
2023-07-11 11:37:56 +01:00
if c.Web == nil {
c.Web = &WebConfig{}
}
c.Web.SetDefaults()
2023-07-10 13:55:25 +01:00
if c.TelegramNotifications != nil {
c.TelegramNotifications.SetDefaults()
}
}
type BirthdaysConfig struct {
File string `yaml:"file"`
Template string `yaml:"template"`
2023-07-10 20:25:54 +01:00
Pictures string `yaml:"pictures"`
}
func (bc *BirthdaysConfig) IsValid() error {
if bc.File == "" {
return ErrBirthdaysConfigFileEmpty
}
return nil
}
func (bc *BirthdaysConfig) SetDefaults() {}
2023-07-10 13:55:25 +01:00
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
}
2023-07-11 11:37:56 +01:00
type WebConfig struct {
Enabled bool `yaml:"enabled"`
Port int `yaml:"port"`
2023-07-11 11:37:56 +01:00
}
func (wc *WebConfig) SetDefaults() {}
2023-07-11 11:37:56 +01:00
func (wc *WebConfig) IsValid() error {
return nil
}
type TelegramNotificationsConfig struct {
BotToken string `yaml:"bot_token"`
ChannelID string `yaml:"channel_id"`
}
2023-07-10 13:55:25 +01:00
func (tnc *TelegramNotificationsConfig) SetDefaults() {}
func (tnc *TelegramNotificationsConfig) IsValid() error {
if tnc.BotToken == "" {
return ErrTelegramNotificationsConfigEmptyBotToken
}
if tnc.ChannelID == "" {
return ErrTelegramNotificationsConfigEmptyChannelID
}
return nil
2023-06-29 22:46:17 +01:00
}
func ReadConfig(path string) (*Config, error) {
2023-07-11 15:43:20 +01:00
fileBytes, err := os.ReadFile(path)
2023-06-29 22:46:17 +01:00
if err != nil {
return nil, err
}
var config *Config
if err := yaml.Unmarshal(fileBytes, &config); err != nil {
return nil, err
}
2023-07-10 13:55:25 +01:00
config.SetDefaults()
2023-06-29 22:46:17 +01:00
return config, nil
}