2023-06-29 22:46:17 +01:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2023-07-10 13:55:25 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-06-29 22:46:17 +01:00
|
|
|
"io/ioutil"
|
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 (
|
2023-07-10 20:08:05 +01:00
|
|
|
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 {
|
2023-07-10 20:08:05 +01:00
|
|
|
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"`
|
2023-07-01 16:48:10 +01:00
|
|
|
TelegramNotifications *TelegramNotificationsConfig `yaml:"telegram_notifications"`
|
|
|
|
}
|
|
|
|
|
2023-07-10 13:55:25 +01:00
|
|
|
func (c *Config) IsValid() error {
|
2023-07-10 20:08:05 +01:00
|
|
|
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() {
|
2023-07-10 20:08:05 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 20:08:05 +01:00
|
|
|
type BirthdaysConfig struct {
|
|
|
|
File string `yaml:"file"`
|
|
|
|
Template string `yaml:"template"`
|
2023-07-10 20:25:54 +01:00
|
|
|
Pictures string `yaml:"pictures"`
|
2023-07-10 20:08:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *WebConfig) SetDefaults() {
|
|
|
|
if wc.Port == 0 {
|
|
|
|
wc.Port = 8080
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *WebConfig) IsValid() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-01 16:48:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-01 16:48:10 +01:00
|
|
|
return nil
|
2023-06-29 22:46:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig(path string) (*Config, error) {
|
|
|
|
fileBytes, err := ioutil.ReadFile(path)
|
|
|
|
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
|
|
|
|
}
|