fix: config, device_name and device_location

This commit is contained in:
Markus Pesch 2018-11-28 12:54:14 +01:00
parent ad171c5d2a
commit 3532c771ec
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
3 changed files with 54 additions and 6 deletions

View File

@ -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) {

View File

@ -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")
}

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)