PKGBUILD/cmd/cmd.go
2019-03-04 10:50:20 +01:00

71 lines
1.6 KiB
Go

package cmd
import (
"fmt"
"os"
"time"
"git.cryptic.systems/fh-trier/go-flucky/cmd/remote"
"git.cryptic.systems/fh-trier/go-flucky/cmd/sensor"
"git.cryptic.systems/fh-trier/go-flucky/cmd/temperature"
"git.cryptic.systems/fh-trier/go-flucky/pkg/logger"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
)
var cfg string
var rootCmd = &cobra.Command{
Use: "flucky",
Short: "Read from sensors",
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)
}
logfiles := make(map[logger.LogValue][]string)
logfiles[logger.LogHumidity] = []string{"/var/log/flucky/humidity.log"}
logfiles[logger.LogTemperature] = []string{"/var/log/flucky/temperature.log"}
fc := config.FluckyConfig{
Device: &types.Device{
DeviceID: uuid.NewV4().String(),
DeviceName: hostname,
CreationDate: time.Now(),
},
FileLogger: &logger.FileLogger{
LogFiles: logfiles,
},
}
err = config.Write(&fc, cfg)
if err != nil {
return err
}
}
return nil
},
}
// Execute a
func Execute(version string) {
rootCmd.Version = version
rootCmd.PersistentFlags().StringVar(&cfg, "config", "/etc/flucky/config.json", "Config file")
// humidity.InitCmd(rootCmd, configDir)
remote.InitCmd(rootCmd, cfg)
sensor.InitCmd(rootCmd, cfg)
temperature.InitCmd(rootCmd, cfg)
rootCmd.Execute()
}