lume/cmd/setstate.go

103 lines
2.6 KiB
Go
Raw Normal View History

2020-03-28 00:31:41 +00:00
package lumecmd
import (
2021-02-17 05:24:32 +00:00
"flag"
"git.kill0.net/chill9/lifx-go"
2020-03-28 00:31:41 +00:00
)
2021-02-17 05:24:32 +00:00
func NewCmdSetState() Command {
return Command{
Name: "set-state",
Func: SetStateCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-state", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
color := fs.String("color", defaultColor, "color state")
fs.StringVar(color, "c", defaultColor, "color state")
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")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
2021-03-11 02:57:05 +00:00
fs.String("format", defaultOutputFormat, "Set the output format")
2021-02-17 05:24:32 +00:00
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
Short: "Set various state attributes",
}
}
2020-03-29 05:08:17 +00:00
func SetStateCmd(args CmdArgs) (int, error) {
2021-03-13 22:20:18 +00:00
var p Printer
2020-03-28 00:31:41 +00:00
c := args.Client
state := lifx.State{}
selector := args.Flags.String("selector")
2021-03-11 02:57:05 +00:00
format := args.Flags.String("format")
if format == "" && args.Config.OutputFormat != "" {
format = args.Config.OutputFormat
}
2020-03-28 00:31:41 +00:00
power := args.Flags.String("power")
if power != "" {
state.Power = power
}
color := args.Flags.String("color")
if color != "" {
state.Color = lifx.NamedColor(color)
}
brightnessFlag := args.Flags.String("brightness")
if brightnessFlag != "" {
brightness := args.Flags.Float64("brightness")
state.Brightness = brightness
}
duration := args.Flags.Float64("duration")
state.Duration = duration
infraredFlag := args.Flags.String("infrared")
if infraredFlag != "" {
infrared := args.Flags.Float64("infrared")
state.Infrared = infrared
}
fast := args.Flags.Bool("fast")
state.Fast = fast
if power == "" && color == "" && brightnessFlag == "" && infraredFlag == "" {
printCmdHelp(args.Name)
return ExitFailure, nil
}
2020-03-28 00:31:41 +00:00
r, err := c.SetState(selector, state)
if err != nil {
2021-01-31 17:08:24 +00:00
return ExitFailure, err
2020-03-28 00:31:41 +00:00
}
if !fast {
2021-03-13 22:20:18 +00:00
p = NewPrinter(format)
p.Results(r.Results)
2020-03-28 00:31:41 +00:00
}
return ExitSuccess, nil
2020-03-28 00:31:41 +00:00
}