1
0
mirror of https://github.com/Luzifer/tasmota-config.git synced 2024-09-19 09:02:55 +00:00
tasmota-config/config.go

46 lines
957 B
Go
Raw Permalink Normal View History

2020-07-04 23:21:48 +00:00
package main
import (
"os"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type configFile struct {
CommandPrefix string `yaml:"command_prefix"`
StatPrefix string `yaml:"stat_prefix"`
Settings map[string]interface{} `yaml:"settings"`
Devices map[string]deviceConfig `yaml:"devices"`
}
type deviceConfig struct {
Topic string `yaml:"topic"`
Settings map[string]interface{} `yaml:"settings"`
}
func (d deviceConfig) constructTopic(prefix, suffix string) string {
return strings.Join([]string{
strings.Trim(prefix, "/"),
strings.Trim(d.Topic, "/"),
suffix,
}, "/")
}
func loadConfig(p string) (*configFile, error) {
f, err := os.Open(p)
if err != nil {
return nil, errors.Wrap(err, "Unable to open config file")
}
defer f.Close()
var out = &configFile{
CommandPrefix: "cmnd",
StatPrefix: "stat",
}
return out, errors.Wrap(yaml.NewDecoder(f).Decode(out), "Unable to decode config file")
}