PKGBUILD/cmd/cmd.go

66 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"os"
"time"
"github.com/go-flucky/flucky/cmd/daemon"
"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: "Read from sensors",
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)
}
cnf := config.Configuration{
Device: &types.Device{
DeviceID: uuid.NewV4().String(),
DeviceName: hostname,
HumidityLogfile: "/var/log/flucky/humidity.json",
TemperatureLogfile: "/var/log/flucky/temperature.json",
CreationDate: time.Now(),
},
}
err = config.Write(&cnf, configFile)
if err != nil {
return err
}
}
return nil
},
}
// Execute a
func Execute(version string) {
rootCmd.Version = version
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
daemon.InitCmd(rootCmd, configFile)
//humidity.InitCmd(rootCmd, configFile)
rgbled.InitCmd(rootCmd, configFile)
sensor.InitCmd(rootCmd, configFile)
temperature.InitCmd(rootCmd, configFile)
rootCmd.Execute()
}