PKGBUILD/cmd/root.go

83 lines
2.4 KiB
Go
Raw Normal View History

2018-11-07 19:07:15 +00:00
package cmd
import (
2019-02-22 12:08:58 +00:00
"fmt"
2019-08-20 19:37:45 +00:00
"math"
2019-02-22 12:08:58 +00:00
"os"
2019-02-24 21:46:36 +00:00
"time"
2019-02-22 12:08:58 +00:00
"github.com/Masterminds/semver"
"github.com/go-flucky/flucky/cmd/completion"
"github.com/go-flucky/flucky/cmd/compression"
"github.com/go-flucky/flucky/cmd/convert"
2019-06-15 13:58:41 +00:00
"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"
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/cmd/sensor"
"github.com/go-flucky/flucky/cmd/temperature"
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/pkg/types"
2019-02-22 12:08:58 +00:00
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/pkg/config"
2019-02-22 12:08:58 +00:00
uuid "github.com/satori/go.uuid"
2018-11-07 19:07:15 +00:00
"github.com/spf13/cobra"
)
var configFile string
2018-11-19 21:36:21 +00:00
2018-11-07 19:07:15 +00:00
var rootCmd = &cobra.Command{
Use: "flucky",
Short: "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
2019-02-22 12:08:58 +00:00
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// check if config file exists
if _, err := os.Stat(configFile); os.IsNotExist(err) {
2019-02-22 12:08:58 +00:00
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
2019-08-20 19:37:45 +00:00
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{
2019-02-22 12:08:58 +00:00
Device: &types.Device{
ID: uuid.NewV4().String(),
Name: hostname,
2019-08-20 19:37:45 +00:00
CreationDate: t,
2019-02-22 12:08:58 +00:00
},
StorageEndpoint: "file:///var/log/flucky/logfile.csv",
2019-02-22 12:08:58 +00:00
}
err = config.Write(&cnf, configFile)
2019-02-22 12:08:58 +00:00
if err != nil {
return err
}
}
return nil
},
2018-11-07 19:07:15 +00:00
}
// Execute a
func Execute(version *semver.Version) {
rootCmd.Version = version.String()
2018-11-07 19:07:15 +00:00
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
completion.InitCmd(rootCmd)
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)
2018-11-07 19:07:15 +00:00
rootCmd.Execute()
}