43 lines
719 B
Go
43 lines
719 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
|
|
"git.tcp.direct/kayos/door5/iot"
|
|
)
|
|
|
|
type Pins map[iot.Thing]iot.GPIO
|
|
|
|
type Config struct {
|
|
Pins Pins `toml:"pins"`
|
|
}
|
|
|
|
func (p Pins) MarshalTOML() ([]byte, error) {
|
|
var rendered string
|
|
for thing, pin := range p {
|
|
rendered += fmt.Sprintf("%s = %d\n", thing.String(), pin)
|
|
}
|
|
return []byte(rendered), nil
|
|
}
|
|
|
|
func ReadConfig(path string) (*Config, error) {
|
|
var config = &Config{}
|
|
b, e := os.ReadFile(path)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
err := toml.Unmarshal(b, config)
|
|
return config, err
|
|
}
|
|
|
|
func (c *Config) WriteConfig(path string) error {
|
|
b, e := toml.Marshal(c)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
return os.WriteFile(path, b, 0644)
|
|
}
|