2020-05-17 11:00:51 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2020-06-10 19:13:05 +00:00
|
|
|
"git.cryptic.systems/volker.raschek/flucky/cli/daemon"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/cli/sensor"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
2020-05-17 11:00:51 +00:00
|
|
|
"github.com/Masterminds/semver"
|
|
|
|
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Execute a
|
|
|
|
func Execute(version *semver.Version) error {
|
|
|
|
|
|
|
|
rootCmd := &cobra.Command{
|
|
|
|
Use: "flucky",
|
|
|
|
// Short: "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
|
|
|
|
PersistentPreRunE: preRunError,
|
|
|
|
Version: version.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultConfigFile, err := getDefaultConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.PersistentFlags().String("config", defaultConfigFile, "Config file")
|
|
|
|
rootCmd.PersistentFlags().String("loglevel", "info", "Set the Loglevel. Possible values: debug, info, warn, error, fatal")
|
|
|
|
|
|
|
|
subCommands := []func(cmd *cobra.Command) error{
|
|
|
|
daemon.InitCmd,
|
|
|
|
sensor.InitCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subCommand := range subCommands {
|
|
|
|
if err := subCommand(rootCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func preRunError(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
configFile := cmd.Flag("config").Value.String()
|
|
|
|
|
|
|
|
// check if config file exists
|
|
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
|
|
|
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to determine 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
|
|
|
|
postgresTimeStamp := time.Now()
|
|
|
|
location, err := time.LoadLocation("Europe/Berlin")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
postgresTimeStamp = time.Date(postgresTimeStamp.Year(), postgresTimeStamp.Month(), postgresTimeStamp.Day(), postgresTimeStamp.Hour(), postgresTimeStamp.Minute(), postgresTimeStamp.Second(), int(math.Round(float64(postgresTimeStamp.Nanosecond())/1000000)*1000000), location)
|
|
|
|
|
2020-07-30 22:02:29 +00:00
|
|
|
// default cache directory
|
|
|
|
defaultCacheDir, err := getDefaultCacheDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-17 11:00:51 +00:00
|
|
|
// Default configuration
|
2020-07-30 22:02:29 +00:00
|
|
|
dsn := fmt.Sprintf("sqlite3://%v/sqlite.db?cache=shared&mode=memory&foreign_keys=on", defaultCacheDir)
|
2020-05-17 11:00:51 +00:00
|
|
|
cnf := config.Config{
|
|
|
|
Device: &types.Device{
|
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Name: hostname,
|
|
|
|
CreationDate: postgresTimeStamp,
|
|
|
|
},
|
2020-07-30 22:02:29 +00:00
|
|
|
DSN: dsn,
|
2020-05-17 11:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = config.Write(&cnf, configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-30 22:02:29 +00:00
|
|
|
// getDefaultConfigFile returns the default path to the configuration file of
|
|
|
|
// flucky
|
2020-05-17 11:00:51 +00:00
|
|
|
func getDefaultConfigFile() (string, error) {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Can not read current user: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch u.Uid {
|
|
|
|
case "0":
|
|
|
|
return "/etc/flucky/config.json", nil
|
|
|
|
default:
|
|
|
|
return filepath.Join(u.HomeDir, ".config/flucky/config.json"), nil
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 22:02:29 +00:00
|
|
|
|
|
|
|
// getDefaultCacheDir returns the default path to the cache directory where
|
|
|
|
// flucky stores his measured values.
|
|
|
|
func getDefaultCacheDir() (string, error) {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Can not read current user: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch u.Uid {
|
|
|
|
case "0":
|
|
|
|
return "/var/cache/flucky", nil
|
|
|
|
default:
|
|
|
|
return filepath.Join(u.HomeDir, ".cache/flucky"), nil
|
|
|
|
}
|
|
|
|
}
|