package cmd import ( "fmt" "os" "time" lg "github.com/volker-raschek/flucky/cmd/logger" "github.com/volker-raschek/flucky/cmd/remote" "github.com/volker-raschek/flucky/cmd/sensor" "github.com/volker-raschek/flucky/cmd/temperature" "github.com/volker-raschek/flucky/pkg/logger" "github.com/volker-raschek/flucky/pkg/types" uuid "github.com/satori/go.uuid" "github.com/spf13/cobra" "github.com/volker-raschek/flucky/pkg/config" ) var cfg string var rootCmd = &cobra.Command{ Use: "flucky", Short: "Read from sensors", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // check if config file exists if _, err := os.Stat(cfg); os.IsNotExist(err) { hostname, err := os.Hostname() if err != nil { return fmt.Errorf("Can not locate the hostname: %v", err) } logfiles := make(map[logger.LogValue][]string) logfiles[logger.LogHumidity] = []string{"/var/log/flucky/humidity.log"} logfiles[logger.LogTemperature] = []string{"/var/log/flucky/temperature.log"} fc := config.FluckyConfig{ Device: &types.Device{ DeviceID: uuid.NewV4().String(), DeviceName: hostname, CreationDate: time.Now(), }, FileLogger: &logger.FileLogger{ LogFiles: logfiles, }, } err = config.Write(&fc, cfg) if err != nil { return err } } return nil }, } // Execute a func Execute(version string) { rootCmd.Version = version rootCmd.PersistentFlags().StringVar(&cfg, "config", "/etc/flucky/config.json", "Config file") lg.InitCmd(rootCmd, cfg) // humidity.InitCmd(rootCmd, configDir) remote.InitCmd(rootCmd, cfg) sensor.InitCmd(rootCmd, cfg) temperature.InitCmd(rootCmd, cfg) rootCmd.Execute() }