Goconfig.go -rw-r--r-- 1.9 KiB
1package main
2
3import (
4 "errors"
5 "fmt"
6 "os"
7 "path"
8
9 "github.com/BurntSushi/toml"
10)
11
12func defaultConfigPath() string {
13 xdg_home := os.Getenv("XDG_CONFIG_HOME")
14 if xdg_home != "" {
15 return path.Join(xdg_home, "lingua/config.toml")
16 }
17 return "config.toml"
18}
19
20type Config struct {
21 Sources []Source
22 Model string
23 Language Language
24}
25
26func (c *Config) ByName(name string) Source {
27 for i := range c.Sources {
28 if c.Sources[i].Name() == name {
29 return c.Sources[i]
30 }
31 }
32 return nil
33}
34
35type SourceConfig struct {
36 Name string `toml:"name"`
37 Language string `toml:"language"`
38 Kind string `toml:"kind"`
39 Url string `toml:"url"`
40 Id int64 `toml:"id"`
41}
42
43type innerConfig struct {
44 MinifluxEndpoint string `toml:"miniflux-endpoint"`
45 Language string `toml:"language"`
46 Model string `toml:"model"`
47 Sources []SourceConfig `toml:"sources"`
48}
49
50func load_config(path string) (*Config, error) {
51 var cfg innerConfig
52 if _, err := toml.DecodeFile(path, &cfg); err != nil {
53 return nil, err
54 }
55 localLang, ok := Languages[cfg.Language]
56 if !ok {
57 return nil, errors.New(fmt.Sprintf("Unsupported local language: %s", cfg.Language))
58 }
59 sources := []Source{}
60 for _, sourceCfg := range cfg.Sources {
61 language, ok := Languages[sourceCfg.Language]
62 if !ok {
63 return nil, errors.New(fmt.Sprintf("Unsupported remote language: %s", sourceCfg.Language))
64 }
65 if sourceCfg.Kind == "RSS" || sourceCfg.Kind == "rss" {
66 sources = append(sources, &RssFeed{
67 url: sourceCfg.Url, name: sourceCfg.Name, language: language})
68 } else if sourceCfg.Kind == "Miniflux" || sourceCfg.Kind == "miniflux" {
69 sources = append(sources, &MinifluxFeed{
70 endpoint: cfg.MinifluxEndpoint, name: sourceCfg.Name, language: language, feedId: sourceCfg.Id,
71 })
72 }
73 }
74 return &Config{Sources: sources, Model: cfg.Model, Language: localLang}, nil
75}