PKGBUILD/cmd/rgbled/add.go

78 lines
1.7 KiB
Go
Raw Normal View History

package rgbled
import (
"fmt"
"log"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/types"
"github.com/spf13/cobra"
)
var enabled bool
2019-06-16 18:11:10 +00:00
var location string
var addRgbLedCmd = &cobra.Command{
Use: "add",
Short: "Add a RGB-LED",
Aliases: []string{"append"},
Args: cobra.ExactArgs(4),
Example: fmt.Sprintf(`flucky rgb-led add <name> <gpio-for-blue> <gpio-for-green> <gpio-for-red>
flucky rgb-led add my-led GPIO13 GPIO17 GPIO26`),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(*configFile)
if err != nil {
log.Fatalln(err)
}
// determine gpio port
2019-06-16 18:11:10 +00:00
gpioBlue, err := types.StringToGPIO(args[1])
if err != nil {
log.Fatalln(err)
}
gpioGreen, err := types.StringToGPIO(args[2])
if err != nil {
log.Fatalln(err)
}
2019-06-16 18:11:10 +00:00
gpioRed, err := types.StringToGPIO(args[3])
if err != nil {
log.Fatalln(err)
}
// create new sensor struct
rgbLED := &types.RGBLED{
RGBLEDName: args[0],
RGBLEDLocation: location,
RGBLEDEnabled: enabled,
ActionMapping: types.DefaultActionMapping,
2019-06-16 18:11:10 +00:00
BaseColorsToGPIO: map[types.BaseColor]*types.GPIO{
types.BaseColorBlue: &gpioBlue,
types.BaseColorGreen: &gpioGreen,
types.BaseColorRed: &gpioRed,
},
}
// // add sensor entry to list
err = cnf.AddRGBLED(rgbLED)
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(cnf, *configFile)
if err != nil {
log.Fatalln(err)
}
},
}
func init() {
rgbLedCmd.AddCommand(addRgbLedCmd)
addRgbLedCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
addRgbLedCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location")
}