lume/cmd/toggle.go

51 lines
1.1 KiB
Go
Raw Normal View History

2020-03-27 00:05:50 +00:00
package lumecmd
2021-02-17 05:24:32 +00:00
import (
"flag"
)
func NewCmdToggle() Command {
return Command{
Name: "toggle",
Func: ToggleCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("toggle", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
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>] [--duration <sec>]",
Short: "Toggle the power on/off",
}
}
2021-03-17 00:23:50 +00:00
func ToggleCmd(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
duration := ctx.Flags.Float64("duration")
selector := ctx.Flags.String("selector")
format := ctx.Flags.String("format")
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-27 00:05:50 +00:00
r, err := c.Toggle(selector, duration)
if err != nil {
2021-01-31 17:08:24 +00:00
return ExitFailure, err
2020-03-27 00:05:50 +00:00
}
2021-03-11 02:57:05 +00:00
2021-03-13 22:20:18 +00:00
p = NewPrinter(format)
p.Results(r.Results)
2021-03-11 02:57:05 +00:00
return ExitSuccess, nil
2020-03-27 00:05:50 +00:00
}