PKGBUILD/cmd/root.go

131 lines
3.5 KiB
Go
Raw Normal View History

2018-11-07 19:07:15 +00:00
package cmd
import (
2019-02-22 12:08:58 +00:00
"fmt"
"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
"github.com/Masterminds/semver"
"github.com/go-flucky/flucky/cmd/completion"
"github.com/go-flucky/flucky/cmd/compression"
"github.com/go-flucky/flucky/cmd/convert"
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/cmd/daemon"
"github.com/go-flucky/flucky/cmd/db"
"github.com/go-flucky/flucky/cmd/humidity"
"github.com/go-flucky/flucky/cmd/pressure"
"github.com/go-flucky/flucky/cmd/rgbled"
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/cmd/sensor"
"github.com/go-flucky/flucky/cmd/temperature"
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/pkg/types"
"github.com/volker-raschek/go-logger/pkg/logger"
2019-02-22 12:08:58 +00:00
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/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"
)
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
}
}
2019-02-22 12:08:58 +00:00
err := db.InitCmd(rootCmd, version)
if err != nil {
return err
}
err = rootCmd.Execute()
if err != nil {
return err
}
return nil
2018-11-07 19:07:15 +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
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
}
}
return nil
2018-11-07 19:07:15 +00:00
}