-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
67 lines (53 loc) · 1.4 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
package main
import (
"errors"
"log/slog"
"os"
"github.com/kubaceg/sofar_g3_lsw3_logger_reader/adapters/export/otlp"
"github.com/kubaceg/sofar_g3_lsw3_logger_reader/adapters/export/mosquitto"
"gopkg.in/yaml.v2"
)
type Config struct {
Debug bool `default:"false" yaml:"debug"`
Inverter struct {
Port string `yaml:"port"`
LoggerSerial uint `yaml:"loggerSerial"`
ReadInterval int `default:"60" yaml:"readInterval"`
LoopLogging bool `default:"true" yaml:"loopLogging"`
AttrWhiteList []string `yaml:"attrWhiteList"`
AttrBlackList []string `yaml:"attrBlackList"`
} `yaml:"inverter"`
Mqtt mosquitto.MqttConfig `yaml:"mqtt"`
Otlp otlp.Config `yaml:"otlp"`
}
func (c *Config) validate() error {
if c.Inverter.Port == "" {
return errors.New("missing required inverter.port config")
}
if c.Inverter.LoggerSerial == 0 {
return errors.New("missing required inverter.loggerSerial config")
}
return nil
}
func NewConfig(configPath string) (*Config, error) {
config := &Config{}
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, err
}
if err := config.validate(); err != nil {
return nil, err
}
return config, nil
}
func (c Config) getLoglevel() slog.Leveler {
if c.Debug {
return slog.LevelDebug
}
return slog.LevelInfo
}