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-03-04 19:52:41 +00:00
|
|
|
lg "git.cryptic.systems/fh-trier/go-flucky/cmd/logger"
|
2018-11-19 21:36:21 +00:00
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/cmd/remote"
|
2019-02-24 21:46:36 +00:00
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/cmd/sensor"
|
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/cmd/temperature"
|
2019-03-04 09:45:00 +00:00
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/logger"
|
2019-02-24 21:46:36 +00:00
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
2018-11-07 19:07:15 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-02-17 17:23:59 +00:00
|
|
|
var cfg 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
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-03-04 09:45:00 +00:00
|
|
|
logfiles := make(map[logger.LogValue][]string)
|
|
|
|
logfiles[logger.LogHumidity] = []string{"/var/log/flucky/humidity.log"}
|
|
|
|
logfiles[logger.LogTemperature] = []string{"/var/log/flucky/temperature.log"}
|
|
|
|
|
2019-02-22 12:08:58 +00:00
|
|
|
fc := config.FluckyConfig{
|
|
|
|
Device: &types.Device{
|
2019-02-24 21:46:36 +00:00
|
|
|
DeviceID: uuid.NewV4().String(),
|
2019-02-28 18:48:25 +00:00
|
|
|
DeviceName: hostname,
|
2019-02-24 21:46:36 +00:00
|
|
|
CreationDate: time.Now(),
|
2019-02-22 12:08:58 +00:00
|
|
|
},
|
2019-03-04 09:45:00 +00:00
|
|
|
FileLogger: &logger.FileLogger{
|
|
|
|
LogFiles: logfiles,
|
|
|
|
},
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = config.Write(&fc, cfg)
|
|
|
|
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-02-17 17:23:59 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&cfg, "config", "/etc/flucky/config.json", "Config file")
|
2018-11-19 21:36:21 +00:00
|
|
|
|
2019-03-04 19:52:41 +00:00
|
|
|
lg.InitCmd(rootCmd, cfg)
|
2019-02-17 17:23:59 +00:00
|
|
|
// humidity.InitCmd(rootCmd, configDir)
|
|
|
|
remote.InitCmd(rootCmd, cfg)
|
2019-02-24 21:46:36 +00:00
|
|
|
sensor.InitCmd(rootCmd, cfg)
|
|
|
|
temperature.InitCmd(rootCmd, cfg)
|
2018-11-07 19:07:15 +00:00
|
|
|
|
|
|
|
rootCmd.Execute()
|
|
|
|
}
|