PKGBUILD/cmd/cmd.go

81 lines
2.3 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)
// Default configuration
cnf := config.Configuration{
Device: &types.Device{
DeviceID: uuid.NewV4().String(),
DeviceName: hostname,
CreationDate: t,
},
StorageEndpoint: "file:///var/log/flucky/logfile.csv",
}
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, version)
convert.InitCmd(rootCmd, &configFile, version)
daemon.InitCmd(rootCmd, &configFile, version)
humidity.InitCmd(rootCmd, &configFile, version)
pressure.InitCmd(rootCmd, &configFile, version)
rgbled.InitCmd(rootCmd, &configFile, version)
sensor.InitCmd(rootCmd, &configFile, version)
temperature.InitCmd(rootCmd, &configFile, version)
rootCmd.Execute()
}