refac(cmd): mergeing, ordering and outsourcing cmd subcommands
This commit is contained in:
@ -1,104 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
enabled bool
|
||||
gpioIn string
|
||||
i2cAddress uint8
|
||||
i2cBus int
|
||||
location string
|
||||
tickDuration string
|
||||
wireID string
|
||||
)
|
||||
|
||||
var addSensorCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add Sensor",
|
||||
Aliases: []string{"append"},
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: `flucky sensor add --gpio GPIO14 indoor DHT11
|
||||
flucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20
|
||||
flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// determine sensor model
|
||||
sensorModel, err := types.SelectSensorModel(args[1])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// create new sensor struct
|
||||
sensor := &types.Sensor{
|
||||
Name: args[0],
|
||||
Model: sensorModel,
|
||||
Location: location,
|
||||
Enabled: enabled,
|
||||
TickDuration: tickDuration,
|
||||
}
|
||||
|
||||
// determine gpio port if set
|
||||
if gpioIn != "" &&
|
||||
i2cAddress == 0 &&
|
||||
i2cBus == 0 &&
|
||||
wireID == "" {
|
||||
gpio, err := types.StringToGPIO(gpioIn)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
sensor.GPIONumber = &gpio
|
||||
}
|
||||
|
||||
// set i2c connection settings
|
||||
if gpioIn == "" &&
|
||||
i2cAddress != 0 &&
|
||||
i2cBus != 0 &&
|
||||
wireID == "" {
|
||||
sensor.I2CAddress = &i2cAddress
|
||||
sensor.I2CBus = &i2cBus
|
||||
}
|
||||
|
||||
// set wire connection settings
|
||||
if gpioIn == "" &&
|
||||
i2cAddress == 0 &&
|
||||
i2cBus == 0 &&
|
||||
wireID != "" {
|
||||
sensor.WireID = &wireID
|
||||
}
|
||||
|
||||
// add sensor entry to list
|
||||
err = cnf.AddSensor(sensor)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(addSensorCmd)
|
||||
|
||||
addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable new sensor")
|
||||
addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "Defines the GPIO port")
|
||||
addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "Defines the I2C address on the I2C bus")
|
||||
addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "Defines the I2C bus")
|
||||
addSensorCmd.Flags().StringVar(&location, "location", "", "Location of the sensor")
|
||||
addSensorCmd.Flags().StringVar(&tickDuration, "tick-duration", "1m", "Controls how often values should be read from the sensor when running flucky in daemon mode")
|
||||
addSensorCmd.Flags().StringVar(&wireID, "wire-id", "", "Defines the Wire-ID")
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var disableSensorCmd = &cobra.Command{
|
||||
Use: "disable",
|
||||
Short: "Disable Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Example: "flucky sensor disable outdoor",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// disable sensor entry to list
|
||||
err = cnf.DisableSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(disableSensorCmd)
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var enableSensorCmd = &cobra.Command{
|
||||
Use: "enable",
|
||||
Short: "Enable Sensor",
|
||||
Example: "flucky sensor enable outdoor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// disable sensor entry to list
|
||||
err = cnf.EnableSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(enableSensorCmd)
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var listSensorCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List Sensors",
|
||||
Aliases: []string{"ls"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// print sensors on stdout
|
||||
err = cli.PrintSensors(cnf, os.Stdout)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(listSensorCmd)
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rmSensorCmd = &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "Remove Sensor",
|
||||
Example: "flucky sensor rm outdoor",
|
||||
Aliases: []string{"rm"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// // add remote entry to list
|
||||
err = cnf.RemoveSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(rmSensorCmd)
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var renameSensorCmd = &cobra.Command{
|
||||
Use: "rename",
|
||||
Short: "Rename Sensor",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: fmt.Sprintf("flucky sensor rename indoor outdoor\nflucky sensor rename f98b00ea-a9b2-4e00-924f-113859d0af2d outdoor"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// rename sensor
|
||||
err = cnf.RenameSensor(args[0], args[1])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(renameSensorCmd)
|
||||
}
|
@ -1,25 +1,291 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"github.com/Masterminds/semver"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
configFile *string
|
||||
|
||||
version *semver.Version
|
||||
enabled bool
|
||||
gpioIn string
|
||||
i2cAddress uint8
|
||||
i2cBus int
|
||||
location string
|
||||
tickDuration string
|
||||
wireID string
|
||||
)
|
||||
|
||||
var sensorCmd = &cobra.Command{
|
||||
Use: "sensor",
|
||||
Short: "Manage Sensors",
|
||||
}
|
||||
// InitCmd initialize all sensor subcommands
|
||||
func InitCmd(cmd *cobra.Command) error {
|
||||
sensorCmd := &cobra.Command{
|
||||
Use: "sensor",
|
||||
Short: "Manage Sensors",
|
||||
}
|
||||
|
||||
// InitCmd da
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
addSensorCmd := &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add Sensor",
|
||||
Aliases: []string{"append"},
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: `flucky sensor add --gpio GPIO14 indoor DHT11
|
||||
flucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20
|
||||
flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`,
|
||||
RunE: addSensor,
|
||||
}
|
||||
addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable new sensor")
|
||||
addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "Defines the GPIO port")
|
||||
addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "Defines the I2C address on the I2C bus")
|
||||
addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "Defines the I2C bus")
|
||||
addSensorCmd.Flags().StringVar(&location, "location", "", "Location of the sensor")
|
||||
addSensorCmd.Flags().StringVar(&tickDuration, "tick-duration", "1m", "Controls how often values should be read from the sensor when running flucky in daemon mode")
|
||||
addSensorCmd.Flags().StringVar(&wireID, "wire-id", "", "Defines the Wire-ID")
|
||||
|
||||
disableSensorCmd := &cobra.Command{
|
||||
Use: "disable",
|
||||
Short: "Disable Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Example: "flucky sensor disable outdoor",
|
||||
RunE: deleteSensor,
|
||||
}
|
||||
|
||||
enableSensorCmd := &cobra.Command{
|
||||
Use: "enable",
|
||||
Short: "Enable Sensor",
|
||||
Example: "flucky sensor enable outdoor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: enableSensor,
|
||||
}
|
||||
|
||||
listSensorCmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List Sensors",
|
||||
Aliases: []string{"ls"},
|
||||
RunE: listSensors,
|
||||
}
|
||||
|
||||
removeSensorCmd := &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "Remove Sensor",
|
||||
Aliases: []string{"rm"},
|
||||
Example: "flucky sensor remove outdoor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: enableSensor,
|
||||
}
|
||||
|
||||
renameSensorCmd := &cobra.Command{
|
||||
Use: "rename",
|
||||
Short: "Rename Sensor",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: `flucky sensor rename indoor outdoor
|
||||
flucky sensor rename f98b00ea-a9b2-4e00-924f-113859d0af2d outdoor`,
|
||||
RunE: renameSensor,
|
||||
}
|
||||
|
||||
sensorCmd.AddCommand(addSensorCmd)
|
||||
sensorCmd.AddCommand(disableSensorCmd)
|
||||
sensorCmd.AddCommand(enableSensorCmd)
|
||||
sensorCmd.AddCommand(listSensorCmd)
|
||||
sensorCmd.AddCommand(removeSensorCmd)
|
||||
sensorCmd.AddCommand(renameSensorCmd)
|
||||
cmd.AddCommand(sensorCmd)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func addSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sensorModel, err := types.SelectSensorModel(args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sensor := &types.Sensor{
|
||||
Name: args[0],
|
||||
Model: sensorModel,
|
||||
Location: location,
|
||||
Enabled: enabled,
|
||||
TickDuration: tickDuration,
|
||||
}
|
||||
|
||||
// determine gpio port if set
|
||||
if gpioIn != "" &&
|
||||
i2cAddress == 0 &&
|
||||
i2cBus == 0 &&
|
||||
wireID == "" {
|
||||
gpio, err := types.StringToGPIO(gpioIn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sensor.GPIONumber = &gpio
|
||||
}
|
||||
|
||||
// set i2c connection settings
|
||||
if gpioIn == "" &&
|
||||
i2cAddress != 0 &&
|
||||
i2cBus != 0 &&
|
||||
wireID == "" {
|
||||
sensor.I2CAddress = &i2cAddress
|
||||
sensor.I2CBus = &i2cBus
|
||||
}
|
||||
|
||||
// set wire connection settings
|
||||
if gpioIn == "" &&
|
||||
i2cAddress == 0 &&
|
||||
i2cBus == 0 &&
|
||||
wireID != "" {
|
||||
sensor.WireID = &wireID
|
||||
}
|
||||
|
||||
// add sensor entry to list
|
||||
err = cnf.AddSensor(sensor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = cnf.DisableSensor(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = config.Write(cnf, configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func enableSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = cnf.EnableSensor(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = config.Write(cnf, configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func listSensors(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = cli.PrintSensors(cnf, os.Stdout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = config.Write(cnf, configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = cnf.RemoveSensor(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = config.Write(cnf, configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func renameSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = cnf.RenameSensor(args[0], args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = config.Write(cnf, configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user