fix: config, device_name and device_location
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
				
			|||||||
package config
 | 
					package config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -8,7 +9,14 @@ var configDir string
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var configCmd = &cobra.Command{
 | 
					var configCmd = &cobra.Command{
 | 
				
			||||||
	Use:   "config",
 | 
						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) {
 | 
					func InitCmd(cmd *cobra.Command, c string) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,13 +8,14 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var force bool
 | 
					var force bool
 | 
				
			||||||
 | 
					var logDir string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var createConfigCmd = &cobra.Command{
 | 
					var createConfigCmd = &cobra.Command{
 | 
				
			||||||
	Use:   "create",
 | 
						Use:   "create",
 | 
				
			||||||
	Short: "create",
 | 
						Short: "create",
 | 
				
			||||||
	Run: func(cmd *cobra.Command, args []string) {
 | 
						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)
 | 
								log.Fatal(err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
@@ -23,4 +24,5 @@ var createConfigCmd = &cobra.Command{
 | 
				
			|||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	configCmd.AddCommand(createConfigCmd)
 | 
						configCmd.AddCommand(createConfigCmd)
 | 
				
			||||||
	createConfigCmd.Flags().BoolVarP(&force, "force", "f", false, "Force the creation of a new configuration")
 | 
						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")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,19 +6,25 @@ import (
 | 
				
			|||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/satori/go.uuid"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
 | 
						"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/satori/go.uuid"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var configFilename = "config.json"
 | 
					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)
 | 
						configPath := filepath.Join(configDir, configFilename)
 | 
				
			||||||
 | 
						humiditiesLogfile := filepath.Join(logDir, humiditiesLogfileName)
 | 
				
			||||||
 | 
						temperaturLogfile := filepath.Join(logDir, temperatureLogfileName)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	config := &types.Config{
 | 
						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
 | 
						// 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) {
 | 
					func Read(configDir string) (*types.Config, error) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	configPath := filepath.Join(configDir, configFilename)
 | 
						configPath := filepath.Join(configDir, configFilename)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user