tss/app/config.go

122 lines
2.6 KiB
Go
Raw Normal View History

2022-04-27 00:19:05 -04:00
package app
2022-04-26 09:55:59 -04:00
import (
2022-05-27 18:26:30 -04:00
"fmt"
2022-04-26 09:55:59 -04:00
"os"
2022-04-28 19:24:21 -04:00
"path"
2022-04-26 09:55:59 -04:00
2022-05-03 17:14:37 -04:00
"go.balki.me/tss/parser"
2022-04-26 09:55:59 -04:00
"gopkg.in/yaml.v3"
)
2022-05-02 23:23:56 -04:00
type FeedLimit int
const NoLimit FeedLimit = -1
2022-05-27 18:26:30 -04:00
type FeedURL []string
2022-04-28 19:24:21 -04:00
type FeedCfg struct {
2022-05-03 17:14:37 -04:00
Name string `yaml:"name"`
Channel string `yaml:"channel"`
Rhash string `yaml:"rhash"`
2022-05-27 18:26:30 -04:00
Url FeedURL `yaml:"url"`
2022-05-03 17:14:37 -04:00
Cron string `yaml:"cron"`
Proxy string `yaml:"proxy"`
Type parser.FeedType `yaml:"type"`
2022-05-07 21:01:14 -04:00
Title string `yaml:"title"`
2022-05-03 17:14:37 -04:00
FTL *int `yaml:"first_time_limit"`
FirstTimeLimit FeedLimit `yaml:"-"`
2022-04-28 19:24:21 -04:00
}
2022-04-26 09:55:59 -04:00
type Config struct {
2022-05-02 23:23:56 -04:00
Proxy string `yaml:"proxy"`
TelegramProxy string `yaml:"telegram_proxy"`
TelegramAuthToken string `yaml:"telegram_auth_token"` //TODO: read from credential file
2022-05-01 23:28:54 -04:00
DataDir string `yaml:"data_dir"`
LastSuccessPath string `yaml:"last_loaded_path"`
DbDir string `yaml:"db_dir"`
2022-05-02 23:23:56 -04:00
FTL *int `yaml:"first_time_limit"`
2022-05-01 23:28:54 -04:00
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-05-01 15:32:41 -04:00
if c.DbDir == "" {
c.DbDir = path.Join(c.DataDir, "feed_data")
}
2022-05-02 23:23:56 -04:00
err = os.MkdirAll(c.DbDir, 0755)
if err != nil {
return nil, err
}
err = os.MkdirAll(c.DataDir, 0755)
if err != nil {
return nil, err
}
2022-05-01 19:13:27 -04:00
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
}
}
}
2022-05-02 11:36:24 -04:00
if c.TelegramProxy == "NONE" {
c.TelegramProxy = ""
}
2022-05-01 19:13:27 -04:00
for i, _ := range c.Feeds {
feedCfg := &c.Feeds[i]
if feedCfg.Proxy == "NONE" {
feedCfg.Proxy = ""
}
2022-05-02 23:23:56 -04:00
if feedCfg.FTL != nil {
feedCfg.FirstTimeLimit = FeedLimit(*feedCfg.FTL)
} else if c.FTL != nil {
feedCfg.FirstTimeLimit = FeedLimit(*c.FTL)
} else {
feedCfg.FirstTimeLimit = NoLimit
}
2022-05-01 19:13:27 -04:00
}
2022-04-26 09:55:59 -04:00
return &c, nil
}
2022-05-27 18:26:30 -04:00
func (fu *FeedURL) UnmarshalYAML(node *yaml.Node) error {
*fu = []string{""}
switch node.Kind {
case yaml.SequenceNode:
return node.Decode((*[]string)(fu))
case yaml.ScalarNode:
return node.Decode(&(*fu)[0])
}
return fmt.Errorf("unexpected node type: %s, at %d:%d", node.ShortTag(), node.Line, node.Column)
}