/
config.go
60 lines (47 loc) · 917 Bytes
/
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
package mqttexporter
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
)
type Config struct {
Broker string
Topics []Topic
}
type Topic struct {
Name string
Fields []string
}
func NewConfig() *Config {
return &Config{
Broker: ":1883",
Topics: nil,
}
}
func (c *Config) decode(data []byte) error {
if _, err := toml.Decode(string(data), c); err != nil {
return err
}
return nil
}
func (c *Config) FromFile(filePath string) error {
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("could not read config file from %s", filePath)
}
if err = c.decode(data); err != nil {
return fmt.Errorf("could not decode config file from %s", filePath)
}
return nil
}
func (t *Topic) IsFieldFiltered(key string) bool {
if t.Fields == nil || len(t.Fields) == 0 {
return false
}
for _, field := range t.Fields {
if field == key {
return false
}
}
return true
}