tss/app/config.go

49 lines
810 B
Go
Raw Normal View History

2022-04-27 00:19:05 -04:00
package app
2022-04-26 09:55:59 -04:00
import (
"os"
2022-04-28 19:24:21 -04:00
"path"
2022-04-26 09:55:59 -04:00
"gopkg.in/yaml.v3"
)
2022-04-28 19:24:21 -04:00
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"`
}
2022-04-26 09:55:59 -04:00
type Config struct {
2022-04-28 19:24:21 -04:00
DataDir string `yaml:"data_dir"`
LastSuccessPath string `yaml:"last_loaded_path"`
Feeds []FeedCfg `yaml:"feeds"`
2022-04-26 09:55:59 -04:00
}
2022-04-27 00:19:05 -04:00
func ParseConfig(configPath string) (*Config, error) {
2022-04-28 19:24:21 -04:00
2022-04-27 00:19:05 -04:00
cfg, err := os.ReadFile(configPath)
2022-04-26 09:55:59 -04:00
if err != nil {
return nil, err
}
c := Config{}
err = yaml.Unmarshal(cfg, &c)
if err != nil {
return nil, err
}
2022-04-28 19:24:21 -04:00
if c.DataDir == "" {
c.DataDir = "."
}
if c.LastSuccessPath == "" {
c.LastSuccessPath = path.Join(c.DataDir, "last_success.yml")
}
2022-04-26 09:55:59 -04:00
return &c, nil
}