fix: config, device_name and device_location

This commit is contained in:
2018-11-28 12:54:14 +01:00
parent ad171c5d2a
commit 3532c771ec
3 changed files with 54 additions and 6 deletions

View File

@ -6,19 +6,25 @@ import (
"os"
"path/filepath"
"github.com/satori/go.uuid"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
"github.com/satori/go.uuid"
)
var configFilename = "config.json"
var humiditiesLogfileName = "humidities.log"
var temperatureLogfileName = "temperature.log"
func Create(configDir string, force bool) error {
func Create(configDir, logDir string, force bool) error {
configPath := filepath.Join(configDir, configFilename)
humiditiesLogfile := filepath.Join(logDir, humiditiesLogfileName)
temperaturLogfile := filepath.Join(logDir, temperatureLogfileName)
config := &types.Config{
DeviceID: uuid.NewV4().String(),
DeviceID: uuid.NewV4().String(),
HumidityLogfile: humiditiesLogfile,
TemperatureLogfile: temperaturLogfile,
}
// If no config file exists, create a new one on the location
@ -47,6 +53,38 @@ func Create(configDir string, force bool) error {
}
func DeviceName(deviceName, configDir string) error {
cnf, err := Read(configDir)
if err != nil {
return err
}
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)