package app import ( "fmt" "os" "path" "go.balki.me/tss/parser" "gopkg.in/yaml.v3" ) type FeedLimit int const NoLimit FeedLimit = -1 type FeedURL []string type FeedCfg struct { Name string `yaml:"name"` Channel string `yaml:"channel"` Rhash string `yaml:"rhash"` Url FeedURL `yaml:"url"` Cron string `yaml:"cron"` Proxy string `yaml:"proxy"` Type parser.FeedType `yaml:"type"` Title string `yaml:"title"` 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 } func (fu *FeedURL) UnmarshalYAML(node *yaml.Node) error { switch node.Kind { case yaml.SequenceNode: return node.Decode((*[]string)(fu)) case yaml.ScalarNode: *fu = []string{""} return node.Decode(&(*fu)[0]) } return fmt.Errorf("unexpected node type: %s, at %d:%d", node.ShortTag(), node.Line, node.Column) }