fix: configuration pkg
This commit is contained in:
@ -1,136 +1,68 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
|
||||
"github.com/satori/go.uuid"
|
||||
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
)
|
||||
|
||||
var configFilename = "config.json"
|
||||
var humiditiesLogfileName = "humidities.log"
|
||||
var temperatureLogfileName = "temperature.log"
|
||||
var validUUID = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
|
||||
|
||||
func Create(configDir, logDir string, force bool) error {
|
||||
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)
|
||||
}
|
||||
|
||||
configPath := filepath.Join(configDir, configFilename)
|
||||
humiditiesLogfile := filepath.Join(logDir, humiditiesLogfileName)
|
||||
temperaturLogfile := filepath.Join(logDir, temperatureLogfileName)
|
||||
// Read the configuration file
|
||||
func Read(configFile string) (*FluckyConfig, error) {
|
||||
|
||||
config := &types.Config{
|
||||
DeviceID: uuid.NewV4().String(),
|
||||
HumidityLogfile: humiditiesLogfile,
|
||||
TemperatureLogfile: temperaturLogfile,
|
||||
}
|
||||
fc := &FluckyConfig{}
|
||||
|
||||
// If no config file exists, create a new one on the location
|
||||
if !force {
|
||||
if _, err := os.Stat(configPath); !os.IsNotExist(err) {
|
||||
return fmt.Errorf("%v already exists. Use -f to overwrite", configPath)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(configDir, os.ModePerm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create directory: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.Create(configPath)
|
||||
f, err := os.Open(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create config: %v", err)
|
||||
return nil, fmt.Errorf("Can not open file %v: %v", configFile, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
config.JSONWriter(f)
|
||||
err = fc.JSONDecoder(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can not decode json file %v: %v", configFile, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return fc, nil
|
||||
|
||||
}
|
||||
|
||||
func DeviceName(deviceName, configDir string) error {
|
||||
cnf, err := Read(configDir)
|
||||
// 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()
|
||||
|
||||
cnf.DeviceName = deviceName
|
||||
|
||||
err = Write(cnf, configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeviceLocation(deviceLocation, configDir string) error {
|
||||
cnf, err := Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cnf.DeviceLocation = deviceLocation
|
||||
|
||||
err = Write(cnf, configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Read(configDir string) (*types.Config, error) {
|
||||
|
||||
configPath := filepath.Join(configDir, configFilename)
|
||||
|
||||
var config types.Config
|
||||
|
||||
// If no config file exists, return an error
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("Can not find config %v: %v", configPath, err)
|
||||
}
|
||||
|
||||
// open config file
|
||||
jsonFile, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can not open file %v: %v", configPath, err)
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
|
||||
jsonParser := json.NewDecoder(jsonFile)
|
||||
if err := jsonParser.Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func Write(config *types.Config, configDir string) error {
|
||||
|
||||
configPath := filepath.Join(configDir, configFilename)
|
||||
|
||||
// If no config file exists, return an error
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("Can not find config %v: %v", configPath, err)
|
||||
}
|
||||
|
||||
// open config file
|
||||
jsonFile, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not open file %v: %v", configPath, err)
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
|
||||
err = config.JSONWriter(jsonFile)
|
||||
err = cfg.JSONWriter(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user