forked from kubaceg/sofar_g3_lsw3_logger_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
51 lines (40 loc) · 984 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
package main
import (
"errors"
"os"
"github.com/kubaceg/sofar_g3_lsw3_logger_reader/adapters/databases/mosquitto"
"gopkg.in/yaml.v2"
)
type Config struct {
Inverter struct {
Port string `yaml:"port"`
LoggerSerial uint `yaml:"loggerSerial"`
ReadInterval int `default:"60" yaml:"readInterval"`
} `yaml:"inverter"`
Mqtt mosquitto.MqttConfig `yaml:"mqtt"`
}
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
}