summaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: e330a482f396cb16c11856c804d348b3b7bb5c89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package config

import (
	"fmt"
	"io/ioutil"
	"strings"
)

type Map map[string]interface{}
type Feeds map[string]*Feed

func (f Feeds) String() string {
	var b strings.Builder
	app := func(a ...interface{}) {
		_, _ = fmt.Fprint(&b, a...)
	}
	app("Feeds [")

	first := true
	for k, v := range f {
		if !first {
			app(", ")
		}
		app(`"`, k, `"`, ": ")
		if v == nil {
			app("<nil>")
		} else {
			_, _ = fmt.Fprintf(&b, "%+v", *v)
		}
		first = false
	}
	app("]")

	return b.String()
}

type Config struct {
	GlobalConfig Map
	Feeds        Feeds
}

type Feed struct {
	Name       string
	Target     string `yaml:"-"`
	Url        string
	MinFreq    int   `yaml:"min-frequency"`
	InclImages *bool `yaml:"include-images"`
}

func Load(path string) (Config, error) {
	var finishedCfg Config

	buf, err := ioutil.ReadFile(path)
	if err != nil {
		return finishedCfg, fmt.Errorf("while reading '%s': %w", path, err)
	}

	var parsedCfg config
	if parsedCfg, err = parse(buf); err != nil {
		return finishedCfg, err
	}

	finishedCfg = Config{
		GlobalConfig: parsedCfg.GlobalConfig,
		Feeds:        make(Feeds),
	}

	if err := buildFeeds(parsedCfg.Feeds, "", finishedCfg.Feeds); err != nil {
		return finishedCfg, fmt.Errorf("while parsing: %w", err)
	}

	return finishedCfg, nil
}