package cmd import ( "fmt" "os" "time" "github.com/go-flucky/flucky/cmd/daemon" "github.com/go-flucky/flucky/cmd/sensor" "github.com/go-flucky/flucky/cmd/temperature" "github.com/go-flucky/flucky/pkg/types" "github.com/go-flucky/flucky/pkg/config" uuid "github.com/satori/go.uuid" "github.com/spf13/cobra" ) var configPath 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(configPath); os.IsNotExist(err) { hostname, err := os.Hostname() if err != nil { return fmt.Errorf("Can not locate the hostname: %v", err) } cnf := config.Configuration{ Device: &types.Device{ DeviceID: uuid.NewV4().String(), DeviceName: hostname, HumidityLogfile: "/var/log/flucky/humidity.json", TemperatureLogfile: "/var/log/flucky/temperature.json", CreationDate: time.Now(), }, } err = config.Write(&cnf, configPath) if err != nil { return err } } return nil }, } // Execute a func Execute(version string) { rootCmd.Version = version rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/flucky/config.json", "Config file") daemon.InitCmd(rootCmd, configPath) sensor.InitCmd(rootCmd, configPath) temperature.InitCmd(rootCmd, configPath) rootCmd.Execute() }