From 3532c771ec49f3118887226c9d489a24dde38512 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Wed, 28 Nov 2018 12:54:14 +0100 Subject: [PATCH] fix: config, device_name and device_location --- cmd/config/config.go | 10 +++++++++- cmd/config/create.go | 4 +++- pkg/config/config.go | 46 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/cmd/config/config.go b/cmd/config/config.go index 1097858..d0fb624 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -1,6 +1,7 @@ package config import ( + "git.cryptic.systems/fh-trier/go-flucky/pkg/config" "github.com/spf13/cobra" ) @@ -8,7 +9,14 @@ var configDir string var configCmd = &cobra.Command{ Use: "config", - Short: "Manage configuration", + Short: "Manage Configuration", + Run: func(cmd *cobra.Command, args []string) { + if args[0] == "device.name" { + config.DeviceName(args[1], configDir) + } else if args[0] == "device.location" { + config.DeviceLocation(args[1], configDir) + } + }, } func InitCmd(cmd *cobra.Command, c string) { diff --git a/cmd/config/create.go b/cmd/config/create.go index 9227288..5796842 100644 --- a/cmd/config/create.go +++ b/cmd/config/create.go @@ -8,13 +8,14 @@ import ( ) var force bool +var logDir string var createConfigCmd = &cobra.Command{ Use: "create", Short: "create", Run: func(cmd *cobra.Command, args []string) { - if err := config.Create(configDir, force); err != nil { + if err := config.Create(configDir, logDir, force); err != nil { log.Fatal(err) } }, @@ -23,4 +24,5 @@ var createConfigCmd = &cobra.Command{ func init() { configCmd.AddCommand(createConfigCmd) createConfigCmd.Flags().BoolVarP(&force, "force", "f", false, "Force the creation of a new configuration") + createConfigCmd.Flags().StringVarP(&logDir, "log-dir", "l", "/var/log/flucky", "Directory for logfiles") } diff --git a/pkg/config/config.go b/pkg/config/config.go index 2fde266..9a6e97f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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)