lume/cmd/setcolor.go

138 lines
3.4 KiB
Go
Raw Normal View History

2020-03-28 23:05:35 +00:00
package lumecmd
import (
2021-02-17 05:24:32 +00:00
"flag"
2020-03-28 23:05:35 +00:00
"fmt"
"git.kill0.net/chill9/lifx-go"
2020-03-28 23:05:35 +00:00
)
2021-02-17 05:24:32 +00:00
func NewCmdSetColor() Command {
return Command{
Name: "set-color",
Func: SetColorCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-color", flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
hue := fs.String("hue", defaultHue, "hue level")
fs.StringVar(hue, "H", defaultHue, "hue level")
saturation := fs.String("saturation", defaultSaturation, "saturation level")
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
rgb := fs.String("rgb", defaultRGB, "RGB value")
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
name := fs.String("name", defaultName, "named color")
fs.StringVar(name, "n", defaultName, "named color")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
Short: "Set the color",
}
}
2021-03-17 00:23:50 +00:00
func SetColorCmd(ctx Context) (int, error) {
2021-03-13 22:20:18 +00:00
var p Printer
2021-03-17 00:23:50 +00:00
c := ctx.Client
2020-03-28 23:05:35 +00:00
state := lifx.State{}
2021-03-17 00:23:50 +00:00
selector := ctx.Flags.String("selector")
2021-04-11 02:05:05 +00:00
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
2021-03-11 02:57:05 +00:00
2021-03-17 00:23:50 +00:00
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
2021-03-11 02:57:05 +00:00
}
2020-03-28 23:05:35 +00:00
2021-03-17 00:23:50 +00:00
power := ctx.Flags.String("power")
2020-03-28 23:05:35 +00:00
if power != "" {
state.Power = power
}
2021-03-17 00:23:50 +00:00
hueFlag := ctx.Flags.String("hue")
saturationFlag := ctx.Flags.String("saturation")
rgbFlag := ctx.Flags.String("rgb")
name := ctx.Flags.String("name")
2020-03-28 23:05:35 +00:00
if (hueFlag == "" || saturationFlag == "") && rgbFlag == "" && name == "" {
2021-03-17 00:23:50 +00:00
printCmdHelp(ctx.Name)
2021-01-31 17:08:24 +00:00
return ExitFailure, nil
}
2020-03-28 23:05:35 +00:00
if hueFlag != "" || saturationFlag != "" {
color := lifx.HSBKColor{}
if hueFlag != "" {
2021-03-17 00:23:50 +00:00
hue := ctx.Flags.Float32("hue")
2020-03-28 23:05:35 +00:00
color.H = lifx.Float32Ptr(hue)
}
if saturationFlag != "" {
2021-03-17 00:23:50 +00:00
saturation := ctx.Flags.Float32("saturation")
2020-03-28 23:05:35 +00:00
color.S = lifx.Float32Ptr(saturation)
}
state.Color = color
} else if rgbFlag != "" {
color, err := parseRGB(rgbFlag)
if err != nil {
2021-01-31 17:08:24 +00:00
return ExitFailure, err
2020-03-28 23:05:35 +00:00
}
state.Color = color
2020-04-04 15:27:09 +00:00
} else if name != "" {
2021-03-17 00:23:50 +00:00
hsb, ok := ctx.Config.Colors[name]
2020-04-04 15:27:09 +00:00
if !ok {
2021-01-31 17:08:24 +00:00
return ExitFailure, fmt.Errorf("%s is not a defined color", name)
2020-04-04 15:27:09 +00:00
}
color, err := lifx.NewHSBColor(hsb[0], hsb[1], hsb[2])
if err != nil {
2021-01-31 17:08:24 +00:00
return ExitFailure, err
2020-04-04 15:27:09 +00:00
}
state.Color = color
2020-03-28 23:05:35 +00:00
}
2021-03-17 00:23:50 +00:00
brightnessFlag := ctx.Flags.String("brightness")
2020-03-28 23:05:35 +00:00
if brightnessFlag != "" {
2021-03-17 00:23:50 +00:00
brightness := ctx.Flags.Float64("brightness")
2020-03-28 23:05:35 +00:00
state.Brightness = brightness
}
2021-03-17 00:23:50 +00:00
duration := ctx.Flags.Float64("duration")
2020-03-28 23:05:35 +00:00
state.Duration = duration
2021-03-17 00:23:50 +00:00
fast := ctx.Flags.Bool("fast")
2020-03-28 23:05:35 +00:00
state.Fast = fast
r, err := c.SetState(selector, state)
if err != nil {
fmt.Printf("fatal: %s\n", err)
2021-01-31 17:08:24 +00:00
return ExitFailure, err
2020-03-28 23:05:35 +00:00
}
if !fast {
2021-03-13 22:20:18 +00:00
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
2020-03-28 23:05:35 +00:00
}
return ExitSuccess, nil
2020-03-28 23:05:35 +00:00
}