This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
/
config.go
142 lines (118 loc) · 3.77 KB
/
config.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2017 Martin Planer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package felix
import (
"io/ioutil"
"time"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// Default values for the configuration
const (
DefaultFeedFetchInterval = 65 * time.Minute
DefaultUserAgent = "felix"
DefaultPort = 6554
DefaultFeedOutputMaxAge = 6 * time.Hour
DefaultCleanupInterval = 1 * time.Hour
DefaultCleanupMaxAge = 24 * time.Hour
)
// Config contains the configuration
type Config struct {
FetchInterval time.Duration `yaml:"fetchInterval"`
UserAgent string `yaml:"userAgent"`
Port int `yaml:"port"`
FeedOutputMaxAge time.Duration `yaml:"feedOutputMaxAge"`
CleanupInterval time.Duration `yaml:"cleanupInterval"`
CleanupMaxAge time.Duration `yaml:"cleanupMaxAge"`
Feeds []FeedConfig `yaml:"feeds"`
ItemFilters []FilterConfig `yaml:"itemFilters"`
LinkFilters []FilterConfig `yaml:"linkFilters"`
}
var emptyConfig = Config{}
// FeedConfig contains the configuration of a single feed.
type FeedConfig struct {
Type string
URL string
FetchInterval time.Duration
}
// FilterConfig is the common configuration of all filter types.
// See FilterConfig.Unmarshal for unmarshaling of the raw config value for more specific types.
type FilterConfig struct {
Type string
raw map[string]interface{}
}
// UnmarshalYAML is a custom YAML unmarshal handler to handle the common filter config elements.
// See https://godoc.org/gopkg.in/yaml.v2#Unmarshaler.
func (fc *FilterConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
aux := &struct {
Type string
}{}
if err := unmarshal(aux); err != nil {
return err
}
raw := map[string]interface{}{}
if err := unmarshal(raw); err != nil {
return err
}
fc.Type = aux.Type
fc.raw = raw
return nil
}
// Unmarshal decodes the raw config values for a more specific config type.
func (fc *FilterConfig) Unmarshal(v interface{}) error {
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "yaml", // Re-use 'yaml' field tags instead of 'mapstructure'
Result: v,
})
if err != nil {
return err
}
return dec.Decode(fc.raw)
}
// ItemTitleFilterConfig contains the configuration of a ItemTitleFilter.
type ItemTitleFilterConfig struct {
Type string
Titles []string
// TODO: allow grouping of title and alternative title? -> [][]string
}
// LinkDomainFilterConfig contains the configuration of a LinkDomainFilter.
type LinkDomainFilterConfig struct {
Domains []string
}
// LinkURLRegexFilterConfig contains the configuration of a LinkURLRegexFilter.
type LinkURLRegexFilterConfig struct {
Exprs []string
}
// LinkFilenameAsTitleFilterConfig contains the configuration of a LinkFilenameAsTitleFilter.
type LinkFilenameAsTitleFilterConfig struct {
TrimExt bool `yaml:"trimExt"`
}
type LinkDuplicatesFilterConfig struct {
Size int
}
// NewConfig returns a new configuration with default values.
func NewConfig() Config {
// TODO: return value or pointer?
return Config{
UserAgent: DefaultUserAgent,
Port: DefaultPort,
FetchInterval: DefaultFeedFetchInterval,
FeedOutputMaxAge: DefaultFeedOutputMaxAge,
CleanupInterval: DefaultCleanupInterval,
CleanupMaxAge: DefaultCleanupMaxAge,
}
}
// ConfigFromFile returns a configuration parsed from the given file.
func ConfigFromFile(filename string) (Config, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return emptyConfig, err
}
config := NewConfig()
if err := yaml.Unmarshal(b, &config); err != nil {
return emptyConfig, errors.Wrap(err, "could not unmarshal config")
}
return config, nil
}