2023-03-11 15:03:45 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-03-11 19:42:21 +00:00
|
|
|
"errors"
|
2023-03-11 15:03:45 +00:00
|
|
|
"fmt"
|
2023-03-11 19:42:21 +00:00
|
|
|
"io/fs"
|
2023-03-11 15:03:45 +00:00
|
|
|
"os"
|
2023-03-11 19:42:21 +00:00
|
|
|
"strconv"
|
2023-03-11 15:03:45 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TelegramConfig struct {
|
|
|
|
ChannelID int64 `json:"channel_id"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LogConfig struct {
|
|
|
|
Level logrus.Level
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonLogConfig struct {
|
|
|
|
Level string `json:"level"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListenerConfig struct {
|
|
|
|
SockPath string `json:"sock_path"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Telegram TelegramConfig
|
|
|
|
Log LogConfig
|
|
|
|
Listener ListenerConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonConfig struct {
|
|
|
|
Telegram TelegramConfig `json:"telegram"`
|
|
|
|
Log jsonLogConfig `json:"log"`
|
|
|
|
Listener ListenerConfig `json:"listener"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLevel(lvlStr string) logrus.Level {
|
|
|
|
for _, lvl := range logrus.AllLevels {
|
|
|
|
if lvl.String() == lvlStr {
|
|
|
|
return lvl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return logrus.InfoLevel
|
|
|
|
}
|
|
|
|
|
2023-03-11 19:42:21 +00:00
|
|
|
var defaultConfig Config = Config{
|
|
|
|
Telegram: TelegramConfig{
|
|
|
|
ChannelID: 0,
|
|
|
|
Token: "",
|
|
|
|
},
|
|
|
|
Log: LogConfig{
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
},
|
|
|
|
Listener: ListenerConfig{
|
|
|
|
SockPath: "input/notifier.sock",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkOverride(conf *Config) {
|
|
|
|
if val, ok := os.LookupEnv("LOG_LEVEL"); ok {
|
|
|
|
conf.Log.Level = parseLevel(val)
|
|
|
|
}
|
|
|
|
if val, ok := os.LookupEnv("SOCK_PATH"); ok {
|
|
|
|
conf.Listener.SockPath = val
|
|
|
|
}
|
|
|
|
if val, ok := os.LookupEnv("TELEGRAM_CHANNEL_ID"); ok {
|
|
|
|
if intVal, err := strconv.Atoi(val); err == nil {
|
|
|
|
conf.Telegram.ChannelID = int64(intVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if val, ok := os.LookupEnv("TELEGRAM_TOKEN"); ok {
|
|
|
|
conf.Telegram.Token = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 15:03:45 +00:00
|
|
|
func New(filepath string) (*Config, error) {
|
|
|
|
content, err := os.ReadFile(filepath)
|
|
|
|
if err != nil {
|
2023-03-11 19:42:21 +00:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
conf := defaultConfig
|
|
|
|
checkOverride(&conf)
|
|
|
|
return &conf, nil
|
|
|
|
}
|
2023-03-11 15:03:45 +00:00
|
|
|
return nil, fmt.Errorf("failed to read config file %q: %w", filepath, err)
|
|
|
|
}
|
|
|
|
var jsonConf jsonConfig
|
|
|
|
if err := json.Unmarshal(content, &jsonConf); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
|
|
|
}
|
2023-03-11 19:42:21 +00:00
|
|
|
conf := &Config{
|
2023-03-11 15:03:45 +00:00
|
|
|
Telegram: jsonConf.Telegram,
|
|
|
|
Log: LogConfig{
|
|
|
|
Level: parseLevel(jsonConf.Log.Level),
|
|
|
|
},
|
|
|
|
Listener: jsonConf.Listener,
|
2023-03-11 19:42:21 +00:00
|
|
|
}
|
|
|
|
checkOverride(conf)
|
|
|
|
return conf, nil
|
2023-03-11 15:03:45 +00:00
|
|
|
}
|