PKGBUILD/pkg/config/config.go

69 lines
1.4 KiB
Go

package config
import (
"fmt"
"io"
"os"
"regexp"
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
)
var validUUID = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
type Config interface {
AddSensor(sensor *types.Sensor) error
AddRemote(remote *Remote) error
DisableRemote(nameOrUUID string) error
DisableSensor(nameOrUUID string) error
EnableRemote(nameOrUUID string) error
EnableSensor(nameOrUUID string) error
GetDevice() *Device
GetRemotes() []*Remote
GetSensors() []*types.Sensor
JSONDecoder(r io.Reader) error
JSONWriter(w io.Writer) error
RemoveSensor(nameOrUUID string) error
RemoveRemote(nameOrUUID string) error
SetDevice(device *Device)
ToJSON() (string, error)
}
// Read the configuration file
func Read(configFile string) (*FluckyConfig, error) {
fc := &FluckyConfig{}
f, err := os.Open(configFile)
if err != nil {
return nil, fmt.Errorf("Can not open file %v: %v", configFile, err)
}
defer f.Close()
err = fc.JSONDecoder(f)
if err != nil {
return nil, fmt.Errorf("Can not decode json file %v: %v", configFile, err)
}
return fc, nil
}
// Write the configuration into a file, specified by the configuration filepath
func Write(cfg Config, configFile string) error {
f, err := os.Create(configFile)
if err != nil {
return err
}
defer f.Close()
err = cfg.JSONWriter(f)
if err != nil {
return err
}
return nil
}