2018-11-07 19:07:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-02-22 12:08:58 +00:00
|
|
|
"fmt"
|
2020-01-13 21:26:41 +00:00
|
|
|
"log"
|
2019-08-20 19:37:45 +00:00
|
|
|
"math"
|
2019-02-22 12:08:58 +00:00
|
|
|
"os"
|
2019-02-24 21:46:36 +00:00
|
|
|
"time"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2019-09-01 10:27:06 +00:00
|
|
|
"github.com/Masterminds/semver"
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/cmd/completion"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/compression"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/convert"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/daemon"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/db"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/humidity"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/pressure"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/rgbled"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/sensor"
|
|
|
|
"github.com/volker-raschek/flucky/cmd/temperature"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
2020-01-13 21:26:41 +00:00
|
|
|
"github.com/volker-raschek/go-logger/pkg/logger"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/config"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
var (
|
|
|
|
configFile string
|
|
|
|
)
|
|
|
|
|
|
|
|
// Execute a
|
|
|
|
func Execute(version *semver.Version) error {
|
|
|
|
|
|
|
|
rootCmd := &cobra.Command{
|
|
|
|
Use: "flucky",
|
|
|
|
Short: "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
|
|
|
|
PersistentPreRunE: preRunError,
|
|
|
|
Version: version.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
logLevel := ""
|
|
|
|
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
|
|
|
|
rootCmd.PersistentFlags().StringVar(&logLevel, "loglevel", "info", "Set the Loglevel. Possible values: debug, info, warn, error, fatal")
|
|
|
|
|
|
|
|
subCommands := []func(cmd *cobra.Command) error{
|
|
|
|
completion.InitCmd,
|
|
|
|
compression.InitCmd,
|
|
|
|
convert.InitCmd,
|
|
|
|
daemon.InitCmd,
|
|
|
|
humidity.InitCmd,
|
|
|
|
pressure.InitCmd,
|
|
|
|
rgbled.InitCmd,
|
|
|
|
sensor.InitCmd,
|
|
|
|
temperature.InitCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subCommand := range subCommands {
|
|
|
|
if err := subCommand(rootCmd); err != nil {
|
|
|
|
return err
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
2020-01-13 21:26:41 +00:00
|
|
|
}
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2020-01-18 13:55:43 +00:00
|
|
|
err := db.InitCmd(rootCmd, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rootCmd.Execute()
|
2020-01-13 21:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2018-11-07 19:07:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
func parseLogger(logLevel string) logger.Logger {
|
|
|
|
log.Println(logLevel)
|
|
|
|
switch logLevel {
|
|
|
|
case "debug", "DEBUG":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelDebug)
|
|
|
|
case "info", "INFO":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelInfo)
|
|
|
|
case "warn", "WARN":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelWarn)
|
|
|
|
case "error", "ERROR":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelError)
|
|
|
|
case "fatal", "FATAL":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelFatal)
|
|
|
|
default:
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelInfo)
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 19:07:15 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
func preRunError(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
// check if config file exists
|
|
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not locate the hostname: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Time must be truncted for postgres. Postgres currently does not support
|
|
|
|
// nanoseconds which is automatically include into the go time object
|
|
|
|
postgresTimeStamp := time.Now()
|
|
|
|
location, err := time.LoadLocation("Europe/Berlin")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
postgresTimeStamp = time.Date(postgresTimeStamp.Year(), postgresTimeStamp.Month(), postgresTimeStamp.Day(), postgresTimeStamp.Hour(), postgresTimeStamp.Minute(), postgresTimeStamp.Second(), int(math.Round(float64(postgresTimeStamp.Nanosecond())/1000000)*1000000), location)
|
|
|
|
|
|
|
|
// Default configuration
|
|
|
|
cnf := config.Configuration{
|
|
|
|
Device: &types.Device{
|
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Name: hostname,
|
|
|
|
CreationDate: postgresTimeStamp,
|
|
|
|
},
|
|
|
|
StorageEndpoint: "file:///var/log/flucky/logfile.csv",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = config.Write(&cnf, configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 11:00:50 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
return nil
|
2018-11-07 19:07:15 +00:00
|
|
|
}
|