refac(cmd): mergeing, ordering and outsourcing cmd subcommands
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
@ -12,33 +13,64 @@ import (
|
||||
var (
|
||||
cachedMeasuredValues uint
|
||||
compression bool
|
||||
configFile *string
|
||||
round float64
|
||||
temperatureUnit string
|
||||
)
|
||||
|
||||
var daemonCmd = &cobra.Command{
|
||||
Use: "daemon",
|
||||
Short: "Read continuously data from all enabled sensors",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
// InitCmd initialize all daemon subcommands
|
||||
func InitCmd(cmd *cobra.Command) error {
|
||||
daemonCmd := &cobra.Command{
|
||||
Use: "daemon",
|
||||
Short: "Read continuously data from all enabled sensors",
|
||||
Example: "flucky daemon",
|
||||
RunE: run,
|
||||
}
|
||||
|
||||
logger := logger.NewDefaultLogger(logger.LogLevelDebug)
|
||||
daemon.SetLogger(logger)
|
||||
daemon.Start(cnf, cachedMeasuredValues, compression, round)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
|
||||
cmd.AddCommand(daemonCmd)
|
||||
daemonCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values")
|
||||
daemonCmd.Flags().UintVar(&cachedMeasuredValues, "cached-values", 500, "Number of cached values before saveing into the storage endpoint")
|
||||
daemonCmd.Flags().Float64Var(&round, "round", 0.5, "Round values. The value 0 deactivates the function")
|
||||
cmd.AddCommand(daemonCmd)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func run(cmd *cobra.Command, args []string) error {
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined: %v", err)
|
||||
}
|
||||
|
||||
// WARNING: Must set logger for daemon package
|
||||
// logLevel, err := cmd.Flags().GetString("loglevel")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("No loglevel defined: %v", err)
|
||||
// }
|
||||
|
||||
//flogger := initializeLogger(logLevel)
|
||||
// daemon.SetLogLevel(flogger)
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return daemon.Start(cnf, cachedMeasuredValues, compression, round)
|
||||
}
|
||||
|
||||
func initializeLogger(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 nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user