PKGBUILD/cmd/cmd.go

82 lines
2.2 KiB
Go

package cmd
import (
"fmt"
"math"
"os"
"time"
"github.com/Masterminds/semver"
"github.com/go-flucky/flucky/cmd/compression"
"github.com/go-flucky/flucky/cmd/convert"
"github.com/go-flucky/flucky/cmd/daemon"
"github.com/go-flucky/flucky/cmd/humidity"
"github.com/go-flucky/flucky/cmd/pressure"
"github.com/go-flucky/flucky/cmd/rgbled"
"github.com/go-flucky/flucky/cmd/sensor"
"github.com/go-flucky/flucky/cmd/temperature"
"github.com/go-flucky/flucky/pkg/types"
"github.com/go-flucky/flucky/pkg/config"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
)
var configFile string
var rootCmd = &cobra.Command{
Use: "flucky",
Short: "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
PersistentPreRunE: func(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
t := time.Now()
l, _ := time.LoadLocation("Europe/Berlin")
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), int(math.Round(float64(t.Nanosecond())/1000000)*1000000), l)
cnf := config.Configuration{
Device: &types.Device{
DeviceID: uuid.NewV4().String(),
DeviceName: hostname,
Logfile: "/var/log/flucky/logfile.csv",
CreationDate: t,
},
}
err = config.Write(&cnf, configFile)
if err != nil {
return err
}
}
return nil
},
}
// Execute a
func Execute(version *semver.Version) {
rootCmd.Version = version.String()
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
compression.InitCmd(rootCmd, &configFile)
convert.InitCmd(rootCmd, &configFile)
daemon.InitCmd(rootCmd, &configFile)
// db.InitCmd(rootCmd, &configFile)
humidity.InitCmd(rootCmd, &configFile)
pressure.InitCmd(rootCmd, &configFile)
rgbled.InitCmd(rootCmd, &configFile)
sensor.InitCmd(rootCmd, &configFile)
temperature.InitCmd(rootCmd, &configFile)
rootCmd.Execute()
}