Add a command for validateColor API

This commit is contained in:
Ryan Cavicchioni 2021-04-21 01:33:35 +00:00
parent 8133953bb1
commit 23210af0c3
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 53 additions and 0 deletions

View File

@ -20,6 +20,7 @@ func init() {
RegisterCommand(NewCmdToggle())
RegisterCommand(NewCmdVersion())
RegisterCommand(NewCmdBreathe())
RegisterCommand(NewCmdValidate())
}
var Version string

48
cmd/validate.go Normal file
View File

@ -0,0 +1,48 @@
package lumecmd
import (
"errors"
"flag"
"fmt"
"git.kill0.net/chill9/lifx-go"
)
func NewCmdValidate() Command {
return Command{
Name: "validate",
Func: ValidateCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("validate", flag.ExitOnError)
return fs
}(),
Use: "<command>",
Short: "Validate a color string",
}
}
func ValidateCmd(ctx Context) (int, error) {
c := ctx.Client
if len(ctx.Args) != 1 {
printCmdHelp(ctx.Name)
return ExitFailure, nil
}
color := lifx.NamedColor(ctx.Args[0])
i, err := c.ValidateColor(color)
if err != nil {
return ExitFailure, err
}
if validColor, ok := i.(*lifx.HSBKColor); ok {
fmt.Print(validColor)
} else {
return ExitFailure, errors.New("go type %T but wanted *HSBKColor")
}
fmt.Println()
return ExitSuccess, nil
}

4
lume.1
View File

@ -90,6 +90,10 @@ Show extended details about the lights
.Op Fl Fl simple | Fl Fl table
.Xc
Toggle the power
.It Xo Ic validate
.Ar color_string
.Xc
Validate a color string
.It Xo Ic version
.Xc
Print the version