tss/app/config.go
2022-05-01 15:32:41 -04:00

54 lines
923 B
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 {
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")
}
return &c, nil
}