PKGBUILD/cmd/root.go

131 lines
3.5 KiB
Go

package cmd
import (
"fmt"
"log"
"math"
"os"
"time"
"github.com/Masterminds/semver"
"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"
"github.com/volker-raschek/go-logger/pkg/logger"
"github.com/volker-raschek/flucky/pkg/config"
uuid "github.com/satori/go.uuid"
"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
}
}
err := db.InitCmd(rootCmd, version)
if err != nil {
return err
}
err = rootCmd.Execute()
if err != nil {
return err
}
return nil
}
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)
}
}
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
}