aboutsummaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: 2df7d98575c1a56988078576768f0910ff9bc260 (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
package config

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
)

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

type config struct {
	GlobalConfig Map `yaml:",inline"`
	Feeds        []configGroupFeed
}

type Config struct {
	GlobalConfig Map `yaml:",inline"`
	Feeds        Feeds
}

type Feed struct {
	Name    string
	Target  string
	Url     string
	MinFreq int
	Config  Map
}

func Load(path string) (*Config, error) {
	buf, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("while reading '%s': %w", path, err)
	}

	var parsedCfg config
	if err := yaml.Unmarshal(buf, &parsedCfg); err != nil {
		return nil, fmt.Errorf("while unmarshalling: %w", err)
	}
	fmt.Printf("--- parsedCfg:\n%+v\n\n", parsedCfg)

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

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

	return &finishedCfg, nil
}