2018-11-07 19:07:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-02-22 12:08:58 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-02-24 21:46:36 +00:00
|
|
|
"time"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2019-05-12 09:57:53 +00:00
|
|
|
"github.com/volker-raschek/flucky/cmd/sensor"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/temperature"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
|
|
|
uuid "github.com/satori/go.uuid"
|
2018-11-07 19:07:15 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-05-12 09:57:53 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/config"
|
2018-11-07 19:07:15 +00:00
|
|
|
)
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
var configPath string
|
2018-11-19 21:36:21 +00:00
|
|
|
|
2018-11-07 19:07:15 +00:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "flucky",
|
|
|
|
Short: "Read from sensors",
|
2019-02-22 12:08:58 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
// check if config file exists
|
2019-06-13 19:25:32 +00:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
2019-02-22 12:08:58 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not locate the hostname: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
cnf := config.Configuration{
|
2019-02-22 12:08:58 +00:00
|
|
|
Device: &types.Device{
|
2019-06-13 21:21:41 +00:00
|
|
|
DeviceID: uuid.NewV4().String(),
|
|
|
|
DeviceName: hostname,
|
|
|
|
HumidityLogfile: "/var/log/flucky/humidity.json",
|
|
|
|
TemperatureLogfile: "/var/log/flucky/temperature.json",
|
|
|
|
CreationDate: time.Now(),
|
2019-02-22 12:08:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
err = config.Write(&cnf, configPath)
|
2019-02-22 12:08:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2018-11-07 19:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute a
|
|
|
|
func Execute(version string) {
|
|
|
|
rootCmd.Version = version
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/flucky/config.json", "Config file")
|
2019-06-15 12:25:45 +00:00
|
|
|
// humidity.InitCmd(rootCmd, configPath)
|
2019-06-13 19:25:32 +00:00
|
|
|
sensor.InitCmd(rootCmd, configPath)
|
|
|
|
temperature.InitCmd(rootCmd, configPath)
|
2018-11-07 19:07:15 +00:00
|
|
|
rootCmd.Execute()
|
|
|
|
}
|