tss/app/config.go
2022-05-02 11:36:24 -04:00

81 lines
1.5 KiB
Go

package app
import (
"os"
"path"
"gopkg.in/yaml.v3"
)
type FeedCfg struct {
Name string `yaml:"name"`
Channel string `yaml:"channel"`
Rhash string `yaml:"rhash"`
Url string `yaml:"url"`
Cron string `yaml:"cron"`
Proxy string `yaml:"proxy"`
}
type Config struct {
Proxy string `yaml:"proxy"`
TelegramProxy string `yaml:"telegram_proxy"`
//TODO: read from credential file
TelegramAuthToken string `yaml:"telegram_auth_token"`
DataDir string `yaml:"data_dir"`
LastSuccessPath string `yaml:"last_loaded_path"`
DbDir string `yaml:"db_dir"`
Feeds []FeedCfg `yaml:"feeds"`
}
func ParseConfig(configPath string) (*Config, error) {
cfg, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
c := Config{}
err = yaml.Unmarshal(cfg, &c)
if err != nil {
return nil, err
}
if c.DataDir == "" {
c.DataDir = "."
}
if c.LastSuccessPath == "" {
c.LastSuccessPath = path.Join(c.DataDir, "last_success.yml")
}
if c.DbDir == "" {
c.DbDir = path.Join(c.DataDir, "feed_data")
}
if c.Proxy != "" {
if c.TelegramProxy == "" {
c.TelegramProxy = c.Proxy
}
for i, _ := range c.Feeds {
feedCfg := &c.Feeds[i]
if feedCfg.Proxy == "" {
feedCfg.Proxy = c.Proxy
}
}
}
if c.TelegramProxy == "NONE" {
c.TelegramProxy = ""
}
for i, _ := range c.Feeds {
feedCfg := &c.Feeds[i]
if feedCfg.Proxy == "NONE" {
feedCfg.Proxy = ""
}
}
return &c, nil
}