48 lines
955 B
Go
48 lines
955 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"gitea.berezovskyi.dev/oleksandr/ezcoo-usb-control/pkg/bridge"
|
|
"gitea.berezovskyi.dev/oleksandr/ezcoo-usb-control/pkg/ezcoo"
|
|
)
|
|
|
|
type appConfig struct {
|
|
Device ezcoo.Config `yaml:"device"`
|
|
MQTT bridge.Config `yaml:"mqtt"`
|
|
}
|
|
|
|
func (c *appConfig) SetDefaults() {
|
|
c.Device.SetDefaults()
|
|
c.MQTT.SetDefaults()
|
|
}
|
|
|
|
func (c *appConfig) Validate() error {
|
|
if err := c.Device.Validate(); err != nil {
|
|
return err
|
|
}
|
|
return c.MQTT.Validate()
|
|
}
|
|
|
|
func loadConfig(path string) (appConfig, error) {
|
|
var cfg appConfig
|
|
if path != "" {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return cfg, fmt.Errorf("open config: %w", err)
|
|
}
|
|
defer f.Close()
|
|
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
|
|
return cfg, fmt.Errorf("parse config: %w", err)
|
|
}
|
|
}
|
|
cfg.SetDefaults()
|
|
if err := cfg.Validate(); err != nil {
|
|
return cfg, fmt.Errorf("validate config: %w", err)
|
|
}
|
|
return cfg, nil
|
|
}
|