59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
|
"git.cryptic.systems/fh-trier/go-flucky/cmd/remote"
|
|
|
|
"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)
|
|
}
|
|
|
|
fc := config.FluckyConfig{
|
|
Device: &types.Device{
|
|
DeviceID: uuid.NewV4().String(),
|
|
DeviceName: &hostname,
|
|
},
|
|
}
|
|
|
|
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, configDir)
|
|
|
|
rootCmd.Execute()
|
|
}
|