tss/app/config.go
2022-05-02 23:23:56 -04:00

105 lines
2.1 KiB
Go

package app
import (
"os"
"path"
"gopkg.in/yaml.v3"
)
type FeedLimit int
const NoLimit FeedLimit = -1
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"`
FTL *int `yaml:"first_time_limit"`
FirstTimeLimit FeedLimit `yaml:"-"`
}
type Config struct {
Proxy string `yaml:"proxy"`
TelegramProxy string `yaml:"telegram_proxy"`
TelegramAuthToken string `yaml:"telegram_auth_token"` //TODO: read from credential file
DataDir string `yaml:"data_dir"`
LastSuccessPath string `yaml:"last_loaded_path"`
DbDir string `yaml:"db_dir"`
FTL *int `yaml:"first_time_limit"`
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")
}
err = os.MkdirAll(c.DbDir, 0755)
if err != nil {
return nil, err
}
err = os.MkdirAll(c.DataDir, 0755)
if err != nil {
return nil, err
}
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 = ""
}
if feedCfg.FTL != nil {
feedCfg.FirstTimeLimit = FeedLimit(*feedCfg.FTL)
} else if c.FTL != nil {
feedCfg.FirstTimeLimit = FeedLimit(*c.FTL)
} else {
feedCfg.FirstTimeLimit = NoLimit
}
}
return &c, nil
}